-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
87 lines (86 loc) · 1.74 KB
/
stack.c
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
80
81
82
83
84
85
86
87
#include<stdio.h>
#define CAPACITY 5
void puch(int);
int pop();
int peek();
int stack[CAPACITY] , top = -1;
int main()
{
int choice , ele , num;
while(1)
{
printf("\n1 . Push\n");
printf("2 . Pop\n");
printf("3 . Pop\n");
printf("4 . Traverse\n");
printf("5 . Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter an element : ");
scanf("%d",&ele);
push(ele);
break;
case 2: num = pop();
if (num!=0)
printf("\nPopped element is %d\n\n",num);
else
printf("Stack Underflow\n\n");
break;
case 3:peek();
break;
case 4:traverse();
break;
case 5:exit(1);
default : printf("Invalid Input\n\n");
}
}
}
void push(int ele)
{
if (isFull())
printf("Stack Overflow\ n");
else
stack[++top] = ele;
printf("\n%d is pushed to the stack\n\n",ele);
}
int pop()
{
if(isNull())
return 0;
else
return stack[top--];
}
int peek()
{
if(isNull()){
printf("\nStack Underflow\n\n");
return -1;
}
else
return stack[top];
}
int isFull()
{
if (top == CAPACITY-1)
return 1;
else return 0;
}
int isNull()
{
if (top == -1)
return 1;
else
return 0;
}
void traverse()
{
if(isNull())
printf("\nStack Underflow\n\n");
else{
for(int i=0;i<=top;i++)
printf("%d ",stack[i]);
}
}