Stack
Stack
h>
2. int stack[100],i,j,choice=0,n,top=-1;
3. void push();
4. void pop();
5. void show();
6. void main ()
7. {
8.
10. scanf("%d",&n);
12.
13. printf("\n----------------------------------------------\n");
14. while(choice != 4)
15. {
17. printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
19. scanf("%d",&choice);
20. switch(choice)
21. {
22. case 1:
23. {
24. push();
25. break;
26. }
27. case 2:
28. {
29. pop();
30. break;
31. }
32. case 3:
33. {
34. show();
35. break;
36. }
37. case 4:
38. {
39. printf("Exiting....");
40. break;
41. }
42. default:
43. {
45. }
46. };
47. }
48. }
49.
51. {
53. if (top == n )
55. else
56. {
58. scanf("%d",&val);
61. }
62. }
63.
65. {
67. printf("Underflow");
68. else
70. }
72. {
74. {
75. printf("%d\n",stack[i]);
76. }
78. {
80. }
81. }
82. <stdio.h>
83. int stack[100],choice,n,top,x,i;
84. void push(void);
85. void pop(void);
86. void display(void);
87. int main()
88. {
89. top=-1;
90. printf("\n Enter the size of STACK[MAX=100]:");
91. scanf("%d",&n);
92. printf("\n\t STACK OPERATIONS USING ARRAY");
93. printf("\n\t--------------------------------");
94. printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
4.EXIT");
95. do
96. {
97. printf("\n Enter the Choice:");
98. scanf("%d",&choice);
99. switch(choice)
100. {
101. case 1:
102. {
103. push();
104. break;
105. }
106. case 2:
107. {
108. pop();
109. break;
110. }
111. case 3:
112. {
113. display();
114. break;
115. }
116. case 4:
117. {
118. printf("\n\t EXIT POINT ");
119. break;
120. }
121. default:
122. {
123. printf ("\n\t Please Enter a
Valid Choice(1/2/3/4)");
124. }
125.
126. }
127. }
128. while(choice!=4);
129. return 0;
130. }
131. void push()
132. {
133. if(top>=n-1)
134. {
135. printf("\n\tSTACK is over flow");
136.
137. }
138. else
139. {
140. printf(" Enter a value to be pushed:");
141. scanf("%d",&x);
142. top++;
143. stack[top]=x;
144. }
145. }
146. void pop()
147. {
148. if(top<=-1)
149. {
150. printf("\n\t Stack is under flow");
151. }
152. else
153. {
154. printf("\n\t The popped elements is
%d",stack[top]);
155. top--;
156. }
157. }
158. void display()
159. {
160. if(top>=0)
161. {
162. printf("\n The elements in STACK \n");
163. for(i=top; i>=0; i--)
164. printf("\n%d",stack[i]);
165. printf("\n Press Next Choice");
166. }
167. else
168. {
169. printf("\n The STACK is empty");
170. }
171.
172. }
173. Copy
174. OUTPUT:
175. Enter the size of STACK[MAX=100]:10
176.
177. STACK OPERATIONS USING ARRAY
178. --------------------------------
179. 1.PUSH
180. 2.POP
181. 3.DISPLAY
182. 4.EXIT
183. Enter the Choice:1
184. Enter a value to be pushed:12
185.
186. Enter the Choice:1
187. Enter a value to be pushed:24
188.
189. Enter the Choice:1
190. Enter a value to be pushed:98
191.
192. Enter the Choice:3
193.
194. The elements in STACK
195.
196. 98
197. 24
198. 12
199. Press Next Choice
200. Enter the Choice:2
201.
202. The popped elements is 98
203. Enter the Choice:3
204.
205. The elements in STACK
206.
207. 24
208. 12
209. Press Next Choice
210. Enter the Choice:4
211.
212. EXIT POINT
213.
214. A new version of the above code which is shorter for real time
implementation of stack data structure in C languag