-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.c
More file actions
63 lines (57 loc) · 1.03 KB
/
queue.c
File metadata and controls
63 lines (57 loc) · 1.03 KB
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
//the coding is UTF-8
#include "queue.h"
//初始化队列
int InitQueue(LinkQueue *Q)
{
Q->front = Q->rear = (QNode *)malloc(sizeof(QNode));
Q->front->next = NULL;
return 1;
}
//入队
int push_back(LinkQueue *Q, ElemType e)
{
QNode *p = (QNode *)malloc(sizeof(QNode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return 1;
}
//出队
int pop_front(LinkQueue *Q)
{
QNode *p;
if (Q->front == Q->rear)
return 0;
p = Q->front->next;
Q->front->next = p->next;
if (Q->rear == p)
Q->rear = Q->front;
free(p);
return 1;
}
//尾出队
int pop_back(LinkQueue *Q)
{
QNode *p;
if (Q->front == Q->rear)
return 0;
p = Q->front;
while (p->next != Q->rear)
p = p->next;
p->next = NULL;
free(Q->rear);
Q->rear = p;
return 1;
}
//队头
ElemType getfront(LinkQueue *Q)
{
if (Q->front != Q->rear)
return Q->front->next->data;
}
//判队空
int QueueEmpty(LinkQueue *Q)
{
return Q->front == Q->rear;
}