链队列的表示和实现

1.链队列的表示

typedef  struct QNode

{

    QElemType      data;

    struct   QNode   *next;

}QNode, *QueuePtr;

typedef  struct

{

   QueuePtr   *front;          //队头指针

   QueuePtr   *rear;          //队尾指针

}LinkQueue;

2.基本操作的函数原型

1)创建

Status InitQueue(LinkQueue &Q);

操作结果:构造一个空对列Q

2)判队空

Status QueueEmpty(LinkQueue Q); 

操作结果:若Q为空队列,返回TRUE,否则返回FALSE

3)求队长

int QueueLength(LinkQueue Q);

操作结果:返回Q的元素个数

4)入队

Status EnQueue(LinkQueue &Q,QElemType e);

操作结果:插入元素e为新的队尾元素

5)出队

Status DeQueue(LinkQueue &Q,QElemType &e)
操作结果:若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;

                 否则返回ERROR

6)获取队头元素

Status GetHead(LinkQueue Q,QElemType &e)
操作结果:若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR

7)清空

Status ClearQueue(LinkQueue &Q)
操作结果:将Q清为空队列

8)输出队列中的全部元素

void Print(LinkQueue &Q)
操作结果:输出队列Q中的全部元素

9)销毁

Status DestroyQueue(LinkQueue &Q)
操作结果:销毁队列Q ,Q不再存在

3.链队列及其操作示意图

4.附录(实现代码) 

//----*---*---*--------
//程序名称:链队列
//编译环境:VC++ 6.0
//作者:Bee_darker
//修改日期:2018-10-25
//----*---*---*--------

#define  OK   1
#define  OVERFLOW -1
#define  ERROR  0
#define  TRUE  1
#define  FALSE  0

#include "stdio.h"
#include "stdlib.h"

typedef int   Status;
typedef int   QElemType;

//定义链队列
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)
		exit(OVERFLOW);				//存储分配失败
	Q.front->next = NULL;
	return OK;
}

//判队空
Status QueueEmpty(LinkQueue Q)
{
	if(Q.front == Q.rear)
		return  TRUE;
	else
		return  FALSE;
}

//求队长
int QueueLength(LinkQueue Q)
{
	QueuePtr p = Q.front->next;
	int len = 0;
	while(p)
	{
		p = p->next;
		++len;
	}
	return len;
}
//入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
	//插入元素e为Q的新的队尾元素
	QueuePtr p;
	p = (QueuePtr)malloc(sizeof(QNode));
	if(!p)	exit(OVERFLOW);		//存储分配失败
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return OK;
}

//出队
Status DeQueue(LinkQueue &Q,QElemType &e)
{
	//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
	//否则返回ERROR
	QueuePtr p;
	if(Q.front == Q.rear)
		return ERROR;
	p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if(Q.rear == p)
		Q.rear = Q.front;
	free(p);
	return OK;
}

//获取队头元素
Status GetHead(LinkQueue Q,QElemType &e)
{
	if(Q.front != Q.rear)
	{
		e=Q.front ->next ->data ;
		return OK;
	}
	else
		return ERROR;
}

//清空
Status ClearQueue(LinkQueue &Q)
{
	Q.front = Q.rear;
	return OK;
}
//输出队列中的全部元素
void Print(LinkQueue &Q)
{
	QueuePtr p = Q.front->next;
	printf("Print:Q.front->");
	while(p)
	{
		printf("%d->",p->data);
		p = p->next;
	}
	printf("Q.rear");
}

//销毁
Status DestroyQueue(LinkQueue &Q)
{
	while(Q.front)
	{
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	}
	return OK;
}

int main()
{
	LinkQueue Q;
	QElemType x;
	QElemType e;
	InitQueue(Q);
	printf("QueueEmpty:%d\n",QueueEmpty(Q));
	EnQueue(Q,1);
	EnQueue(Q,2);
	EnQueue(Q,3);
	printf("QueueLength:%d\n",QueueLength(Q));
	Print(Q);
	printf("\nDeQueue:%d\n",DeQueue(Q,x));
	Print(Q);
	GetHead(Q,e);
	printf("\nGetHead:%d\n",e);
	printf("ClearQueue:%d\n",ClearQueue(Q));
	Print(Q);
	printf("\nDestroyQueue:%d\n",DestroyQueue(Q));
	return 0;
}

5.测试结果示例

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值