两个顺序栈共享一个数组的存储空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//两个顺序栈共享一个数据空间
#include<stdio.h>
#include "stdlib.h"
#define MAXSIZE 100 //是顺序栈所能存储的最多元素个数
typedef int datatype;
typedef struct//顺序栈的定义 
{
    datatype stack[MAXSIZE]; //stack 数组存储栈中所有的数据元素 
    int top;
 } seqstack;//顺序栈的类型定义 
 seqstack *S,*B,s,b;//顺序栈变量定义 
 
void INITSTACK(seqstack *S,seqstack *B)
{
    S->top=-1;
    B->top=MAXSIZE;
    printf("S,B两个栈共享一个数组的存储空间!");
}
 
 int EMPTY(seqstack *S,seqstack *B)
 {
     if(S->top<0&&B->top>=MAXSIZE)
     {printf("栈空!");
     return 1;
     }
     else return 0;
 }
 int FULL(seqstack *S,seqstack *B)
 {
     if(S->top==B->top-1)
     {printf("栈满");
     return 1;
     }
    else return 0
  } 
  seqstack *SPUSH(seqstack *S,datatype x)
  {
      
      if(FULL(S,B))
      {
         return 0
      }
      else 
      {printf("请输入数据");
          scanf("%d",&x);
          S->top++;
          S->stack[S->top]=x;
      }
      return S;
   } 
   int SPOP(seqstack *S,seqstack *B)
   {datatype x;
       if(EMPTY(S,B))
       {
        return 0;
       }
       else 
       {
           x=S->stack[S->top];
           S->top--;
           printf("出栈成功:%d\n",x); 
           return (x);
       }
   }
   
   seqstack *BPUSH(seqstack *B,datatype x)
   {
       if(FULL(S,B))
       {
        return 0;    
       }
       else 
       {printf("请输入数据");
          scanf("%d",&x);
           B->top--;
           B->stack[B->top]=x;
       }
       return B;
   }
   
   int BPOP(seqstack *S,seqstack *B)
   {datatype x;
       if(EMPTY(S,B))
       {
           return 0;
       }
       else
       {
           x=B->stack[B->top];
           B->top++;
           printf("出栈成功:%d\n",x);
           return x;
       }
   }
   int select()
    
    {int b; 
    printf("两个顺序栈共用一个数组的存储空间:\n请选择功能\n");
       printf("1.初始化\t");
       printf("2.栈1进栈\t");
       printf("3.栈1出栈\t");
       printf("4.栈2进栈\t");
       printf("5.栈2出栈\t\n");
    printf("0.退出\t\n");
    scanf("%d",&b);
    return b;    
    }

   
   int main()
   {
    int select();
    int a;
      S=&s;
    B=&b; 
       void INITSTACK(seqstack *S,seqstack *B);
    int EMPTY(seqstack *S,seqstack *B);
    int FULL(seqstack *S,seqstack *B);
    seqstack *SPUSH(seqstack *S,datatype x);
    int SPOP(seqstack *S,seqstack *B);
    seqstack *BPUSH(seqstack *B,datatype x);
    int BPOP(seqstack *S,seqstack *B);
    while(1)
    {    
        a=select();
        switch(a)
       {
     case 0: exit(0);
    case 1:INITSTACK(&s,&b);
    break;
           case 2:
       SPUSH(&s,a);
       break;
       case 3:
       SPOP(&s,&b);
       break;
       case 4:
       BPUSH(&b,a);
       break;
       case 5:
        BPOP(&s,&b);
       }

   }
         return 0;
        
    }