-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack
77 lines (77 loc) · 1.9 KB
/
stack
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
#include<stdio.h>
#define MAX 10
int stack[MAX],top=-1;
void push(int data){
if(top==MAX-1){
printf("Stack Overflow");
}
else{
top++;
stack[top]=data;
}
}
void pop(){
int data;
if(top==-1)
printf("Stack Underflow");
else{
data=stack[top];
top--;
printf("Deleted data element %d",data);
}
}
void peek(){
int data;
if(top==-1)
printf("Stack is Empty");
else{
data=stack[top];
printf("Element at the top of stack:%d",data);
}
}
void display(){
int i;
if(top==-1)
printf("Stack is Empty");
else{
printf("The elements are:");
for(i=top;i>=0;i--){
printf("%d ",stack[i]);
}
}
}
int main(){
int option,item;
do{
printf("\n""*****MAIN MENU*****");
printf("\n 1. peek");
printf("\n 2. pop");
printf("\n 3. peek");
printf("\n 4. Display");
printf("\n 5. Exit");
printf("\n Enter your option:");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the item to be pushed:");
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
break;
default:
printf("invalid condition");
break;
}
}while(option!=5);
return 0;
}