-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackarray.c
96 lines (91 loc) · 1.85 KB
/
stackarray.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
#include <stdio.h>
#include<conio.h>
#define MAX 5
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*FUNCTION TO DISPLAY STACK*/
void display (int stack[])
{ int i;
printf ("\nThe Stack is: ");
if (top == -1)
printf ("empty");
else
{ for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",stack[i]);
}
printf ("\n");
}
/*MAIN PROGRAM*/
int main()
{
int stack [MAX], item;
int ch;
clrscr ();
top = -1;
do
{ do
{ printf ("\nAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
}while (ch<1 || ch>3);
switch (ch)
{
case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
{ printf ("\nAfter Pushing ");
display (stack);
if (top == (MAX-1))
printf ("\nThe Stack is Full");
}
else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{ printf ("\nThe Popped item is %d. After Popping: ",item);
display (stack);
}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
return 0;
}