-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.c
78 lines (67 loc) · 1.04 KB
/
25.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
// 25. Implement two Stacks using a Single Array & Check for Overflow & Underflow
#include <stdio.h>
#define MAX_SIZE 1000
int stack[MAX_SIZE];
int top1 = -1;
int top2 = MAX_SIZE;
void pop1();
void pop2();
void push1(int);
void push2(int);
void printStack();
int main() {
push1(10);
push2(1);
push1(34);
push1(54);
push2(3);
push2(5);
push2(9);
pop1();
pop1();
pop2();
}
void printStack() {
for (int i = 0; i <= top1; i++) {
printf("%d ", stack[i]);
}
printf("|");
for (int i = top2; i < MAX_SIZE; i++) {
printf(" %d", stack[i]);
}
printf("\n");
}
void push1(int num) {
if (top1 == top2 - 1) {
printf("ERROR: Stack full!");
return;
}
top1++;
stack[top1] = num;
printStack();
}
void push2(int num) {
if (top2 == top1 + 1) {
printf("ERROR: Stack full!");
return;
}
top2--;
stack[top2] = num;
printStack();
}
void pop1() {
if (top1 == -1) {
printf("ERROR: Stack empty!");
return;
}
top1--;
printStack();
}
void pop2() {
if (top2 == MAX_SIZE) {
printf("ERROR: Stack empty!");
return;
}
top2++;
printStack();
}