-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
91 lines (80 loc) · 1.76 KB
/
stack.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
#include <stdio.h>
#define MAX 10
int data[MAX], top;
void initialize();
int empty();
int full();
void push();
void pop();
void print();
int main() {
int ch;
initialize();
do {
printf("\n ***STACK OPERATIONS");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. Print");
printf("\n 4. EXIT");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
if (full() == 0) {
push();
} else {
printf("\nStack is Full!!!");
}
break;
case 2:
if (empty() == 0) {
pop();
} else {
printf("\nStack is Empty!!!");
}
break;
case 3:
print();
break;
case 4:
break;
default:
printf("Invalid choice");
}
} while (ch != 4);
return 0;
}
void initialize() {
top = -1;
}
int full() {
return (top == MAX - 1);
}
int empty() {
return (top == -1);
}
void push() {
int x;
printf("Enter data to be inserted: ");
scanf("%d", &x);
top = top + 1;
data[top] = x;
}
void pop() {
int x;
x = data[top];
top = top - 1;
printf("\nData popped = %d", x);
}
void print() {
int i;
if (empty()) {
printf("\nStack is Empty!!!");
} else {
printf("\nStack data: ");
for (i = top; i >= 0; i--) {
printf("%d\t", data[i]);
}
printf("\n"); // Added newline for better formatting
}
}