Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34383
  • 博文数量: 7
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 11
  • 用 户 组: 普通用户
  • 注册时间: 2016-01-20 14:45
文章分类
文章存档

2017年(1)

2016年(6)

我的朋友

分类: LINUX

2016-03-30 23:15:44


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MAXNUM 4
  4. #define OVERFLOW -1
  5. #define UNDERFLOW -2
  6. #define FLAGERROR -3

  7. typedef int elemtype;

  8. typedef struct stack
  9. {
  10.         int top;
  11.         int flag;
  12. }stack;

  13. typedef struct ShareArrStack
  14. {
  15.         elemtype data[MAXNUM];
  16.         stack stack1;
  17.         stack stack2;
  18. }ShareArrStack;

  19. void InitStack(ShareArrStack *s)
  20. {
  21.         (s->stack1).top = -1;
  22.         (s->stack1).flag = 1;
  23.         (s->stack2).top = MAXNUM;
  24.         (s->stack2).flag = 2;
  25. }

  26. int IsFull(ShareArrStack *s)
  27. {
  28.         return (s->stack1.top + 1 == s->stack2.top);
  29. }

  30. int IsEmpty(stack *sn)
  31. {
  32.         return ((sn->top == -1)||(sn->top == MAXNUM));
  33. }

  34. void Push(ShareArrStack *s,stack sn,elemtype x)
  35. {
  36.         if (IsFull(s))
  37.         {
  38.                 printf("Stack overflow!");
  39.                 exit(OVERFLOW);
  40.         }
  41.         else
  42.         {
  43.                 switch(sn.flag)
  44.                 {
  45.                 case 1:
  46.                         {
  47.                                 s->stack1.top += 1;
  48.                                 s->data[s->stack1.top] = x;
  49.                                 break;
  50.                         }
  51.                 case 2:
  52.                         {
  53.                                 s->stack2.top -= 1;
  54.                                 s->data[s->stack2.top] = x;
  55.                                 break;
  56.                         }
  57.                 default:
  58.                         {
  59.                                 exit(FLAGERROR);
  60.                         }
  61.                 }
  62.         }
  63. }

  64. elemtype Pop(ShareArrStack *s,stack sn)
  65. {
  66.         if (IsEmpty(&sn))
  67.         {
  68.                 printf("stack underflow!");
  69.                 exit(UNDERFLOW);
  70.         }
  71.         else
  72.         {
  73.                 switch(sn.flag)
  74.                 {
  75.                 case 1:
  76.                         {
  77.                                 return (s->data[s->stack1.top --]);
  78.                                 break;
  79.                         }
  80.                 case 2:
  81.                         {
  82.                                 return (s->data[s->stack2.top ++]);
  83.                                 break;
  84.                         }
  85.                 default:
  86.                         {
  87.                                 exit(FLAGERROR);
  88.                         }
  89.                 }
  90.         }
  91. }

  92. int main()
  93. {
  94.         ShareArrStack *s = NULL;
  95.         s = (ShareArrStack *)malloc(sizeof(ShareArrStack));
  96.         InitStack(s);
  97.         Push(s,s->stack1,1);
  98.         Push(s,s->stack2,9);
  99.         Push(s,s->stack1,2);
  100.         Push(s,s->stack2,8);
  101.         printf("%d, ",Pop(s,s->stack2));
  102.         printf("%d",Pop(s,s->stack1));
  103.         return 0;
  104. }

[root@localhost C++]# ./Arr2Stack
8, 2[root@localhost C++]#
阅读(1514) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~