教材:严版数据结构

代码如下:

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
QueuePtr front;//队头指针
QueuePtr rear;//队尾指针
}LinkQueue;

Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front)
exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}

Status DestroyQueue(LinkQueue &Q)//销毁队列
{
while (Q.front) {
Q.rear = Q.front->next;
free(Q.front);
Q.front = Q.rear;
}
return OK;
}

Status CLearQueue(LinkQueue &Q)//清空队列
{
QueuePtr p, q;
if (!Q.front)
exit(OVERFLOW);
p = Q.front->next;
Q.front->next = NULL;
while (p)
{
q = p;
p = p->next;
free(q);
}
Q.rear = Q.front;
return OK;
}

Status QueueEmpty(LinkQueue Q)
{
if (Q.rear == Q.front)
return TRUE;
else
return FALSE;
}

int QueueLength(LinkQueue Q)
{
QueuePtr p;
int n = 0;
p = Q.front->next;
while (p)
{
n++;
p = p->next;
}
return n;
}

Status GetHead(LinkQueue Q, QElemType &e)
{
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
return OK;
}

Status EnQueue(LinkQueue &Q, QElemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p)
exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}

Status DeQueue(LinkQueue &Q, QElemType &e)
{
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
free(p);
return OK;
}

Status QueueTraverse(LinkQueue &Q)
{
QueuePtr p = Q.front->next;
if (p == NULL)
return ERROR;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}

void main()
{
int i, n;
QElemType k, h, a, f;
LinkQueue Q;
printf("创建一个空队列!\n");
InitQueue(Q);
printf("判断队列是否为空!\n");
printf("QueueEmpty(Q)=%d\n", QueueEmpty(Q));
printf("创建队列的元素个数:\n");
cin >> n;
printf("输入%d个插入队列的元素的值:\n", n);
for (i = 0; i < n; i++)
{
cin >> k;
EnQueue(Q, k);
}
printf("输出队列元素的值:\n");
QueueTraverse(Q);
printf("输入插入队列的元素的值:");
cin >> h;
EnQueue(Q, h);
printf("输入插入一个队列元素后队列元素的值:");
QueueTraverse(Q);
DeQueue(Q, a);
printf("输入第1个删除的队头元素的值:%d\n", a);
DeQueue(Q, a);
printf("输入第2个删除的队头元素的值:%d\n", a);
printf("输出两次删除队头元素后队列的元素值:");
QueueTraverse(Q);
if (!GetHead(Q, f))
printf("输出队头元素的值:%d\n", f);
printf("输出队列元素的个数:%d\n", QueueLength(Q));
printf("将Q清为空队列!\n");
CLearQueue(Q);
printf("输出队列元素的个数:%d\n", QueueLength(Q));
printf("判断队列是否为空!\n");
printf("QueueEmpty(Q)=%d\n", QueueEmpty(Q));
system("pause");

}