严蔚敏数据结构C语言版队列的链式存储方式

 一、队列的结构体定义

#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
#define OK 1
typedef int QElemType;
typedef int Status;
typedef struct QNode{
	QElemType data;
	struct QNode* next;
}QNode,*QueuePtr;
typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

 二、初始化队列

Status InitQueue(LinkQueue &Q)
{
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)return ERROR;
	Q.front->next=NULL;
	return OK;
}

三、销毁队列 

Status DestroyQueue(LinkQueue &Q)
{
	if(!Q.front)return ERROR;
	QueuePtr p;
	while(Q.front->next)
	{
		p=Q.front->next;
		Q.front->next=p->next;
		free(p);
	}
	free(Q.front);
	Q.front=Q.rear=NULL;
	return OK;
}

四、入队

void EnQueue(LinkQueue &Q,QElemType e)
{
	if(!Q.rear)return;
	QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
	s->data=e;
	Q.rear->next=s;
	s->next=NULL;
	Q.rear=s;
}

五、出队 

void DeQueue(LinkQueue &Q,QElemType &e)
{
	if(Q.front==Q.rear)return;
	QueuePtr s=Q.front->next;
	e=s->data;
	Q.front->next=s->next;
	if(Q.rear==s)Q.rear=Q.front;
	free(s);
}

六、遍历打印队列 

void PrintQueue(LinkQueue Q)
{
	printf("队列中的元素为:\n");
	if(!Q.front)return;
	for(QueuePtr p=Q.front->next;p;p=p->next)
	{
		printf("%d ",p->data);
		if((p->data-1)%5==0)puts("");
	}
	puts("\n");
}

总代码如下 

#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
#define OK 1
typedef int QElemType;
typedef int Status;
typedef struct QNode{
	QElemType data;
	struct QNode* next;
}QNode,*QueuePtr;
typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q);
Status DestroyQueue(LinkQueue &Q);
void DeQueue(LinkQueue &Q,QElemType &e);
void EnQueue(LinkQueue &Q,QElemType e);
void PrintQueue(LinkQueue Q);
int main()
{
	LinkQueue Q;
	InitQueue(Q);
	QElemType e;
	for(int i=19422130;i>=19422101;i--)EnQueue(Q,i);
	PrintQueue(Q);
	printf("删掉前十五个元素:\n");
	for(int j=1;j<=15;j++)
	{
		DeQueue(Q,e);
		printf("%d ",e);
		if((e-1)%5==0)puts("");
	}
	puts("\n");
	PrintQueue(Q);
	DestroyQueue(Q);
	return 0;
}
Status InitQueue(LinkQueue &Q)
{
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)return ERROR;
	Q.front->next=NULL;
	return OK;
}
Status DestroyQueue(LinkQueue &Q)
{
	if(!Q.front)return ERROR;
	QueuePtr p;
	while(Q.front->next)
	{
		p=Q.front->next;
		Q.front->next=p->next;
		free(p);
	}
	free(Q.front);
	Q.front=Q.rear=NULL;
	return OK;
}
void DeQueue(LinkQueue &Q,QElemType &e)
{
	if(Q.front==Q.rear)return;
	QueuePtr s=Q.front->next;
	e=s->data;
	Q.front->next=s->next;
	if(Q.rear==s)Q.rear=Q.front;
	free(s);
}
void EnQueue(LinkQueue &Q,QElemType e)
{
	if(!Q.rear)return;
	QueuePtr s=(QueuePtr)malloc(sizeof(QNode));
	s->data=e;
	Q.rear->next=s;
	s->next=NULL;
	Q.rear=s;
}
void PrintQueue(LinkQueue Q)
{
	printf("队列中的元素为:\n");
	if(!Q.front)return;
	for(QueuePtr p=Q.front->next;p;p=p->next)
	{
		printf("%d ",p->data);
		if((p->data-1)%5==0)puts("");
	}
	puts("\n");
}

运行结果

队列中的元素为:
19422130 19422129 19422128 19422127 19422126
19422125 19422124 19422123 19422122 19422121
19422120 19422119 19422118 19422117 19422116
19422115 19422114 19422113 19422112 19422111
19422110 19422109 19422108 19422107 19422106
19422105 19422104 19422103 19422102 19422101


删掉前十五个元素:
19422130 19422129 19422128 19422127 19422126
19422125 19422124 19422123 19422122 19422121
19422120 19422119 19422118 19422117 19422116


队列中的元素为:
19422115 19422114 19422113 19422112 19422111
19422110 19422109 19422108 19422107 19422106
19422105 19422104 19422103 19422102 19422101

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AAAAAZBX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值