编程实现链式队列

#include<iostream>
using namespace std;

typedef struct Node
{
	char a;
	struct Node *next;
}Lnode;

typedef struct Queue
{
	Lnode *front;
	Lnode *rear;
}LQueue;

//初始化,实参传入a的地址
void Initiate(LQueue *a)
{
	a->front=NULL;
    a->rear=NULL;
}

//添加元素
void AppendQueue(LQueue *a,char b)
{
	Lnode * p;
	p=(Lnode*)malloc(sizeof(Lnode));
	p->a=b;
	p->next=NULL;
	if(a->front==NULL)  a->front=a->rear=p;
	else
	{
		a->rear->next=p;
		a->rear=p;
	}
}

int QueueNotEmpty(LQueue *Q)
{
	if(Q->front==NULL )
		return 0;
	else return 1;
}

//取得队列第一个元素,并删除
char QueueDelete(LQueue *Q)
{
	if(Q->front==NULL) 
	{
		cout<<"the Queue havent elements"<<endl;
    	return NULL;
	}
	Lnode *p;
	char data;
	p=Q->front;
	data=p->a;
	Q->front=Q->front->next;
	if(Q->front==NULL)  Q->rear=NULL;
	free(p);
	return data;
}
//取得队列第一个元素
char QueueGet(LQueue *Q)
{
	if(Q->front==NULL) 
	{cout<<"the Queue is empty"<<endl;
	return NULL;}
	else
	{
		return Q->front->a;
	}
}
//销毁队列
void Destroy(LQueue *Q)
{
	Lnode *p;
	if(Q->front==NULL)  return ;
	while(Q->front!=NULL)
	{
		p=Q->front;
		Q->front=Q->front->next;
		free(p);
	}
}


void main()
{
	LQueue Q;
	char a;
	Initiate(&Q);
	//初始化
	AppendQueue(&Q,'p');
	AppendQueue(&Q,'h');
	AppendQueue(&Q,'a');
	AppendQueue(&Q,'j');
	a=QueueDelete(&Q);//取队头元素并删除
	cout<<a<<endl;
	cout<<QueueGet(&Q)<<endl;//取得队友元素
	Destroy(&Q);//销毁队列
	if(QueueNotEmpty) cout<<"destroy sucessfuly "<<endl;
	else cout<<"destroy unsucessfully"<<endl;
	system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值