-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multiple Stack.c
194 lines (186 loc) · 4.03 KB
/
Multiple Stack.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include<stdio.h>
void menu();
void push(int);
void pop(int);
void display();
int size,n1,n2,top1,top2;
int a[100];
void main()
{
int s1;
printf("Enter the size of the array\n");
scanf("%d",&size);
printf("Enter the size of the first stack\n");
scanf("%d",&s1);
if(s1>=size)
main();
n1=s1;
n2=size-s1;
printf("SIZE of First stack:%d\n",s1);
printf("SIZE of Second stack:%d\n",size-s1);
top1=-1;
top2=n1-1;
menu();
}
void menu()
{
int c;
printf("\n\n\nENTER CHOICE\n");
printf("1.Stack ONE\n2.Stack TWO\n3.DISPLAY\n");
scanf("%d",&c);
switch(c)
{
case 1:
{
printf("------------------------STACK ONE------------------------\n");
printf("ENTER CHOICE\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n");
scanf("%d",&c);
switch(c)
{
case 1:push(1);
break;
case 2:pop(1);
break;
case 3:display();
break;
default:menu();
}
}
break;
case 2:
{
printf("------------------------STACK TWO------------------------\n");
printf("ENTER CHOICE\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n");
scanf("%d",&c);
switch(c)
{
case 1:push(2);
break;
case 2:pop(2);
break;
case 3:display();
break;
default:menu();
}
}
break;
case 3:display();
break;
case 4:menu();
}
}
void push(int st)
{
if(st==1)
{
if(top1==n1-1)
{
printf("OVERFLOW");
menu();
}
else
{
top1++;
printf("Enter value to be inserted\n");
scanf("%d",&a[top1]);
}
}
else
{
if(top2==n1+n2-1)
{
printf("OVERFLOW");
menu();
}
else
{
top2++;
printf("Enter value to be inserted\n");
scanf("%d",&a[top2]);
}
}
menu();
}
void display()
{
int temp;
printf("STACK 1\n");
if(top1==-1)
printf("!!!EMPTY!!!\n");
else
{
temp=top1;
printf("----------------------------------------------------------------\n");
while(temp!=0)
{
printf("%d\t",a[temp]);
temp--;
}
printf("%d\t",a[temp]);
printf("\n----------------------------------------------------------------\n");
}
printf("STACK 2\n");
if(top2==n1-1)
printf("!!!EMPTY!!!\n");
else
{
temp=top2;
printf("----------------------------------------------------------------\n");
while(temp!=0)
{
printf("%d\t",a[temp]);
temp--;
}
printf("%d\t",a[temp]);
printf("\n----------------------------------------------------------------\n");
}
menu();
}
void pop(int st)
{
if(st==1)
{
if(top1==-1)
{
printf("EMPTY!!!\n");
menu();
}
else
{
if(top1==0)
{
printf("POPPED:%d",a[top1]);
top1=-1;
}
else
{
printf("POPPED:%d",a[top1]);
top1--;
}
}
}
else
{
if(top2==n1-1)
{
printf("EMPTY!!!\n");
menu();
}
else
{
if(top2==n1)
{
printf("POPPED:%d",a[top2]);
top2=n1-1;
}
else
{
printf("POPPED:%d",a[top2]);
top2--;
}
}
}
menu();
}