Repository navigation
Expand file tree
/
Copy pathstack.c
More file actions
147 lines (137 loc) · 2.39 KB
/
Copy pathstack.c
File metadata and controls
147 lines (137 loc) · 2.39 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#define N 5
int stack[N],x;
int top = -1;
void push();
void pop();
void isEmpty();
void size();
void topElement();
void display();
void printline();
void main()
{
int a;
do
{
printf("\n 1.Push 2.Pop 3. Is Empty 4. Size 5. Top Element 6. Display 0. Stop : ");
scanf("%d", &a);
printline();
switch(a)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
isEmpty();
break;
case 4:
size();
break;
case 5:
topElement();
break;
case 6:
display();
break;
case 0:
printf("\nExit condition\n");
break ;
default:
printf("\nInvalid choice\n");
}
}
while(a!=0);
}
void push()
{
if(top>=N-1)
{
printf("\nOverflow condition\n\n");
}
else
{
printf("\n\nEnter the element:");
scanf("%d", &x);
top++;
stack[top]=x;
}
printline();
}
void pop()
{
int y;
if(top<=-1)
{
printf("\n\nUnderflow condition\n\n");
}
else
{
y = stack[top];
top--;
printf("\nPopped item is %d", y);
}
printline();
}
void isEmpty()
{
if(top<=-1)
{
printf("\nStack is empty\n");
}
else
{
printf("\nStack has some elements\n");
}
printline();
}
void size()
{
if(top<=-1)
{
printf("\n Underflow condition\n");
}
else
{
printf("\nThe size of the stack is: %d\n", top+1);
}
printline();
}
void topElement()
{
if(top==-1)
{
printf("\nStack is empty\n");
}
else{
int item;
item = stack[top];
printf("\nTop element is %d\n", item);
}
printline();
}
void display()
{
int i;
if(top==-1)
{
printf("\n\n Stack is empty\n");
}
else
{
printf("\nElements of the stack are:");
for(i=top; i>=0; i--)
{
printf("%d ", stack[i]);
}
}
printf("\n");
printline();
}
void printline()
{
printf("\n--------------------------------------------------------------\n");
}