c++设计一个选择式菜单,1. 实现顺序循环或链式队列的进队列、出队列、判断队列空否、显示队列中全部元素的运算。
时间: 2024-05-13 11:15:36 浏览: 112
C++菜单制作
5星 · 资源好评率100%
以下是使用顺序队列实现的选择式菜单代码:
```c++
#include <iostream>
using namespace std;
#define MAXSIZE 100 // 队列最大长度
typedef struct {
int data[MAXSIZE]; // 队列数组
int front; // 队头指针
int rear; // 队尾指针
} SqQueue;
// 初始化队列
void InitQueue(SqQueue &q) {
q.front = q.rear = 0;
}
// 判断队列是否为空
bool QueueEmpty(SqQueue q) {
return q.front == q.rear;
}
// 判断队列是否已满
bool QueueFull(SqQueue q) {
return (q.rear + 1) % MAXSIZE == q.front;
}
// 入队列
bool EnQueue(SqQueue &q, int x) {
if (QueueFull(q)) {
return false;
}
q.data[q.rear] = x;
q.rear = (q.rear + 1) % MAXSIZE;
return true;
}
// 出队列
bool DeQueue(SqQueue &q, int &x) {
if (QueueEmpty(q)) {
return false;
}
x = q.data[q.front];
q.front = (q.front + 1) % MAXSIZE;
return true;
}
// 显示队列中全部元素
void DisplayQueue(SqQueue q) {
if (QueueEmpty(q)) {
cout << "队列为空" << endl;
return;
}
int i = q.front;
while (i != q.rear) {
cout << q.data[i] << " ";
i = (i + 1) % MAXSIZE;
}
cout << endl;
}
int main() {
SqQueue q;
InitQueue(q);
int choice, x;
while (true) {
cout << "1. 入队列" << endl;
cout << "2. 出队列" << endl;
cout << "3. 判断队列是否为空" << endl;
cout << "4. 显示队列中全部元素" << endl;
cout << "0. 退出" << endl;
cout << "请输入选项: ";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入要入队的元素: ";
cin >> x;
if (EnQueue(q, x)) {
cout << "入队成功" << endl;
} else {
cout << "队列已满,入队失败" << endl;
}
break;
case 2:
if (DeQueue(q, x)) {
cout << "出队元素为: " << x << endl;
} else {
cout << "队列为空,出队失败" << endl;
}
break;
case 3:
if (QueueEmpty(q)) {
cout << "队列为空" << endl;
} else {
cout << "队列不为空" << endl;
}
break;
case 4:
DisplayQueue(q);
break;
case 0:
return 0;
default:
cout << "无效选项,请重新输入" << endl;
break;
}
}
return 0;
}
```
使用链式队列实现的选择式菜单代码如下:
```c++
#include <iostream>
using namespace std;
typedef struct LNode { // 链式队列结点结构体
int data;
struct LNode *next;
} LNode, *LinkQueuePtr;
typedef struct { // 链式队列结构体
LinkQueuePtr front; // 队头指针
LinkQueuePtr rear; // 队尾指针
} LinkQueue;
// 初始化队列
void InitQueue(LinkQueue &q) {
q.front = q.rear = new LNode;
q.front->next = NULL;
}
// 判断队列是否为空
bool QueueEmpty(LinkQueue q) {
return q.front == q.rear;
}
// 入队列
void EnQueue(LinkQueue &q, int x) {
LinkQueuePtr p = new LNode;
p->data = x;
p->next = NULL;
q.rear->next = p;
q.rear = p;
}
// 出队列
bool DeQueue(LinkQueue &q, int &x) {
if (QueueEmpty(q)) {
return false;
}
LinkQueuePtr p = q.front->next;
x = p->data;
q.front->next = p->next;
if (q.rear == p) {
q.rear = q.front;
}
delete p;
return true;
}
// 显示队列中全部元素
void DisplayQueue(LinkQueue q) {
if (QueueEmpty(q)) {
cout << "队列为空" << endl;
return;
}
LinkQueuePtr p = q.front->next;
while (p != NULL) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main() {
LinkQueue q;
InitQueue(q);
int choice, x;
while (true) {
cout << "1. 入队列" << endl;
cout << "2. 出队列" << endl;
cout << "3. 判断队列是否为空" << endl;
cout << "4. 显示队列中全部元素" << endl;
cout << "0. 退出" << endl;
cout << "请输入选项: ";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入要入队的元素: ";
cin >> x;
EnQueue(q, x);
cout << "入队成功" << endl;
break;
case 2:
if (DeQueue(q, x)) {
cout << "出队元素为: " << x << endl;
} else {
cout << "队列为空,出队失败" << endl;
}
break;
case 3:
if (QueueEmpty(q)) {
cout << "队列为空" << endl;
} else {
cout << "队列不为空" << endl;
}
break;
case 4:
DisplayQueue(q);
break;
case 0:
return 0;
default:
cout << "无效选项,请重新输入" << endl;
break;
}
}
return 0;
}
```
阅读全文