-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue Linkedlist ADT.c
218 lines (218 loc) · 3.85 KB
/
Queue 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
//QUEUE Linked list ADT
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct NODE
{
void* dp;
struct NODE* next;
}node;
typedef struct QUEUE
{
node* front;
node* rear;
int count;
}queue;
queue* createqueue();
bool enqueue(queue*,void*);
void* dequeue(queue*);
void* retrivelfront(queue*);
void* retrivelrear(queue*);
bool emptyqueue(queue*);
bool fullqueue(queue*);
int queuecount(queue*);
void displayqueue(queue* ,void (*fp)(void*));
bool destroyqueue(queue*);
queue* createqueue()
{
queue* q;
q=(queue*)malloc(sizeof(queue));
if(q)
{
q->front=NULL;
q->count=0;
q->rear=NULL;
return q;
}
else
{
return NULL;
}
}
bool enqueue(queue* q,void* dpin)
{
node* pnew;
pnew=(node*)malloc(sizeof(node));
if(pnew)
{
pnew->dp=dpin;
if(q->count==0)
q->front=pnew;
else
q->rear->next=pnew;
q->rear=pnew;
q->count++;
return true;
}
else
{
return false;
}
}
void* dequeue(queue* q)
{
node* pnode;
void* ddata;
if(q->count==0)
return NULL;
else
pnode=q->front;
if(q->count==1)
{
q->rear=NULL;
}
q->front=q->front->next;
ddata=pnode->dp;
q->count--;
free(pnode);
return ddata;
}
void* retrivelfront(queue* q)
{
if(emptyqueue(q))
return false;
return(q->front->dp);
}
void* retrivelrear(queue* q)
{
if(emptyqueue(q))
return false;
return(q->rear->dp);
}
bool emptyqueue(queue* q)
{
if(!q->count)
return true;
return false;
}
bool fullqueue(queue* q)
{
node* temp;
temp=(node*)malloc(sizeof(node));
if(temp)
return false;
return true;
}
int queuecount(queue* q)
{
return (q->count);
}
void qdisplay(queue* q,void (*print)(void*))
{
node* temp;
if(!q->count)
return;
for(temp=q->front;temp!=q->rear;temp=temp->next)
print(temp->dp);
print(temp->dp);
}
bool qdestory(queue* q)
{
node* temp;
if(q)
{
while(q->front!=NULL)
{
free(q->front->dp);
temp=q->front;
q->front=temp->next;
free(temp);
}
free(q);
}
return true;
}
void print(void* dp)
{
printf("%f\n",*(float*)dp);
}
int main()
{
queue* q;
float* dp;
bool result;
int ch;
q=createqueue();
if(!q)
{
printf("QUEUE creation is failed\n");
return 0;
}
printf("---------------------Queue created successfully----------------------------\n");
while(1)
{
printf("1-Enqueue\n2-Dequeue\n3-Display\n4-Front element\n5-Rear element\n6-Empty\n7-Full\n8-Count\n9-Exit");
printf("\nEnter the choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
dp=(float*)malloc(sizeof(float));
printf("Enter the floating point number\n");
scanf("%f",dp);
result=enqueue(q,dp);
result==0?printf("Failed\n"):printf("Successfull");
break;
case 2:
dp=dequeue(q);
if(dp)
printf("Deleted elements is: %f",*dp);
else
printf("Queue is Empty\n");
break;
case 3:
if(!emptyqueue(q))
printf("Queue is empty\n");
else
printf("Queue contants are: \n");
qdisplay(q,print);
break;
case 4:
dp=(float*)retrivelfront(q);
if(dp)
printf("Front element is: %f\n",*dp);
else
printf("Queue empty\n");
break;
case 5:
dp=(float*)retrivelrear(q);
if(dp)
printf("Rear element is: %f\n",*dp);
else
printf("Queue empty\n");
break;
case 6:
if(emptyqueue(q))
printf("Queue is empty\n");
else
printf("Queue not empty\n");
break;
case 7:
if(fullqueue(q))
printf("Queue is full\n");
else
printf("Queue not full\n");
break;
case 8:
printf("Elements in queue are:%d\n",queuecount(q));
break;
case 9:
if(qdestory(q))
printf("Queue destoryed\n");
else
printf("Failed\n");
return 0;
default:printf("Invalid choice\n");
}
}
}