-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack Linkedlist ADT.c
221 lines (221 loc) · 3.81 KB
/
Stack Linkedlist 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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct node
{
void* dp;
struct node* next;
}NODE;
typedef struct stack
{
int count;
NODE* top;
}STACK;
STACK* creatstack();
bool pushstack(STACK*,void*);
void* popstack(STACK*);
void stackdisplay(STACK*,void(*pd)(void*));
void* stacktop(STACK*);
bool emptystack(STACK*);
bool stackfull();
int stackcount(STACK*);
bool destorystack(STACK*);
STACK* creatstack()
{
STACK* s;
s=(STACK*)malloc(sizeof(STACK*));
if(s)
{
s->top=NULL;
s->count=0;
}
return s;
}
bool pushstack(STACK* head,void* dpin)
{
NODE* pnew;
pnew=(NODE*)malloc(sizeof(NODE));
if(pnew)
{
pnew->dp=dpin;
pnew->next=head->top;
head->top=pnew;
head->count++;
return(true);
}
else
{
return(false);
}
}
void* popstack(STACK* head)
{
NODE* pout;
void* dpout;
{
if(!head->count)
{
return(NULL);
}
else
{
pout=head->top;
head->top=pout->next;
head->count--;
dpout=pout->dp;
free(pout);
return(dpout);
}
}
}
void stackdisplay(STACK* head,void (*pd)(void*))
{
NODE* temp;
for(temp=head->top;temp!=0;temp=temp->next)
pd(temp->dp);
}
void* stacktop(STACK* head)
{
if(!head->count)
{
return NULL;
}
else
{
return(head->top->dp);
}
}
bool emptystack(STACK* head)
{
if(!head->count)
return(true);
else
return(false);
}
bool stackfull()
{
NODE* temp;
temp=(NODE*)malloc(sizeof(NODE));
if(!temp)
{
return(true);
}
else
{
free(temp);
return(false);
}
}
int stackcount(STACK* head)
{
return(head->count);
}
bool destorystack(STACK* head)
{
NODE* temp;
if(head)
{
while(head->top!=NULL)
{
free(head->top->dp);
temp=head->top;
head->top=head->top->next;
free(temp);
}
free(head);
}
return (true);
}
void pd(void* dp)
{
printf("%d\n",*(int*)dp);
}
int main()
{
STACK* head;
int* dp,c;
bool result;
head=creatstack();
if(!head)
{
printf("Stack creation is Failed\n");
return(1);
}
printf("Stack created successfully\n");
while(1)
{
printf("1-PUSH\n2-POP\n3-DISPLAY\n4-STACK TOP\n5-EMPTY STACK\n6-STACK FULL\n7-STACK COUNT\n8-EXIT\n");
printf("\nEnter the choice\n");
scanf("%d",&c);
switch(c)
{
case 1:
dp=(int*)malloc(sizeof(int));
printf("Enter the element to be pushed\n");
scanf("%d",dp);
result=pushstack(head,dp);
result==0?printf("Failed\n"):printf("Successful\n");
break;
case 2:
dp=(int*)popstack(head);
if(dp)
{
printf("Popped elemets is %d\n",(*dp));
free(dp);
}
else
{
printf("Stack is empty we can't delete the element\n");
}
break;
case 3:
if(emptystack(head))
{
printf("stack has no elements to display:\n");
}
else
{
printf("stack contants are\n");
stackdisplay(head,pd);
}
break;
case 4:
dp=(int*)stacktop(head);
if(dp)
{
printf("Top element in stack is %d\n",*dp);
}
else
{
printf("Stack is Empty there is no top element to display\n");
}
break;
case 5:
if(emptystack(head))
printf("Stack is empty\n");
else
printf("Stack is not empty\n");
break;
case 6:
if(stackfull()) //we are allocating dynamical so it is rear to be full
{
printf("Stack is Full\n");
}
else
{
printf("Stack is not Full\n");
}
break;
case 7:
printf("Elements in stack are= %d\n",stackcount(head));
break;
case 8:
if(destorystack(head))
printf("stack is destoryed\n");
else
printf("stack does not exist\n");
return(1);
default:printf("Invalid choice\n");
}
}
}