-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
79 lines (75 loc) · 1.27 KB
/
stack.c
File metadata and controls
79 lines (75 loc) · 1.27 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>
#define SIZE 5
int stack[SIZE];
int top=-1;
int el;
int ch;
void push(int);
void pop();
int isEmpty();
int isFull();
void traverse();
void peek();
void main(){
while(1){
printf("1.Push");
printf("1.Pop");
printf("3.Peek");
printf("4.Traverse");
int ch;
printf("enter choice: ");
scanf("%d",&ch);
switch(ch){
case '1':printf("Enter element:");
scanf("%d",&el);
push(el);
case'2':pop();
case'3':peek();
case'4':traverse();
case'5':exit(0);
default:printf("Invalid operation");
}}}
void push(int el){
if(isFull){
printf("Stack is full");
}
else{
top++;
stack[top]=el;
}
}
int isFull(){
if(top==SIZE-1){
return 1;
}
else{
return 0;
}
}
void pop(){
if(isEmpty){
printf("Stack is empty");
}
else{
int popped=stack[top];
printf("returning %d from stack",popped);
top--;
}
}
int isEmpty(){
if(top==-1){
return 1;
}
else{
return 0;
}
}
void peek(){
printf("%d is returned",stack[top]);
}
void traverse(){
for(int i=0;i<top-1;i++){
int t=stack[i];
printf("%d\t",t);
}
}