Repository navigation
Expand file tree
/
Copy pathstack.c
More file actions
67 lines (61 loc) · 1.44 KB
/
Copy pathstack.c
File metadata and controls
67 lines (61 loc) · 1.44 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
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int item) {
if (top == MAX - 1) {
printf("Stack Overflowed !!\n");
return;
}
top++;
stack[top] = item;
}
void pop() {
if (top == -1) {
printf("Stack Underflow !!\n");
return;
}
int popped_item = stack[top];
top--;
printf("Popped item: %d\n", popped_item);
}
void display() {
if (top == -1) {
printf("Stack is empty!\n");
return;
}
printf("The elements in the stack are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\t", stack[i]);
}
printf("\n");
}
int main(void) {
int choice, item;
printf("\n\n----------STACK DATA STRUCTURE-----------\n\n");
while(1) {
printf("1. PUSH 2. POP 3. DISPLAY 4. EXIT\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter element to be inserted: ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("\nThank you for using the stack\n");
exit(0);
default:
printf("Invalid Input !!\nPlease enter a valid option.\n");
}
}
return 0;
}