-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack Array ADT.c
235 lines (235 loc) · 4.1 KB
/
Stack Array ADT.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//Array stack ADT of student information
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct array_stack
{
void** st;
int top;
int count;
int size;
}AST;
AST* creatstack()
{
int n;
AST* h=(AST*)malloc(sizeof(AST));
if(h)
{
printf("Enter the size of an array:\n");
scanf("%d",&n);
h->st=(void**)calloc(n,sizeof(void*));
if(h->st)
{
h->top=-1;
h->count=0;
h->size=n;
}
else
{
free(h);
return(NULL);
}
}
return(h);
}
bool pushstack(AST* h,void* dpin)
{
if(h->top==h->size-1)
{
return(false);
}
else
{
h->top++;
h->st[h->top]=dpin;
h->count++;
return(true);
}
}
bool stackempty(AST* h)
{
if(h->count==0)
return (true);
else
return (false);
}
void* popstack(AST* h)
{
void* dpout;
if(stackempty(h))
{
return (NULL);
}
else
{
dpout=h->st[h->top];
h->top--;
h->count--;
return (dpout);
}
}
void stackdisplay(AST* h,void (*pd)(void*))
{
int i;
if(stackempty(h))
{
printf("Stack is empty\n");
return;
}
else
{
for(i=h->top;i>=0;i--)
{
pd(h->st[i]);
}
}
}
bool stackfull(AST* h)
{
if(h->top==h->size-1)
return (true);
else
return false;
}
void* stacktop(AST* h)
{
if(stackempty(h))
return NULL;
else
return (h->st[h->top]);
}
int stackcount(AST* h)
{
return(h->count);
}
bool destory(AST* h)
{
int i;
if(h)
{
for(i=h->top;i>=0;i--)
{
free(h->st[i]);
(h->count)--;
}
free(h);
}
return(true);
}
typedef struct student
{
int sem,roll_no;
char name[15],usn[11],branch[6];
}stud;
void printdata(void* s)
{
stud* t=(stud*)s;
if(s)
{
printf("Name: %s\n",t->name);
printf("Branch: %s\n",t->branch);
printf("USN: %s\n",t->usn);
printf("Sem: %d\n",t->sem);
printf("Roll_no: %d\n\n",t->roll_no);
}
}
int main()
{
stud* s;
AST* ph;
int c;
bool result;
ph=creatstack();
printf("Stack of student using array stack ADT\n");
printf("1-PUSH\n2-POP\n3-DISPLAY\n4-EMPTY STACK\n5-STACK FULL\n6-STACK TOP\n7-STACK COUNT\n8-EXIT\n");
while(1)
{
printf("\n\nEnter the choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
s=(stud*)malloc(sizeof(stud));
if(s)
{
printf("Enter the details of student\n");
printf("Enter the Name:\n");
scanf("%s",s->name);
printf("Enter the Branch:\n");
scanf("%s",s->branch);
printf("Enter the USN:\n");
scanf("%s",s->usn);
printf("Enter the SEM:\n");
scanf("%d",&s->sem);
printf("Enter the Roll_no:\n");
scanf("%d",&s->roll_no);
if(pushstack(ph,s))
{
printf("Student Data inserted Successfully\n");
}
else
{
printf("Failed");
free(s);
}
}
break;
case 2:
s=(stud*)popstack(ph);
if(s)
{
printdata(s);
free(s);
}
else
{
printf("Pop opertion is failed\n");
}
break;
case 3:
if(stackempty(ph))
printf("No student information is there to display\n");
else
printf("Student details are as follows:\n");
stackdisplay(ph,printdata);
break;
case 4:
if(stackempty(ph))
printf("Stack is empty\n");
else
printf("Stack is not empty\n");
break;
case 5:
if(stackfull(ph))
printf("Stack is full\n");
else
printf("Stack is not full\n");
break;
case 6:
s=(stud*)stacktop(ph);
if(s)
{
printf("Top element in the Stack is:\n");
printdata(s);
}
else
{
printf("There is no student information present in Stack\n");
}
break;
case 7:
printf("Number of Student information in stack are: %d",stackcount(ph));
break;
case 8:
result=destory(ph);
if (result)
printf("Stack destroyed successfully\n");
else
printf("Failed to destroy stack\n");
return 0;
break;
default:
printf("Entered choice is invalid\n");
}
}
}