-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path队列_链队列_.cpp
123 lines (114 loc) · 1.86 KB
/
队列_链队列_.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
#define ElemType int
#define Status int
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct
{
Queueptr front;
Queueptr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QNode*)malloc(sizeof(QNode));
if(!Q.front)
{
exit(-1);
}
Q.front->next=NULL;
return 0;
}
Status QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
{
return NULL;
}
}
Status QueueLength(LinkQueue Q)
{
int i;
while(Q.front->next!=NULL)
{
i++;
Q.front=Q.front->next;
}
return i;
}
Status GetHead(LinkQueue Q,ElemType &e)
{
e=Q.front->next->data;
return e;
}
Status EnQueue(LinkQueue &Q,ElemType e)
{
Queueptr p;
p=(QNode*)malloc(sizeof(QNode));
p->data=e;
printf("----->%d\n",e);
p->next=NULL;
Q.rear->next=p;
//Q.rear=Q.rear->next;
Q.rear=p;
}
Status DeQueue(LinkQueue &Q,ElemType &e)
{
if(Q.front==Q.rear)
{
return -1;
}
Queueptr p;
p=Q.front->next;
e=p->data;
printf("<-----%d\n",e);
Q.front->next=p->next;
if(Q.rear==p)
{
Q.rear=Q.front;
}
free(p);
}
Status ClearQueue(LinkQueue &Q)
{
if (Q.front == Q.rear)
{
return -1;
}
QNode *node = Q.front->next;//队头元素
while (node)
{
Q.front->next = node->next;//指向新的队头结点
if (Q.rear == node)//当删除的是队尾元素时,将队尾指针指向头结点
{
Q.rear = Q.front;
}
free(node);//释放旧的队头结点
node = Q.front->next;
}
return 0;
}
Status DestoryQueue(LinkQueue &Q)
{
}
int main()
{
LinkQueue Q;
int e;
InitQueue(Q) ;
for(int i=0;i<10;i++)
{
EnQueue(Q,i) ;
}
printf("Queue_head->%d\n",GetHead(Q,e) );
printf("Queue_len->%d\n",QueueLength(Q) );
while(QueueEmpty(Q)!=NULL)
{
DeQueue(Q,e);
}
return 0;
}