-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
79 lines (70 loc) · 1.04 KB
/
stack.c
File metadata and controls
79 lines (70 loc) · 1.04 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
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int stack[SIZE];
int top=-1;
void push(int);
int pop(void);
void display(void);
void peek(void);
void main(){
int n;
while(1){
int ele,a;
printf("enter ur choice 1)PUSH\n 2)POP\n 3)PEEK\n 4)Display(traverse)\n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("enter element\n");
scanf("%d",&a);
push(a);
break;
case 2:
ele=pop();
printf("element poped: %d",ele);
break;
case 3: peek();
break;
case 4: display();
break;
default: exit(0);
}
}
}
void push(int ele){
if(top==SIZE-1){
printf("STACK OVERFLOW\n");
}
else{
stack[++top]=ele;
}
}
int pop(){
int ele;
if(top==-1){
printf("STACK UNDERFLOW\n");
return 1;
}
else{
return stack[top--];
}
}
void peek(){
if(top==-1){
printf("STACK EMPTY\n");
}
else{
printf("%d\n",stack[top]);
}
}
void display(){
if(top==-1){
printf("STACK EMPTY\n");
}
else{
for(int i=0;i<=top;i++){
printf("%d\n",stack[i]);
}
}
}