#include <stdio.h>
#include <stdlib.h>
//typedef struct queue {
// int num;
// int front,now;
// int* array;
//}qu;
//typedef struct queue* arrayqueue;
//_Bool initqueue(arrayqueue code) {
// code->array = malloc(sizeof(int)*10);
// code->num = 10;
// code->front = code->now = 0;
// return 1;
//}
//_Bool push(arrayqueue c,int data) {
// if ((c->now + 1) % c->num == c->front) {//这里可以优化一下
// printf("满了,揪可。");
// return 0;
// }
// c->now=(c->now+1)%c->num;
// c->array[c->now] = data;
// return 1;
//
// ///
// //if ((c->now + 1) % c->num == c->front) //先判断队列是否已满,如果队尾下一个就是队首,那么说明已满
// // return 0;
// //c->now = (c->now + 1) % c->num; //队尾先向前移动一位,注意取余计算才能实现循环
// //c->array[c->now] = data; //在新的位置插入元素
// //return 1;
//}
//_Bool pop(arrayqueue c) {
// if ((c->front + 1) % c->num == c->now)return 0;
// c->front = (c->front + 1) % c->num;
// return 1;
//}
//void printqueue(arrayqueue c) {
// int i = c->front;
// do {
// i = (i + 1) % c->num;
// printf("%d\t", c->array[i]);
// } while (i!=c->now);
//};
//void main() {
// qu test;
// initqueue(&test);
// for (int i = 0; i < 4; i++) {
// push(&test, i);
// }
// printqueue(&test);
// pop(&test);
// printqueue(&test);
//}//以上都是顺序存储
//以下是链表存储且为无头节点,单向循环的形式实现
//新要求没有front指针
typedef struct list* p;
typedef struct list {
int data;
p next;
int boolThefirst;//判断是否为第一个元素是否为空
int num;
}Node;
struct queue {
p front;
p now;
};
typedef struct queue* linkedqueue;
初始化(无头节点方式)
_Bool InItqueue(linkedqueue c) {
p node = malloc(sizeof(Node));
node->boolThefirst = 0;
if (node == NULL)return 0;//初始化失败强制退出
c->now = node;
return 0;
}
//入队方法
_Bool Push(linkedqueue c,int data) {
int tf = c->now->boolThefirst;//填充为第一个元素
if (tf== 0) {
c->now->data = data;
c->now->boolThefirst = 1;
c->now->num = 1;
c->now->next = c->now;
return 0;
}
p new = malloc(sizeof(struct list));//创建新的节点
new->data = data;
new->next = c->now->next;
c->now->next = new;
c->now = new;
}
_Bool printqueue(linkedqueue c) {
if (c->now->next == c->now) {
printf("emmmm,没了");
InItqueue(c);
return 0;
}
p nums = c->now->next;
while (1) {
printf("%d\t", nums->data);
if (nums == c->now)break;
else {
nums = nums->next;
}
}
}
// if (c->now->next== c->now) {//当队列为空
// printf("队列为空了,请初始化");
// return 0;
// }
// printf("<- sir this way\t\t ");
// p node = c->front;
// while (1) { //注意不能直接判空,因为前面没考虑,也就没将新结点now 设定为NULL
// printf("%d ", node->data);
// if (node == c->now) break; //当已经打印最后一个元素后,再结束
// else node = node->next;
// }
// printf("\t<- sir this way\n");
//}
_Bool pop(linkedqueue c) {
p del = c->now->next;
if(del !=c->now ){
c->now->next = c->now->next->next;
free(del);
return 0;
}
free(del);
//初始化
return 0;
//p del = c->front;
//if(del !=c->now ){
// c->front = c->front->next;
// free(del);
// return 0;
//}
//free(del);
//InItqueue(c);//初始化
//return 0;
}
void main() {
struct queue queue1;
InItqueue(&queue1);
for (int i = 0; i < 5; ++i) {
Push(&queue1, i * 100);
}
printqueue(&queue1);
printf("\n");
pop(&queue1);
printqueue(&queue1);
printf("\n");
for (int i = 0; i < 5; ++i) {
Push(&queue1, i * 100);
}
printf("\n");
printqueue(&queue1);
}