-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkQueue.c
93 lines (85 loc) · 1.76 KB
/
LinkQueue.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
/*
* =====================================================================================
*
* Filename: LinkQueue.c
*
* Description:
*
* Version: 1.0
* Created: 11/30/2014 12:31:51 PM
* Revision: none
* Compiler: gcc
*
* Author: 张世龙 (mn), [email protected]
* Company: free
*
* =====================================================================================
*/
#include "Public.h"
#include "LinkQueue.h"
void InitQueue(LinkQueue *queue)
{
assert(queue);
queue->count = 0;
queue->front = NULL;
queue->rear = NULL;
}
void DestoryQueue(LinkQueue *queue)
{
assert(queue);
QNode *nodePtr = queue->front;
while(nodePtr){
QNode *tmpNodePtr = nodePtr->next;
free(nodePtr);
nodePtr = tmpNodePtr;
}
queue->count = 0;
queue->front = NULL;
queue->rear = NULL;
}
void ClearQueue(LinkQueue *queue);
int QueueEmpty(LinkQueue *queue)
{
assert(queue);
return queue->count == 0;
}
int GetHead(LinkQueue *queue,ElemType *e)
{
assert(queue || e);
if(queue->count == 0)
return ERROR;
*e = queue->front->data;
return SUCCESS;
}
int EnQueue(LinkQueue *queue,ElemType *e)
{
assert(queue || e);
QNode *newNodePtr = (QNode*)malloc(sizeof(QNode));
newNodePtr->next = NULL;
newNodePtr->data = *e;
if(queue->count == 0){
queue->front = queue->rear = newNodePtr;
}else{
queue->rear->next = newNodePtr;
queue->rear = newNodePtr;
}
queue->count++;
return SUCCESS;
}
int DeQueue(LinkQueue *queue,ElemType *e)
{
assert(queue || e);
if(queue->count == 0)
return ERROR;
*e = queue->front->data;
QNode *tmpNodePtr = queue->front->next;
free(queue->front);
queue->front = tmpNodePtr;
queue->count--;
return SUCCESS;
}
int QueueLength(LinkQueue *queue)
{
assert(queue);
return queue->count;
}