-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack1.c
More file actions
54 lines (54 loc) · 1.24 KB
/
stack1.c
File metadata and controls
54 lines (54 loc) · 1.24 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
#include<stdio.h>
int stack[50], i, j, n, ch, top = -1;
void push();
void pop();
void display();
int main(){
printf("Enter a number of elements using stack:\n");
scanf("%d", &n);
printf("***STACK USING ARRAY***\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
while(ch!=4){
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("***EXIT***\n");
break;
default:
printf("Enter a valid input!\n");
}
}
}
void push(){
int val;
if(top == n-1)
printf("STACK IS IN OVERFLOW\n");
else{
printf("Enter the value to be pushed:");
scanf("%d", &val);
top++;
stack[top] = val;
}
}
void pop(){
if(top == -1)
printf("STACK IS IN UNDERFLOW\n");
else
top--;
}
void display(){
for(i = top; i >= 0; i--)
printf("%d\n", stack[i]);
if(top == -1)
printf("STACK IS EMPTY.\n");
}