-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.c
69 lines (60 loc) · 1.2 KB
/
Queue.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
//
// Created by charl on 2019-05-12.
//
#include "Queue.h"
LinkQueue* createQueue()
{
LinkQueue* queue = (LinkQueue*) malloc (sizeof(LinkQueue));
queue->front=NULL;
queue->rear=NULL;
queue->count = 0;
return queue;
}
void EnQueue(LinkQueue* queue, void* data)
{
ListNode* s = (ListNode*) malloc (sizeof(ListNode));
s->data = data;
s->next = NULL;
if(isQueueEmpty(queue))
{
queue->front=s;
queue->rear=s;
queue->count = 1;
}
else
{
queue->rear->next = s;
queue->rear = s;
queue->count++;
}
}
void* DeQueue(LinkQueue* queue)
{
ListNode* p;
p = queue->front;
void* output = p->data;
queue->front = p->next;
queue->count--;
if(queue->front == NULL)
queue->rear = NULL;
free(p);
return output;
}
int isQueueEmpty(LinkQueue* queue)
{
return ((queue->rear == NULL) && (queue->front == NULL));
}
void DestroyQueue(LinkQueue* queue)
{
while(!isQueueEmpty(queue))
{
ListNode* p;
p = queue->front;
queue->front = p->next;
if(queue->front == NULL)
queue->rear = NULL;
free(p);
printf("yes\n");
}
free(queue);
}