-
Notifications
You must be signed in to change notification settings - Fork 1
/
Demonstration of Stack – using Array.c
143 lines (121 loc) · 2.38 KB
/
Demonstration of 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct stackk {
int top;
unsigned size;
int* array;
};
struct stackk* create(unsigned size)
{
struct stackk* stackk = (struct stackk*)malloc(sizeof(struct stackk));
stackk->size = size;
stackk->top = -1;
stackk->array = (int*)malloc(stackk->size * sizeof(int));
return stackk;
}
int isFull(struct stackk* stackk)
{
return stackk->top == stackk->size - 1;
}
int isEmpty(struct stackk* stackk)
{
return stackk->top == -1;
}
void push(struct stackk* stackk, int item)
{
if (isFull(stackk))
return;
stackk->array[++stackk->top] = item;
}
int pop(struct stackk* stackk)
{
if (isEmpty(stackk))
return -1;
return stackk->array[stackk->top--];
}
int peek(struct stackk* stackk)
{
if (isEmpty(stackk))
return INT_MIN;
return stackk->array[stackk->top];
}
int main()
{
int val,n;
struct stackk* stackk = create(100);
do
{printf("\n************************* MENU ************************");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEK");
printf("\n4 IS EMPTY");
printf("\n5.EXIT");
printf("\n enter ur choice : ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nenter the value ");
scanf("%d",&val);
push(stackk , val);
break;
case 2:
printf("\n popped element : %d",pop(stackk));
break;
case 3:
printf("\n top element : %d",peek(stackk));
break;
case 4: printf("\n is empty : %d",isEmpty(stackk));
break;
case 5: exit(0);
break;
default: printf("\n Wrong Choice!");
break;
}
printf("\n do u want to cont... ");
}while('y'==getch());
}
// Output:
// ************************* MENU ************************
// 1.PUSH
// 2.POP
// 3.PEEK
// 4 IS EMPTY
// 5.EXIT
// enter ur choice :
// 1
// enter the value
// 45
// do u want to cont...
// ************************* MENU ************************
// 1.PUSH
// 2.POP
// 3.PEEK
// 4 IS EMPTY
// 5.EXIT
// enter ur choice :
// 1
// enter the value
// 56
// do u want to cont...
// ************************* MENU ************************
// 1.PUSH
// 2.POP
// 3.PEEK
// 4 IS EMPTY
// 5.EXIT
// enter ur choice :
// 3
// top element : 56
// do u want to cont...
// ************************* MENU ************************
// 1.PUSH
// 2.POP
// 3.PEEK
// 4 IS EMPTY
// 5.EXIT
// enter ur choice :
// 4
// is empty : 0
// do u want to cont...