-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2Stacks_1Array.cpp
More file actions
127 lines (92 loc) · 1.72 KB
/
2Stacks_1Array.cpp
File metadata and controls
127 lines (92 loc) · 1.72 KB
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
#include <stdio.h>
#include <stdlib.h>
#define max 1000
int stack[max],top1=-1,top2=max;
void display1();
void display2();
void pop1();
void push1(int data);
void pop2();
void push2(int data);
int main()
{
char c;int data;
while(1)
{
printf("What do you want to do? \n a.Push(stack 1) \n b.Push(stack 2) \n c.Pop(stack 1) \n d.Pop(stack 2)\n e.Peep(stack 1)\n f.Peep(stack 2)\n g.Exit \n");
scanf(" %c",&c);
switch(c)
{
case 'a' : if(top1==top2-1)
printf("Overflow\n");
else
{
printf("Enter the element to be pushed \n");
scanf(" %d",&data);
push1(data);
}
break;
case 'b' : if(top1==top2-1)
printf("Overflow\n");
else
{
printf("Enter the element to be pushed \n");
scanf(" %d",&data);
push2(data);
}
break;
case 'c' : pop1();
break;
case 'd' : pop2();
break;
case 'e' : display1();break;
case 'f' : display2();break;
case 'g' : exit(0);
default : printf("Invalid Input. Please try again \n");
}
}
}
void push1(int data)
{
stack[++top1]=data;
}
void push2(int data)
{
stack[--top2]=data;
}
void pop1()
{
if(top1<0)
printf("The selected stack is empty.\n");
else
printf("The element popped is : %d \n",stack[top1--]);
}
void pop2()
{
if(top2>max-1)
printf("The selected stack is empty. \n");
else
printf("The element popped is : %d \n",stack[top2++]);
}
void display1()
{
int i=0;
if(top1<0)
{
printf("The selected stack is empty.\n");
return;
}
while(i<=top1)
printf("%d \n",stack[i++]);
}
void display2()
{
int i=max-1;
if(top2>max-1)
{
printf("The selected stack is empty. \n");
return;
}
while(i>=top2)
printf("%d \n",stack[i--]);
}