队列的链式表示和实现

#include<iostream>
using namespace std;
/*头结点指向第一个空QNode*/
typedef struct QNode//数据结构
{
	int data;
	struct QNode *next;
}QNode,*Queueptr;
typedef struct//操作
{
	Queueptr front;
	Queueptr rear;
}LinkQueue;
bool InitQueue(LinkQueue &q)
{
	if (!(q.front = q.rear = (Queueptr)malloc(sizeof(QNode))))return false;
	q.front->next = NULL;
	return true;
}
bool DestroyQueue(LinkQueue &q)
{
	while (q.front)
	{
		q.rear = q.front->next;
		free(q.front);
		q.front = q.rear;
	}
	return true;
}
bool QueueEmpty(LinkQueue q)
{
	if (q.rear == q.front)return true;
	return false;
}
int QueueLength(LinkQueue q)
{
	return (q.rear - q.front);
}
bool GetHead(LinkQueue q, int &e)
{
	if (q.front == q.rear)return false;
	e = q.front->next->data;
	return true;
}
bool EnQueue(LinkQueue &q, int e)
//插入e为新的队尾
{
	Queueptr p = (Queueptr)malloc(sizeof(QNode));
	p->data = e; p->next = NULL;
	q.rear->next = p;
	q.rear = p;
	return true;
}
bool Dequeue(LinkQueue&q, int &e)
//删除对头元素,并用e返回其值
{
	if (q.front == q.rear)return false;
	e = q.front->next->data;
	if (q.front->next = q.rear)
	{
		free(q.rear);
		q.rear = q.front;
	}
	else
	{
		Queueptr p = q.front->next->next;
		free(q.front->next);
		q.front->next = p;
	}
	return false;
}



int main()
{

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值