-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20_multiple_stack_using_array.c
109 lines (107 loc) · 1.89 KB
/
20_multiple_stack_using_array.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
//Govind J Nair
//S3-D
//23
#include <stdio.h>
#define size 100
int stack[size], begTop=-1, endTop=size;
int begPush(int data) {
if (begTop == endTop-1) {
printf("Stack overflow\n");
return 0;
} else {
stack[++begTop] = data;
return 1;
}
}
int begPop() {
if (begTop == -1) {
printf("Stack underflow\n");
return 0;
} else {
printf("Popped element is %d\n", stack[begTop]);
begTop--;
return 1;
}
}
int begDisplay() {
int i;
printf("\n\nStack contents\n");
if (begTop == -1) {
printf("Empty stack\n");
} else {
for (i=begTop; i>=0; i--) {
printf("%d\n", stack[i]);
}
}
printf("\n");
}
int endPush(int data) {
if (begTop == endTop-1) {
printf("Stack overflow\n");
return 0;
} else {
stack[--endTop] = data;
return 1;
}
}
int endPop() {
if (endTop == size) {
printf("Stack underflow\n");
return 0;
} else {
printf("Popped element is %d\n", stack[endTop]);
endTop++;
return 1;
}
}
int endDisplay() {
int i;
printf("\n\nStack contents\n");
if (endTop == size) {
printf("Empty stack\n");
} else {
for (i=endTop; i<size; i++) {
printf("%d\n", stack[i]);
}
}
printf("\n");
}
int main() {
char ans;
int data;
do {
printf("\n\n\tTwo Stack Using array\n");
printf("\tBeg Stack\n1. Beg Push\n2. Beg Pop\n3. Beg Display\n");
printf("\tEnd Stack\n4. End Push\n5. End Pop\n6. End Display\n");
printf("7. Exit\nEnter your choice : ");
scanf(" %c", &ans);
switch (ans) {
case '1':
printf("Enter data to be pushed : ");
scanf(" %d", &data);
begPush(data);
break;
case '2':
begPop();
break;
case '3':
begDisplay();
break;
case '4':
printf("Enter data to be pushed : ");
scanf(" %d", &data);
endPush(data);
break;
case '5':
endPop();
break;
case '6':
endDisplay();
break;
case '7':
break;
default:
printf("Invalid choice.\n");
}
} while (ans != '7');
}