-
Notifications
You must be signed in to change notification settings - Fork 0
/
队列 - 链式队列.c
107 lines (94 loc) · 1.78 KB
/
队列 - 链式队列.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
#include <stdio.h>
#include <stdlib.h>
#define ERROR -2
#define OVERFLOW 2
#define true 1
#define false 0
typedef int ElemType;
typedef struct LinkNode //链式队列结点
{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct //链式队列
{
LinkNode *front ,*rear; //队列头尾指针
}LinkQueue;
//初始化带头结点,可以统一操作,简洁方便
int InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=malloc(sizeof(LinkNode));
Q->front->next=NULL;
return true;
}
//判读队空
int isEmpty(LinkQueue Q)
{
if(Q.front==Q.rear) return true;
else return false;
}
//入队
int EnQueue(LinkQueue *Q,ElemType e)
{
LinkNode *p=malloc(sizeof(LinkNode));
p->data =e;
p->next =NULL;
(Q->rear)->next=p;
Q->rear=p;
return true;
}
//出队
int DeQueue(LinkQueue *Q,ElemType *e)
{
LinkNode *p;
if(Q->front==Q->rear) return false;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
free(p);
return true;
}
//获取队头元素
int GetQT(LinkQueue Q,ElemType *e)
{
LinkNode *p;
if(Q.front==Q.rear) return false;
p =Q.front->next;
*e=p->data;
printf("QT=%d\n",*e);
return true;
}
//获取队列所有元素
int GetALL(LinkQueue Q)
{
LinkNode *s;
int i=0;
LinkNode *p =Q.front->next;
while(p!=Q.rear->next){
printf("Q[%d]=%d\n",i,p->data);
i++;
s=p;
p=s->next;
}
return true;
}
int main()
{
int i=0;
ElemType e,x;
LinkQueue Q;
InitQueue(&Q);
isEmpty(Q);
for(i;i<5;i++){
scanf("%d",&x);
EnQueue(&Q,x);
};
GetQT(Q,&e);
GetALL(Q);
printf("出队两个元素-------\n");
DeQueue(&Q,&e);
DeQueue(&Q,&e);
GetALL(Q);
}