写一个包含所有队列操作的c语言程序
时间: 2023-09-11 22:05:35 浏览: 52
### 回答1:
以下是一个包含所有队列操作的C语言程序示例:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
// 定义队列结构体
typedef struct {
int front, rear;
int arr[MAX_SIZE];
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = -1;
q->rear = -1;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->front == -1;
}
// 判断队列是否已满
int isFull(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
// 入队操作
void enqueue(Queue *q, int val) {
if (isFull(q)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isEmpty(q)) {
q->front = 0;
q->rear = 0;
} else {
q->rear = (q->rear + 1) % MAX_SIZE;
}
q->arr[q->rear] = val;
}
// 出队操作
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
}
int val = q->arr[q->front];
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front = (q->front + 1) % MAX_SIZE;
}
return val;
}
// 获取队首元素
int getFront(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty. Cannot get front element.\n");
return -1;
}
return q->arr[q->front];
}
// 获取队尾元素
int getRear(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty. Cannot get rear element.\n");
return -1;
}
return q->arr[q->rear];
}
int main() {
Queue q;
initQueue(&q);
// 测试入队操作
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
// 测试出队操作
int val = dequeue(&q);
printf("Dequeued element: %d\n", val);
// 测试获取队首和队尾元素
printf("Front element: %d\n", getFront(&q));
printf("Rear element: %d\n", getRear(&q));
return 0;
}
```
该程序包含了队列的初始化、判断队列是否为空、判断队列是否已满、入队操作、出队操作、获取队首元素、获取队尾元素等所有队列操作。可以通过修改MAX_SIZE的值来更改队列的大小。
### 回答2:
以下是一个包含所有队列操作的简单C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct {
int front, rear;
int data[MAX_SIZE];
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = -1;
q->rear = -1;
}
// 检查队列是否为空
int isEmpty(Queue *q) {
return q->front == -1;
}
// 检查队列是否已满
int isFull(Queue *q) {
return q->rear == MAX_SIZE - 1;
}
// 入队操作
void enqueue(Queue *q, int item) {
if (isFull(q)) {
printf("队列已满,无法插入元素\n");
return;
}
if (isEmpty(q))
q->front = 0;
q->rear++;
q->data[q->rear] = item;
printf("成功插入元素 %d 到队列\n", item);
}
// 出队操作
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("队列为空,无法删除元素\n");
return -1;
}
int item = q->data[q->front];
if (q->front == q->rear)
initQueue(q);
else
q->front++;
printf("成功删除元素 %d 从队列\n", item);
return item;
}
// 获取队头元素
int peek(Queue *q) {
if (isEmpty(q)) {
printf("队列为空\n");
return -1;
}
return q->data[q->front];
}
// 打印队列中的元素
void display(Queue *q) {
if (isEmpty(q)) {
printf("队列为空\n");
return;
}
printf("当前队列中的元素为:");
for (int i = q->front; i <= q->rear; i++)
printf("%d ", q->data[i]);
printf("\n");
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
display(&q); // 10 20 30
dequeue(&q);
display(&q); // 20 30
printf("队头元素为:%d\n", peek(&q)); // 20
return 0;
}
```
上述程序实现了一个队列的基本操作,包括初始化队列、检查队列是否为空、检查队列是否已满、入队、出队、获取队头元素和打印队列中的元素。在`main`函数中,我们创建了一个队列并对其进行了一系列操作,以验证程序的正确性。
### 回答3:
下面是一个包含所有队列操作的C语言程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
// 初始化队列
void initQueue(Queue *queue) {
queue->front = queue->rear = -1;
}
// 判断队列是否为空
int isQueueEmpty(Queue *queue) {
return queue->front == -1;
}
// 判断队列是否已满
int isQueueFull(Queue *queue) {
return queue->rear == MAX_QUEUE_SIZE - 1;
}
// 入队
void enqueue(Queue *queue, int item) {
if (isQueueFull(queue)) {
printf("队列已满,无法入队\n");
return;
}
// 如果队列为空,则同时更新头指针
if (isQueueEmpty(queue)) {
queue->front = 0;
}
queue->rear++;
queue->data[queue->rear] = item;
}
// 出队
int dequeue(Queue *queue) {
if (isQueueEmpty(queue)) {
printf("队列为空,无法出队\n");
return -1;
}
int item = queue->data[queue->front];
// 如果只剩下最后一个元素,出队后更新头指针
if (queue->front == queue->rear) {
queue->front = queue->rear = -1;
} else {
queue->front++;
}
return item;
}
// 获取队头元素
int getFront(Queue *queue) {
if (isQueueEmpty(queue)) {
printf("队列为空,无法获取队头元素\n");
return -1;
}
return queue->data[queue->front];
}
// 获取队列长度
int getQueueLength(Queue *queue) {
if (isQueueEmpty(queue)) {
return 0;
}
return queue->rear - queue->front + 1;
}
int main() {
Queue queue;
initQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
printf("队列长度:%d\n", getQueueLength(&queue));
printf("队头元素:%d\n", getFront(&queue));
int item1 = dequeue(&queue);
int item2 = dequeue(&queue);
printf("出队元素:%d, %d\n", item1, item2);
printf("队列长度:%d\n", getQueueLength(&queue));
printf("队头元素:%d\n", getFront(&queue));
return 0;
}
```
这个程序定义了一个队列结构体`Queue`,包含队列数组、队头指针和队尾指针。通过`initQueue`函数来初始化队列,`isQueueEmpty`函数判断队列是否为空,`isQueueFull`函数判断队列是否已满。`enqueue`函数将元素入队,`dequeue`函数将元素出队,`getFront`函数获取队头元素,`getQueueLength`函数获取队列长度。主函数示例了对队列的操作,包括入队、出队、获取队列长度等。