-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementing_Two_Stacks_with_a_Single_Array.c
More file actions
123 lines (112 loc) · 2.74 KB
/
Implementing_Two_Stacks_with_a_Single_Array.c
File metadata and controls
123 lines (112 loc) · 2.74 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>
#define MAX_SIZE 10
int arr[MAX_SIZE];
int top1 = -1;
int top2 = MAX_SIZE;
void push1(int data) {
if (top1 < top2 - 1) {
top1++;
arr[top1] = data;
} else {
printf("Stack 1 Overflow.\n");
}
}
void push2(int data) {
if (top1 < top2 - 1) {
top2--;
arr[top2] = data;
} else {
printf("Stack 2 Overflow.\n");
}
}
int pop1() {
if (top1 >= 0) {
int data = arr[top1];
top1--;
return data;
} else {
printf("Stack 1 Underflow.\n");
return -1;
}
}
int pop2() {
if (top2 < MAX_SIZE) {
int data = arr[top2];
top2++;
return data;
} else {
printf("Stack 2 Underflow.\n");
return -1;
}
}
void display1() {
if (top1 >= 0) {
printf("Stack 1: ");
for (int i = 0; i <= top1; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("Stack 1 is empty.\n");
}
}
void display2() {
if (top2 < MAX_SIZE) {
printf("Stack 2: ");
for (int i = MAX_SIZE - 1; i >= top2; i--) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("Stack 2 is empty.\n");
}
}
int main() {
int choice, data;
while (1) {
printf("1. Push to Stack 1\n");
printf("2. Push to Stack 2\n");
printf("3. Pop from Stack 1\n");
printf("4. Pop from Stack 2\n");
printf("5. Display Stack 1\n");
printf("6. Display Stack 2\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to push to Stack 1: ");
scanf("%d", &data);
push1(data);
break;
case 2:
printf("Enter data to push to Stack 2: ");
scanf("%d", &data);
push2(data);
break;
case 3:
data = pop1();
if (data != -1) {
printf("Popped from Stack 1: %d\n", data);
}
break;
case 4:
data = pop2();
if (data != -1) {
printf("Popped from Stack 2: %d\n", data);
}
break;
case 5:
display1();
break;
case 6:
display2();
break;
case 7:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}