c 实现简陋的无头节点 无头指针的单向循环链表队列

#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);
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值