-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmultistack.c
122 lines (114 loc) · 2.66 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
#include <stdio.h>
#define MAX 20
int Dstack[MAX]; // Single array for two stacks
int top1 = -1, top2 = MAX; // For 2 stacks
// Push function to insert element
void push(int stackNumber, int el)
{
if (stackNumber == 1) // For stack 1
{
if (top1 == top2 - 1) // Stack full condition
{
printf("\nStack 1 full");
}
else
{
Dstack[++top1] = el;
}
}
else if(stackNumber == 2) // For stack 2
{
if (top2 == top1 + 1) // Stack full condition
{
printf("\nStack 2 full");
}
else
{
Dstack[--top2] = el;
}
}
}
// Pop function to pop an element
int pop(int stackNumber)
{
if (stackNumber == 1) // For stack 1
{
if (top1 == -1) // Stack empty condition
{
printf("\nStack 1 empty");
return -1;
}
return Dstack[top1--];
}
else if(stackNumber == 2) // For stack 2
{
if (top2 == MAX) // Stack empty condition
{
printf("\nStack 2 empty");
return -1;
}
return Dstack[top2++];
}
}
// To display elements of stack
void display(int stackNumber)
{
if (stackNumber == 1) // For stack 1
{
if (top1 == -1)
{
printf("\nStack 1 empty");
return;
}
printf("\nElements of stack 1\n");
for (int i = 0; i <= top1; i++)
printf("%d ", Dstack[i]);
}
else if(stackNumber == 2) // For stack 2
{
if (top2 == MAX)
{
printf("\nStack 2 empty");
return;
}
printf("\nElements of stack 2\n");
for (int i = MAX - 1; i >= top2; i--)
printf("%d ", Dstack[i]);
}
}
int main()
{
int sNum, ch, element, popped;
printf("\nEnter stack number (1 or 2) ");
scanf("%d", &sNum);
do
{
printf("\n\nChoose operation");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Change stack number");
printf("\nPress anyother key to EXIT\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter element to insert ");
scanf("%d", &element);
push(sNum, element);
break;
case 2:
popped = pop(sNum);
printf("\nPopped element %d ", popped);
break;
case 3:
display(sNum);
break;
case 4:
printf("\nEnter stack number (1 or 2)");
scanf("%d", &sNum);
break;
}
} while (ch < 5);
return 0;
}