-
Notifications
You must be signed in to change notification settings - Fork 36
/
implement_stack_using_array.c
102 lines (89 loc) · 1.79 KB
/
implement_stack_using_array.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
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int STACK[MAX],TOP;
/* display stack element*/
void display(int []);
/* push (insert) item into stack*/
void PUSH(int [],int);
/* pop (remove) item from stack*/
void POP (int []);
void main()
{
int ITEM=0;
int choice=0;
TOP=-1;
while(1)
{
/*clrscr();*/
printf("Enter Choice (1: display, 2: insert (PUSH), 3: remove(POP)), 4: Exit..:");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(STACK);
break;
case 2:
printf("Enter Item to be insert :");
scanf("%d",&ITEM);
PUSH(STACK,ITEM);
break;
case 3:
POP(STACK);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice.");
break;
}
getch();
}// end of while(1)
}
/* function : display(),
to display stack elements.
*/
void display(int stack[])
{
int i=0;
if(TOP==-1)
{
printf("Stack is Empty .\n");
return;
}
printf("%d <-- TOP ",stack[TOP]);
for(i=TOP-1;i >=0;i--)
{
printf("\n%d",stack[i]);
}
printf("\n\n");
}
/* function : PUSH(),
to push an item into stack.
*/
void PUSH(int stack[],int item)
{
if(TOP==MAX-1)
{
printf("\nSTACK is FULL CAN't ADD ITEM\n");
return;
}
TOP++;
stack[TOP]=item;
}
/* function : POP(),
to pop an item from stack.
*/
void POP(int stack[])
{
int deletedItem;
if(TOP==-1)
{
printf("STACK is EMPTY.\n");
return;
}
deletedItem=stack[TOP];
TOP--;
printf("%d deleted successfully\n",deletedItem);
return;
}