-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_pop2.c
More file actions
53 lines (50 loc) · 776 Bytes
/
push_pop2.c
File metadata and controls
53 lines (50 loc) · 776 Bytes
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
// read size of the stack
// pop the last element of a stack and display the stack as well as print the index of top element
// after pop if stack is empty display as "underflow"
#include <stdio.h>
#define max 100
int stack[max];
int top = -1;
void push(int x)
{
top++;
stack[top] = x;
}
void peekindex()
{
printf("%d\n", top);
}
void pop()
{
top--;
if (top == -1)
{
printf("underflow\n");
}
}
void traverse()
{
for (int i = 0; i <= top; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
}
int main()
{
int x;
scanf("%d", &x);
while (x--)
{
int t;
scanf("%d", &t);
push(t);
}
pop();
if (top != -1)
{
traverse();
}
peekindex();
return 0;
}