数据结构之链队

本文详细介绍了数据结构中的链队,包括链队的概念、特点、如何通过链表实现链队,以及链队在实际问题中的应用。通过对链队的操作如入队、出队的讨论,深入理解其工作原理。
摘要由CSDN通过智能技术生成

#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct Node
{
	ElemType data;
	struct Node *pNext;
}Node, *pNode;
typedef struct {
	pNode pFront;
	pNode pRear;
}Queue;
void InitQueue(Queue &Q)//队中的两个针指向一个不存放有效元素的头节点
{
	Q.pFront = new Node;
	Q.pRear = Q.pFront;
	Q.pFront->pNext = NULL;
}
void DestroyQueue(Queue &Q)
{
	while (Q.pFront)
	{
		Q.pRear = Q.pFront->pNext;
		delete Q.pFront;
		Q.pFront = Q.pRear;
	}
}
bool QueueEmpty(Queue Q)
{
	if (Q.pRear == Q.pFront)
		return true;
	else
		return false;
}
void EnQueue(Queue &Q, ElemType e)
{
	pNode pNew = new Node;
	pNew->data = e;
	pNew->pNext = NULL;
	Q.pRear->pNext = pNew;
	Q.pRear = pNew;
	pNew = NULL;
}
bool DeQueue(Queue &Q, ElemType &e)
{
	if (QueueEmpty(Q))
		return false;
	pNode pTem = Q.pFront->pNext;
	e = pTem->data;
	Q.pFront->pNext = pTem->pNext;
	if (Q.pRear == pTem)//当队中只有一个有效元素时
		Q.pRear = Q.pFront;
	delete pTem;
	pTem = NULL;
	return true;
}
bool GetHead(Queue Q, ElemType &e)
{
	if (QueueEmpty(Q))
		return false;
	e = Q.pFront->pNext->data;
	return true;
}
void VisitElem(ElemType e)
{
	cout << e << " ";
}
void DisPlay(Queue Q)
{
	if (QueueEmpty(Q))
	{
		cout << "Indeed Empty! No Print!" << endl;
		return;
	}
	pNode pTem = Q.pFront->pNext;
	while (pTem)
	{
		VisitElem(pTem->data);
		pTem = pTem->pNext;
	}
	cout << endl;
}
int main(void)
{
	Queue Q;
	ElemType e = 0;
	InitQueue(Q);
	EnQueue(Q, 1);
	EnQueue(Q, 2);
	EnQueue(Q, 3);
	EnQueue(Q, 4);
	EnQueue(Q, 5);
	EnQueue(Q, 6);
	if (GetHead(Q, e))
		VisitElem(e);
	cout << endl;
	DisPlay(Q);
	DeQueue(Q, e);
	DeQueue(Q, e);
	DisPlay(Q);
	return(0);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值