-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeQueue.c
69 lines (49 loc) · 996 Bytes
/
NodeQueue.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
#include"NodeQueue.h"
#include<malloc.h>
#include<stdlib.h>
NodeQueue * InitQueue(){
NodeQueue * queue=(NodeQueue *)malloc(sizeof(NodeQueue));
queue->front=queue->rear=(QNode *)malloc(sizeof(QNode));
queue->front->next=NULL;
return queue;
}
boolean DestroyQueue(NodeQueue * queue){
while(queue->front){
queue->rear=queue->front->next;
free(queue->front);
queue->front=queue->rear;
}
return 1;
}
boolean EnQueue(NodeQueue * queue,Node * node){
QNode * p=(QNode *)malloc(sizeof(QNode));
if(!p){
return 0;
}
p->data=node;
p->next=NULL;
queue->rear->next=p;
queue->rear=p;
return 1;
}
Node * DeQueue(NodeQueue * queue){
Node * node;
QNode * p=queue->front->next;
if(queue->front==queue->rear){
return NULL;
}
node=p->data;
queue->front->next=p->next;
if(queue->rear==p){
queue->rear=queue->front;
}
free(p);
return node;
}
boolean IsQueueEmpty(NodeQueue * queue){
if(queue->front==queue->rear){
return 0;
}else{
return 1;
}
}