(第三章)4.链队列

#include<stdio.h>
#include<stdlib.h>

typedef struct LQueue
{
	int data;
	struct LQueue * next;
}LQueue;

void Create_LQueue(LQueue * p)
{
	int x;
	LQueue * s;
	printf("请输入入队的元素(输入888结束):");
	scanf_s("%d", &x);
	if(x!=888)
	{
		while(x!=888)
		{
			s=(LQueue *)malloc(sizeof(LQueue));
			s->data=x;
			s->next=NULL;
			p->next=s;
			p=s;  //记着别漏了
			scanf_s("%d", &x);
		}
	}
	else
		p->next=NULL;
}

void En_LQueue(LQueue * p, int x)
{
	LQueue * s;
	s=(LQueue *)malloc(sizeof(LQueue));
	s->data=x;
	s->next=NULL;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	p->next=s;
	p=s;
}
void De_LQueue(LQueue * p, int * x)
{
	LQueue * s;
	s=p->next;
	*x=s->data;
	p->next=s->next;
	free(s);
	printf("元素%d出队\n", *x);
}

void Printf(LQueue * p)
{
	LQueue * s;
	s=p->next;
	if(s)
	{
		printf("队列的内容为:");
		while(s)
		{
			printf("%d ", s->data);
			s=s->next;
		}
		printf("\n");
	}
	else
		printf("队列为空,无法打印!\n");
}

int Length(LQueue * p)
{
	LQueue * s;
	int len=0;
	s=p->next;
	if(s)
	{
		while(s)
		{
			len++;
			s=s->next;
		}
		return len;
	}
	else
		return 0;
}

int main()
{
	LQueue p;
	int x=999;
	int t; int i;
	Create_LQueue(&p);
	Printf(&p);
	printf("队列长度:%d\n", Length(&p));
	printf("将999入队:\n");
	En_LQueue(&p, x);
	Printf(&p);
	printf("元素全部出队:\n");
	while((&p)->next)          //p不是指针,通过<&p>和循环遍历队列
	{
		De_LQueue(&p, &t);
	}
	Printf(&p);
	printf("队列长度:%d\n", Length(&p));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值