圆桌会议环形队列C语言代码
时间: 2023-07-10 17:03:56 浏览: 190
以下是一个简单的圆桌会议环形队列的 C 语言代码实现:
```c
#include <stdio.h>
#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int isFull() {
if ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1)) {
return 1;
} else {
return 0;
}
}
int isEmpty() {
if (front == -1) {
return 1;
} else {
return 0;
}
}
void enqueue(int data) {
if (isFull()) {
printf("Queue is full!\n");
} else {
if (front == -1) {
front = 0;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = data;
}
}
int dequeue() {
int data;
if (isEmpty()) {
printf("Queue is empty!\n");
return (-1);
} else {
data = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
return (data);
}
}
void display() {
int i;
if (isEmpty()) {
printf("Queue is empty!\n");
} else {
printf("Queue elements are: \n");
for (i = front; i != rear; i = (i + 1) % MAX_SIZE) {
printf("%d ", queue[i]);
}
printf("%d", queue[i]);
}
}
int main() {
int choice, data;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to enqueue: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
这个实现使用了一个固定大小的数组来存储元素,并使用了 front 和 rear 指针来跟踪队列的开头和结尾。enqueue 和 dequeue 操作都考虑了环形队列的特殊性质。isplay 操作只是简单地遍历队列并输出元素。
阅读全文