C语言实现循环队列和链队列:初始化、入队、出队操作及菜单演示
C语言实现循环队列和链队列:初始化、入队、出队操作及菜单演示
本文将使用 C 语言实现循环队列和链队列,并编写相应的初始化、入队、出队操作,以及一个交互式菜单,方便用户进行测试和演示。
循环队列的定义
#define MAXSIZE 100 // 队列的最大长度
typedef struct {
int data[MAXSIZE];
int front; // 队头指针
int rear; // 队尾指针
} Queue;
void initQueue(Queue *q) { // 初始化队列
q->front = q->rear = 0;
}
int isEmpty(Queue q) { // 判断队列是否为空
return q.front == q.rear;
}
int isFull(Queue q) { // 判断队列是否已满
return (q.rear + 1) % MAXSIZE == q.front;
}
void enQueue(Queue *q, int x) { // 入队
if (isFull(*q)) {
printf('队列已满,无法入队!\n');
} else {
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE;
}
}
int deQueue(Queue *q) { // 出队
if (isEmpty(*q)) {
printf('队列为空,无法出队!\n');
return -1;
} else {
int x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return x;
}
}
链队列的定义
typedef struct QNode {
int data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct {
QueuePtr front; // 队头指针
QueuePtr rear; // 队尾指针
} LinkQueue;
void initQueue(LinkQueue *q) { // 初始化队列
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!q->front) {
exit(1); // 内存分配失败
}
q->front->next = NULL;
}
int isEmpty(LinkQueue q) { // 判断队列是否为空
return q.front == q.rear;
}
void enQueue(LinkQueue *q, int x) { // 入队
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p) {
exit(1); // 内存分配失败
}
p->data = x;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
int deQueue(LinkQueue *q) { // 出队
if (isEmpty(*q)) {
printf('队列为空,无法出队!\n');
return -1;
} else {
QueuePtr p = q->front->next;
int x = p->data;
q->front->next = p->next;
if (q->rear == p) {
q->rear = q->front;
}
free(p);
return x;
}
}
菜单实现
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 // 队列的最大长度
typedef struct {
int data[MAXSIZE];
int front; // 队头指针
int rear; // 队尾指针
} Queue;
void initQueue(Queue *q) { // 初始化队列
q->front = q->rear = 0;
}
int isEmpty(Queue q) { // 判断队列是否为空
return q.front == q.rear;
}
int isFull(Queue q) { // 判断队列是否已满
return (q.rear + 1) % MAXSIZE == q.front;
}
void enQueue(Queue *q, int x) { // 入队
if (isFull(*q)) {
printf('队列已满,无法入队!\n');
} else {
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE;
}
}
int deQueue(Queue *q) { // 出队
if (isEmpty(*q)) {
printf('队列为空,无法出队!\n');
return -1;
} else {
int x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE;
return x;
}
}
typedef struct QNode {
int data;
struct QNode *next;
} QNode, *QueuePtr;
typedef struct {
QueuePtr front; // 队头指针
QueuePtr rear; // 队尾指针
} LinkQueue;
void initQueue(LinkQueue *q) { // 初始化队列
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!q->front) {
exit(1); // 内存分配失败
}
q->front->next = NULL;
}
int isEmpty(LinkQueue q) { // 判断队列是否为空
return q.front == q.rear;
}
void enQueue(LinkQueue *q, int x) { // 入队
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if (!p) {
exit(1); // 内存分配失败
}
p->data = x;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
int deQueue(LinkQueue *q) { // 出队
if (isEmpty(*q)) {
printf('队列为空,无法出队!\n');
return -1;
} else {
QueuePtr p = q->front->next;
int x = p->data;
q->front->next = p->next;
if (q->rear == p) {
q->rear = q->front;
}
free(p);
return x;
}
}
int main() {
int choice, x;
Queue q1; // 定义循环队列
LinkQueue q2; // 定义链队列
initQueue(&q1); // 初始化循环队列
initQueue(&q2); // 初始化链队列
while (1) {
printf('请选择操作:\n');
printf('1. 初始化队列\n');
printf('2. 入队\n');
printf('3. 出队\n');
printf('4. 退出\n');
scanf('%d', &choice);
switch (choice) {
case 1:
initQueue(&q1);
initQueue(&q2);
printf('初始化成功!\n');
break;
case 2:
printf('请输入要入队的数:');
scanf('%d', &x);
enQueue(&q1, x);
enQueue(&q2, x);
printf('入队成功!\n');
break;
case 3:
x = deQueue(&q1);
if (x != -1) {
printf('循环队列出队元素为:%d\n', x);
}
x = deQueue(&q2);
if (x != -1) {
printf('链队列出队元素为:%d\n', x);
}
break;
case 4:
exit(0);
default:
printf('输入有误,请重新输入!\n');
}
}
return 0;
}
使用方法:
- 编译并运行代码。
- 在菜单中选择操作:
-
- 初始化队列:初始化循环队列和链队列。
-
- 入队:输入要入队的数,并将其加入循环队列和链队列。
-
- 出队:从循环队列和链队列中分别出队一个元素,并打印出队元素。
-
- 退出:退出程序。
-
说明:
- 循环队列使用数组实现,链队列使用链表实现。
- 循环队列的最大长度由
MAXSIZE定义。 - 队列为空时,出队操作将返回
-1,并打印提示信息。 - 队列已满时,入队操作将打印提示信息。
- 程序使用了
malloc函数来动态分配内存,使用free函数来释放内存。 - 程序使用了
exit函数来退出程序。
总结:
本文介绍了如何使用 C 语言实现循环队列和链队列,并提供了一个完整的代码示例,方便读者学习和使用。循环队列和链队列是数据结构中的基本概念,在实际应用中有着广泛的应用。
原文地址: https://www.cveoy.top/t/topic/ok1a 著作权归作者所有。请勿转载和采集!