-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.stack.c
75 lines (70 loc) · 1.17 KB
/
7.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
/*
Author : abhijithvijayan
Title : Stack Operations
*/
#include <stdio.h>
int top = -1, Ar[20], i, item; //Global Variables
void push(int n);
void view();
void pop();
void main()
{
int n, ch;
printf("Enter the size of Stack:");
scanf("%d", &n);
while (ch != 3)
{
printf("\n----Operation Menu----\n\n1.Push\n2.Pop\n3.Exit ");
printf("\nEnter the choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
push(n);
break;
case 2:
pop(n);
break;
default:
break;
}
}
}
void push(int n)
{
if (top == n - 1)
{
printf("\nOverflow!!!\n");
}
else
{
printf("\nEnter the item:");
scanf("%d", &item);
top++; /* Increment top and then insert */
Ar[top] = item;
printf("\nAfter Pushing\nThe Stack becomes\n\n");
view();
}
}
void pop()
{
if (top <= -1)
{
printf("\nUnderflow!!!\n");
}
else
{
item = Ar[top]; /* For popping */
printf("The element popped is %d", Ar[top]);
top--; /* Decrement top */
printf("\nAfter popping\nThe stack becomes\n\n");
view();
}
}
void view()
{
for (i = top; i >= 0; --i)
{
printf("%d\n", Ar[i]);
}
}