-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_operations.c
93 lines (85 loc) · 1.69 KB
/
stack_operations.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
88
89
90
91
92
93
#include<stdio.h>
#define max 100
int top = -1, a[max], i;
void push()
{
int n;
if(top == max - 1)
{
printf("\nStack overflow.");
return;
}
top++;
printf("\nEnter element to push: ");
scanf("%d", &n);
a[top] = n;
}
void pop()
{
if(top == -1)
{
printf("\nStack underflow.");
return;
}
printf("\nDeleted element: %d\n", a[top]);
top--;
}
void peek()
{
if(top == -1)
{
printf("\nStack underflow.");
return;
}
printf("\nTopmost element: %d\n", a[top]);
}
void isempty()
{
if(top == -1)
printf("\nStack is empty.\n");
else
printf("\nStack is not empty.\n");
}
void isfull()
{
if(top == max - 1)
printf("\nStack is full.\n");
else
printf("\nStack is not full.\n");
}
void display()
{
if(top == -1)
{
printf("\nStack underflow.\n");
return;
}
for(i = top; i >= 0; i--)
printf("\n%d", a[i]);
}
void main()
{
int n;
do
{
printf("\n\t\tMENU\n");
printf("\n1. Push\t\t2. Pop\t\t3. Peek\n4. Is Empty\t5. Is Full\t6. Display\n\t\t7.Exit");
printf("\n\nEnter choice: ");
scanf("%d", &n);
switch(n)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: isempty();
break;
case 5: isfull();
break;
case 6: display();
break;
}
}while(n >= 1 && n <= 6);
}