-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multistack.c
147 lines (137 loc) · 2.94 KB
/
Multistack.c
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 Max 10
int topA=-1;
int topB=Max;
int stack[Max];
void pushA(int e)
{
topA++;
stack[topA]=e;
}
void pushB(int e)
{
topB--;
stack[topB]=e;
}
int popA()
{
int e=stack[topA];
topA--;
return e;
}
int popB()
{
int e=stack[topB];
topB++;
return e;
}
void displayA()
{
int i;
if(topA==-1)
{
printf("\n Stack is Empty!!!");
}
else
{
printf("\n StackA : \n");
for(i=topA;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
}
void displayB()
{
int i;
if(topB==Max)
{
printf("\n The Stack is not Empty!!");
}
else
{
printf("\n Stack B : ");
for(i=topB;i<Max;i++)
{
printf("\n %d",stack[i]);
}
}
}
int main()
{
int ch ,val,e;
do
{
printf("\n----------Multi-stack Stack operations-----------");
printf("\n 1. PUSH IN STACK A");
printf("\n 2. PUSH IN STACK B");
printf("\n 3. POP FROM STACK A");
printf("\n 4. POP FROM STACK B");
printf("\n 5. DISPLAY STACK A");
printf("\n 6. DISPLAY STACK B");
printf("\n 7. EXIT");
printf("\n------------------------------------------------");
printf("\n Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(topA==topB-1)
{
printf("\n Stack Overflow!!!");
}
else
{
printf("\n Enter the data in the StackA : ");
scanf("%d",&e);
pushA(e);
}
break;
case 2:
if(topB==topA+1)
{
printf("\n Enter the data to be inserted in the stack : ");
}
else
{
printf("\n Enter the data to be inserted in the Stack B : ");
scanf("%d",&e);
pushB(e);
}
break;
case 3:
if(topA==-1)
{
printf("\n The Stack is Empty!!!");
}
else
{
printf("\n The element pooped out from Stack A is %d",popA());
}
break;
case 4:
if(topB==Max)
{
printf("\n The StackB is Empty!!!");
}
else
{
printf("\n The element that has popped out from stack B is : %d",popB());
}
break;
case 5:
displayA();
break;
case 6:
displayB();
break;
case 7:
displayA();
displayB();
break;
default:
printf("\n Wrong Choice!!!");
}
}while(ch!=7);
}