-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
112 lines (103 loc) · 1.88 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#define n 50
void push(int);
void display();
int pop();
int peek();
int isfull();
int isempty();
int top=0,s[n];
int main()
{
int item,o,a;
do
{
printf("\n1)push\n2)pop\n3)dispay\n4)peak\n5)exit");
printf("\nenter the number to perfrom the operation");
scanf("%d",&o);
switch(o)
{
case 1:if(isfull())
{
printf("\nstack is full");
break;
}
else
{
printf("\nenter the element to perfrom push operation");
scanf("%d",&a);
push(a);
break;
}
case 2:if(isempty())
{
printf("\nthe stack is empty");
break;
}
else
{
item = pop();
printf("\nthe element is %d",item);
break;
}
case 3:if(isempty())
{
printf("\nthe stack is empty ");
break;
}
else
{
display();
break;
}
case 4:if(!isempty())
{
item = peek();
printf("\nthe element is %d",item);
break;
}
else
printf("\nthe stack is empty ");
default:printf("\nyou have entered a error operation");
}
}while(o!=5);
}
void push(int a)
{
s[top]=a;
top=top+1;
}
int pop()
{
int a;
top= top-1;
a=s[top];
return a;
}
void display()
{
int i;
printf("\nthe element in the stack are");
for(i=top-1;i>=0;i--)
printf("%d ",s[i]);
}
int peek()
{
int a;
a=s[top-1];
return a;
}
int isfull()
{
if(top==n)
return 1;
else
return 0;
}
int isempty()
{
if(top==0)
return 1;
else
return 0;
}