#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node,*Linklist;
Linklist front = NULL,rear = NULL;
#define MAX_NUM 8
Linklist init_circle(Linklist node) {
Linklist next = (Linklist)malloc(sizeof(Node));
node->next = next;
node = next;
return node;
}
int is_full() {
if (front == rear->next) {
return 1;
}
return 0;
}
int is_empty() {
if (front == rear) {
return 1;
}
return 0;
}
void push() {
if (is_full()) {
printf("列表已满,无法压入\n");
return;
}
int value;
printf("请输入要压入的数据:");
scanf_s("%d",&value,sizeof(int));
rear->data = value;
rear = rear->next;
}
int pop() {
int e = front->data;
front->data = 0;
front = front->next;
return e;
}
int main() {
Linklist node = (Linklist)malloc(sizeof(Node));
front = node;
rear = node;
int i = 0,e = 0;
for (i; i < MAX_NUM; i++) {
node = init_circle(node);
}
node->next = front;
do {
printf("\n压入输入1,弹出输入2,退出输入3 : ");
scanf_s("%d",&i,sizeof(int));
getchar();
if (i == 1) {
push();
}
else if (i == 2) {
if (is_empty()) {
printf("队列为空无法弹出\n");
return 0;
}
e = pop();
printf("弹出数据为 : %d",e);
}
} while (i != 3);
return 0;
}
循环队列实际空间大小总会比可用最大空间大一个存储单位