(1)实验目的
通过该实验,使学生理解链队列的构造特点并灵活应用,掌握链队基本操作的编程实现,认识栈是在一端进行插入,在另一端进行删除集中操作的线性结构,掌握队列的“先入先出”操作特点,知道判断队列空和满的条件,进一步熟悉C语言中指针操作。
(2)实验内容
用链式存储结构,实现教材定义的队列的基本操作。
(3)参考界面
菜单中包括以下功能:
1.初始化队列,2.销毁队列,3.清空队列,4. 队列判空,5.求队列长度,6.获取队头元素,7.插入一个 元素,8.删除一个元素,9输出所有元素。
要求:自定义的函数中不允许出现提示语和输出语句。
(4)验收/测试用例
通过菜单调用各个操作,测试点:
- 没有初始化前进行其他操作,程序是否能控制住;
- 初始化一个队列;
- 判队列空,屏幕显示队列为空;
- 3个数入队, 3、5、7;
- 队头长度,屏幕输出3;
- 取队头元素,再判队列是否空,然后再判队列长度,(让学生知道取队头元素不改变队列中的内容,队头指针不发生改变);
- 出队,再判队列长度和显示队列中剩余的元素;(多次出队,队列为空之后再执行出队操作,是否提示队列为空);
- 入队一个元素2,再出队,再判断队列是否为空,(主要测试出队操作中特殊情况下的那两行代码是否写了);
- 销毁队,再做其他操作,判断程序是否能控制。
#include<iostream> using namespace std; #define OK 1 #define ERROR 0 typedef int QElemType; typedef int Status; typedef struct QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; //1.初始化队列 Status InitQuene(LinkQueue& Q) { Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if (!Q.front) { exit(OVERFLOW); } Q.front->next = NULL; return OK; } //2.销毁队列 Status DestroyQueue(LinkQueue& Q) { while (Q.front) { Q.rear = Q.front->next; free(Q.front); Q.front = Q.rear; } Q.front = Q.rear = NULL; return OK; } //3.清空队列 Status ClearQueue(LinkQueue& Q) { QueuePtr p = Q.front->next, t; Q.front->next = NULL; while (p) { t = p->next; free(p); p = t; } Q.rear = Q.front; return OK; } //4. 队列判空 Status QueueEmpty(LinkQueue Q) { if (Q.front == Q.rear) { return OK; } else { return ERROR; } } //5.求队列长度 int QueueLength(LinkQueue Q) { int length = 0; while (Q.front != Q.rear) { length++; Q.front = Q.front->next; } return length; } //6.获取队头元素 Status GetHead(LinkQueue Q,QElemType &e) { e = Q.front->next->data; return OK; } //7.插入一个元素 Status EnQueue(LinkQueue& Q, QElemType e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if (!p) { exit(OVERFLOW); } p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } //8.删除一个元素 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; } //9.输出所有元素。 Status Print(LinkQueue Q,QElemType&e) { QueuePtr p; p = Q.front->next; while (p) { e = p->data; cout << e << endl; p = p -> next; } return OK; } int main() { LinkQueue Q; Q.front = NULL; Q.rear = NULL; cout << "1.初始化队列"<< endl; cout << "2.销毁队列" << endl; cout << "3.清空队列" << endl; cout << "4.队列判空" << endl; cout << "5.求队列长度" << endl; cout << "6.获取队头元素" << endl; cout << "7.插入一个 元素" << endl; cout << "8.删除一个元素" << endl; cout << "9.输出所有元素" << endl; cout << "输入负数,退出程序" << endl; int flag = 1; while (flag == 1) { int select; cout << "---------------------" << endl; cout << "请输入你的选择:"; cin >> select; switch (select) { case 1: InitQuene(Q); if (Q.front != NULL) { cout << "队列初始化完成!" << endl; } else { cout << "队列初始化失败!请重新初始化~" << endl; } break; case 2: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { DestroyQueue(Q); if (Q.front == NULL) { cout << "销毁成功" << endl; } else { cout << "销毁失败" << endl; } } break; case 3: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { ClearQueue(Q); if (Q.front == Q.rear) { cout << "清空成功" << endl; } else { cout << "清空失败" << endl; } } break; case 4: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { cout << "队列此时不为空" << endl; } } break; case 5: if(Q.front == NULL) { cout << "队列未初始化" << endl; } else if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { cout << "此时长度为:" << QueueLength(Q) << endl; } break; case 6: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { int e; GetHead(Q, e); cout << "队首元素为:" <<e<< endl; } } break; case 7: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { int option=1; do { int e; cout << "请输入要插入的元素:"; cin >> e; EnQueue(Q, e); int select; cout << "是否继续进行:1.继续 2.退出" << endl; cout << "选择:"; cin >> select; switch (select) { case 1: option = 1; break; case 2: option = 0; break; } } while (option==1); } break; case 8: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { int e; DeQueue(Q, e); cout << "删除队头元素成功" << endl; } } break; case 9: if (Q.front == NULL) { cout << "队列未初始化" << endl; } else { if (QueueEmpty(Q)) { cout << "队列此时为空" << endl; } else { int e; Print(Q, e); } } break; default: if (select < 0) { flag = 0; cout << "退出程序!欢迎下次使用" << endl; break; } else { cout << "输入的指令有误" << endl; } } } }