C语言 任意输入队列长度和队列中的元素值,构造一个顺序循环队列,对其进行清空、插入新元素、返回队头元素以及删除队头元素等操作。 1、插入元素;2、显示队列中的元素;3、显示队列长度;4、显示队头元素;5、删除队头元素后,显示队列中元素,同时显示被删除的元素;6、清空队列元素。

时间: 2024-01-13 08:05:24 浏览: 25
好的,我可以帮你解答这个问题。以下是C语言实现顺序循环队列的代码,其中包括清空、插入新元素、返回队头元素以及删除队头元素等操作: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10 typedef struct { int data[MAX_SIZE]; int front; // 队首指针 int rear; // 队尾指针 } Queue; void init(Queue *q) { q->front = q->rear = 0; } int is_empty(Queue *q) { return q->front == q->rear; } int is_full(Queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } int enqueue(Queue *q, int x) { if (is_full(q)) { printf("Queue is full!\n"); return 0; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAX_SIZE; return 1; } int dequeue(Queue *q, int *x) { if (is_empty(q)) { printf("Queue is empty!\n"); return 0; } *x = q->data[q->front]; q->front = (q->front + 1) % MAX_SIZE; return 1; } int get_front(Queue *q, int *x) { if (is_empty(q)) { printf("Queue is empty!\n"); return 0; } *x = q->data[q->front]; return 1; } int get_size(Queue *q) { return (q->rear - q->front + MAX_SIZE) % MAX_SIZE; } void clear(Queue *q) { q->front = q->rear = 0; } void display(Queue *q) { int i; if (is_empty(q)) { printf("Queue is empty!\n"); return; } printf("Elements of the queue are: "); i = q->front; while (i != q->rear) { printf("%d ", q->data[i]); i = (i + 1) % MAX_SIZE; } printf("\n"); } int main() { Queue q; int x, choice; init(&q); while (1) { printf("\nEnter your choice:\n"); printf("1. Enqueue an element\n"); printf("2. Display the elements\n"); printf("3. Display the size\n"); printf("4. Display the front element\n"); printf("5. Dequeue an element\n"); printf("6. Clear the queue\n"); printf("0. Exit\n"); scanf("%d", &choice); switch (choice) { case 0: exit(0); case 1: printf("Enter the element to be enqueued: "); scanf("%d", &x); if (enqueue(&q, x)) { printf("Enqueued successfully!\n"); } break; case 2: display(&q); break; case 3: printf("Size of the queue is: %d\n", get_size(&q)); break; case 4: if (get_front(&q, &x)) { printf("Front element is: %d\n", x); } break; case 5: if (dequeue(&q, &x)) { printf("Dequeued element is: %d\n", x); printf("Queue after dequeue: "); display(&q); } break; case 6: clear(&q); printf("Queue cleared successfully!\n"); break; default: printf("Invalid choice!\n"); } } return 0; } ``` 以上代码中,`Queue`是一个结构体,包含队列的数据`data`、队首指针`front`和队尾指针`rear`。`init`函数用于初始化队列,`is_empty`和`is_full`函数判断队列是否为空或已满。`enqueue`函数用于插入新元素,`dequeue`函数用于删除队头元素并返回其值,`get_front`函数用于返回队头元素的值而不删除它,`get_size`函数用于返回队列的长度。`clear`函数用于清空队列,`display`函数用于打印队列中的元素。 在`main`函数中,我们使用一个无限循环来接收用户输入的选择。用户可以选择插入元素、显示队列中的元素、显示队列长度、显示队头元素、删除队头元素、清空队列元素或退出程序。程序根据用户的选择调用相应的函数。

相关推荐

rar

最新推荐

recommend-type

利用顺序栈逆置循环队列.docx

设计一个算法,用一个栈s将-一个队列Q逆置: (1)要求采用顺序栈和循环队列来实现。 (2)要求采用链栈和链队列来实现。
recommend-type

C语言用栈和队列实现的回文检测功能示例

主要介绍了C语言用栈和队列实现的回文检测功能,结合具体实例形式分析了C语言栈和队列的定义及使用栈和队列进行回文检测的操作技巧,需要的朋友可以参考下
recommend-type

C语言使用广度优先搜索算法解决迷宫问题(队列)

主要介绍了C语言使用广度优先搜索算法解决迷宫问题,结合迷宫问题分析了C语言队列广度优先搜索算法的相关使用技巧,需要的朋友可以参考下
recommend-type

在C语言中输入一个大写字母,将其转变成一个小写字母,并且有相应的提示。

1.学习简单的C语言编程
recommend-type

优先队列(priority_queue)的C语言实现代码

本文简要介绍一种基于数组二叉堆实现的优先队列,定义的数据结构和实现的函数接口说明如下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。