链队列的基本操作

#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int dataType;

//定义链队列结点结构
typedef struct Node{
	struct Node *next;
	dataType data;
}LQueueNode;
//定义链队列结构
typedef struct {
	LQueueNode *front;  //指向头部结点,出队
	LQueueNode *rear;  //指向尾部结点,入队
}LQueue;
typedef LQueue *pLQueue;  //定义一个指向链队列的指针

//初始化链队列
pLQueue InitLQueue()
{
	pLQueue plq=(LQueue*)malloc(sizeof(LQueue));
	plq->front=plq->rear=NULL;
	return plq;
}

//入队
void EnLQueue(pLQueue plq,dataType value)
{

	LQueueNode *p=(LQueueNode*)malloc(sizeof(LQueueNode));
	p->data=value;
	p->next=NULL;
	if (plq->rear==NULL)  //处理空队列
	{
		plq->rear=p;
		plq->front=p;
	}
	else{
		plq->rear->next=p;
		plq->rear=p;
	}
}

//自动创建一个链队列(不使用头结点)
pLQueue CreateLQueue()
{
	pLQueue plq=InitLQueue();
	int i=1,num=9;
	while (i<=9)
	{
		cout<<i<<" ";
		EnLQueue(plq,i);
		i++;
	}
	cout<<endl;
	return plq;
}

//出队
void DeLQueue(pLQueue plq)
{
	if (plq->front==NULL)
	{
		cout<<"Empty"<<endl;
		return;
	}
	cout<<plq->front->data<<endl;
	LQueueNode *temp=plq->front;
	plq->front=temp->next;
	free(temp);
}

//全部出队
void AllDeLQueue(pLQueue plq)
{
	while (plq->front!=NULL)
	{
		DeLQueue(plq);
	}
	cout<<"Empty"<<endl;
	return;
}

//释放队列
void FreeLQueue(pLQueue plq)
{
	while (plq->front!=NULL)
	{
		LQueueNode *temp=plq->front->next;
		plq->front->next=temp->next;
		free(temp);
	}
	free(plq);
}

void main()
{
	pLQueue plq=CreateLQueue();
	AllDeLQueue(plq);
	FreeLQueue(plq);
	system("pause");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值