forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_Array.c
More file actions
160 lines (139 loc) · 2.74 KB
/
Stack_Array.c
File metadata and controls
160 lines (139 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*EXPERIMENT : 06
Name : Shrusti Moon
Roll no : UEC2024145
Expt 6 : Develop a program to model an array as stack.
*/
#include<stdio.h>
#define MAX 10
struct stack {
int stk[MAX];
int top;
};
// Display function :
void display (struct stack st) {
if (st.top == -1) {
printf("Stack is Empty\n");
}
else {
printf("Stack elements: ");
for(int i = st.top; i>=0; i--) {
printf("%d ",st.stk[i]);
}
printf("\n");
}
}
int main() {
struct stack st;
st.top = -1;
int choice, num ;
while (choice!=3) {
printf("STACK MENU :\n");
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. EXIT\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1 : // Push
if(st.top == MAX-1) {
printf("Stack is Full !");
}
else {
printf("Enter the element to push: ");
scanf("%d",&num);
st.top ++;
st.stk[st.top] = num ;
printf("\nStack elements : ");
display(st);
}
break;
case 2 : //Pop
if(st.top == -1) {
printf("Stack is Empty !");
}
else {
num = st.stk[st.top];
st.top--;
printf("Popped element : %d",num);
printf("Stack elements : ");
display(st);
}
break;
case 3: //Exit
printf("Exiting program...\n");
break ;
default :
printf("Invalid choice! Try Again.\n") ;
}
}
return 0;
}
/* OUTPUT :
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 2
Stack elements : Stack elements: 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 5
Stack elements : Stack elements: 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 3
Stack elements : Stack elements: 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 6
Stack elements : Stack elements: 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 7
Stack elements : Stack elements: 7 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 4
Stack elements : Stack elements: 4 7 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 1
Enter the element to push: 2
Stack elements : Stack elements: 2 4 7 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 2
Popped element : 2Stack elements : Stack elements: 4 7 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 2
Popped element : 4Stack elements : Stack elements: 7 6 3 5 2
STACK MENU :
1. PUSH
2. POP
3. EXIT
Enter your choice: 3
Exiting program...
*/