链队列-数据结构(11)

一、解析

对于链队列,队列的意义就不解析了。而队列的链式存储则是与顺序队列成为一个很好的对比,顺序队列中的存储大小是固定的不,而链式的存储则是可以达到节省存储空间的作用,只是在入队和出队的情况下,需要些额外的操作。参考书上P60-P63。

二、存储结构

//==================链队列===============
typedef struct QNode{
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
	QueuePtr front;//队头
	QueuePtr rear;//队尾
}LinkQueue;

三、操作

//==================链队列===============
typedef struct QNode{
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
	QueuePtr front;//队头
	QueuePtr rear;//队尾
}LinkQueue;

Status InitQueue(LinkQueue &Q){
	//构造一个空队列Q
	Q.front = (QueuePtr)malloc(sizeof(QNode));
	Q.rear = Q.front;
	if (!Q.front)
	{
		return OVERFLOW;
	}
	Q.front->next = NULL;
	return OK;
}

Status EnQueue(LinkQueue &Q, QElemType e){
	//进队列 在队尾入队
	QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return OK;
}

Status DeQueue(LinkQueue &Q, QElemType &e){
	//出队列
	if (Q.front == Q.rear)
	{
		printf("队列为null");
		return ERROR;
	}
	//Q.front = Q.front->next;
	QueuePtr p = Q.front->next;
	e = p->data;
	Q.front->next = p->next;
	if (Q.rear == p)
	{
		Q.rear = Q.front;
	}
	free(p);
	return OK;
}

四、执行

	LinkQueue q;
	InitQueue(q);
	EnQueue(q,1);
	EnQueue(q, 2);
	EnQueue(q, 3);
	EnQueue(q, 4);
	int e;
	DeQueue(q, e);
	printf("%d\n",e);
	DeQueue(q, e);
	printf("%d\n", e);	
	DeQueue(q, e);
	printf("%d\n", e);	
	DeQueue(q, e);
	printf("%d\n", e);
	DeQueue(q, e);
	printf("%d\n", e);

输出:
1
2
3
4
队列为null4
请按任意键继续. . .






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值