链队列的一系列操作

本文详细介绍了一种链式队列的数据结构实现方法,包括初始化、判断空队、入队、出队、遍历、获取长度、获取队头、清空和销毁队列等核心功能。通过具体代码示例展示了如何在C++中使用链表实现队列,并提供了完整的运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
typedef bool Status;
typedef struct Node
{
	ElemType data;
	Node *next;
}node,*pNode;
typedef struct
{
	pNode front;
	pNode rear;
}queue,*pQueue;
Status Init(pQueue);//初始化队列
Status Isempty(pQueue);//判断队列是否为空
Status Inqueue(pQueue,ElemType);//入队
Status Dequeue(pQueue,ElemType*);//出队并将出队的值带回
void Traverse(pQueue);
int Queuelenth(pQueue);
Status Gethead(pQueue,ElemType*);
Status Clearqueue(pQueue);
Status Destoryqueue(pQueue);
int main()
{
	queue linkqueue;int lenth;

	if(Init(&linkqueue))cout<<"The queue is initiate successful."<<'\n'<<endl;
	else 
	{
		cout<<"Initiating Error!!!"<<endl;
		exit(-1);
	}
	//判断队列是否为空
	if(Isempty(&linkqueue))cout<<"The queue is empty."<<endl;
	else cout<<"The queue is not empty."<<endl;
	//1、2、3、4分别入队
	if(!Inqueue(&linkqueue,1)){cout<<"Inqueue error!!!"<<endl;exit(-1);};
	if(!Inqueue(&linkqueue,2)){cout<<"Inqueue error!!!"<<endl;exit(-1);};
	if(!Inqueue(&linkqueue,3)){cout<<"Inqueue error!!!"<<endl;exit(-1);};
	if(!Inqueue(&linkqueue,4)){cout<<"Inqueue error!!!"<<endl;exit(-1);};
	Traverse(&linkqueue);
	lenth=Queuelenth(&linkqueue);
	cout<<"The lenth of queue is "<<lenth<<"."<<'\n'<<endl;
	//1、2、3  分别出队
	ElemType val;
	if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;
	if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;
	if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;
	Traverse(&linkqueue);
	lenth=Queuelenth(&linkqueue);
	cout<<"The lenth of queue is "<<lenth<<"."<<'\n'<<endl;
	//取队头
	if(Gethead(&linkqueue,&val))cout<<"The front of the queue is "<<val<<"."<<'\n'<<endl;
	else cout<<"The front of the queue maybe not exist or empty."<<'\n'<<endl;
	//清理队列
	if(Clearqueue(&linkqueue))cout<<"The queue is clearing."<<'\n'<<endl;
	else cout<<"The queue maybe not exist."<<'/n'<<endl;
	//销毁队列
	if(Destoryqueue(&linkqueue))cout<<"The queue is destorying"<<'\n'<<endl;
	else cout<<"The queue maybe not exist or empty."<<'\n'<<endl;
}
Status Init(pQueue linkqueue)
{
	linkqueue->front=linkqueue->rear=(pNode)malloc(sizeof(Node));
	if(linkqueue->rear==0)
	{
		return false;
	}
	linkqueue->rear->next=NULL;
	return true;
}
Status Isempty(pQueue linkqueue)
{
	if(linkqueue->rear==linkqueue->front)return true;
	else return false;
}
Status Inqueue(pQueue linkqueue,ElemType val)
{
	pNode pNew=(pNode)malloc(sizeof(Node));
	if(pNew==0)return false;
	pNew->data=val;
	linkqueue->rear->next=pNew;
	linkqueue->rear=pNew;
	linkqueue->rear->next=NULL;
	return true;
}
Status Dequeue(pQueue linkqueue,ElemType* val)
{
	if(!Isempty(linkqueue))
	{
		pNode q=linkqueue->front->next;
		*val=q->data;
		linkqueue->front->next=q->next;
		free(q);
		q=NULL;
	}
	else
	{
		return false;
	}
	return true;
}
void Traverse(pQueue linkqueue)
{
	if(!Isempty(linkqueue))
	{
		pNode p=linkqueue->front->next;
		while(p!=NULL)
		{
			cout<<p->data<<" ";
			p=p->next;
		}
		p=NULL;
	}
	else
	{
		cout<<"The queue is empty.";
	}
	cout<<endl;
}
int Queuelenth(pQueue linkqueue)
{
	int i=0;
	pNode p=linkqueue->front->next;
	while(p!=0)
	{
		i++;
		p=p->next;
	}
	return i;
}
Status Gethead(pQueue linkqueue,ElemType* val)
{
	if(!Isempty(linkqueue))
	{
		*val=linkqueue->front->next->data;
		return true;
	}
	else return false;
}
Status Clearqueue(pQueue linkqueue)
{
	if(!Isempty(linkqueue))
	{
		pNode p=linkqueue->front->next;
		while(p!=0)
		{
			linkqueue->front->next=p->next;	
			free(p);	
			p=linkqueue->front->next;
		}
		linkqueue->rear=linkqueue->front;
		return true;
	}
	else
	{
		return false;
	}
}
Status Destoryqueue(pQueue linkqueue)
{
	if(linkqueue->front!=NULL)
	{
		pNode p=linkqueue->front->next;
		while(p!=0)
		{
			linkqueue->front->next=p->next;	
			free(p);	
			p=linkqueue->front->next;
		}
		free(linkqueue->front);
		linkqueue->rear=linkqueue->front;
		return true;
	}
	else
	{
		return false;
	}
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值