-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPush_pop_in_stack.c
More file actions
64 lines (53 loc) · 1.08 KB
/
Copy pathPush_pop_in_stack.c
File metadata and controls
64 lines (53 loc) · 1.08 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
// stack implementation using array
#include<stdio.h>
#define MAX 6
int top = -1;
int stack[MAX];
void push(int item)
{
int i;
if (top == (MAX - 1))
printf("Stack Overflow\n");
else
{
top += 1;
stack[top] = item;
printf("\nAfter push %d in stack the stack is\n", item);
for (i = 0; i <= top; i++)
{
printf("%d\n", stack[i]);
}
}
}
int pop()
{
int t, i;
if (top == -1)
printf("Stack is empty\n");
else
{
t = stack[top];
top -= 1;
printf("\nAfter pop %d from stack the stack is\n", t);
for (i = 0; i <= top; i++)
{
printf("%d\n", stack[i]);
}
}
return t;
}
int main()
{
int result, i;
printf("Enter elements in stack\n");
for (i = 0; i < MAX; i++)
{
scanf("%d", &stack[i]);
push(stack[i]);
}
result = pop();
printf("\nThe First popped element is: %d\n\n", result);
result = pop();
printf("\nThe Second popped element is: %d\n", result);
return 0;
}