数据结构之链队实现

数据结构之链队实现

代码及思想

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef int ElemType;
#define Status int 
#define ERROR 0
#define OK 1
/***************结构体定义***************/
typedef struct LNode
{
	int data;//数据域
	LNode *next;//指向下一个结点的指针域 
}LNode; 

typedef struct
{
	LNode *front,*rear;
	int length;
}LinkQueue;

/******************函数声明******************/
Status LinkQueueInit(LinkQueue &Q);//链队初始化
bool IsEmpty(LinkQueue &Q);//判断链队是否为空 
Status enQueue(LinkQueue &Q,ElemType x);//x入队
Status deQueue(LinkQueue &Q,ElemType &x);//出队队顶并赋值给x
int LinkQueueLength(LinkQueue &Q);//返回队的长度,即元素个数 
void LinkQueueTraverse(LinkQueue &Q);//遍历链队 
void LinkQueueDestroy(LinkQueue &Q);//销毁链队 

/******************函数实现******************/
Status LinkQueueInit(LinkQueue &Q)//链队初始化
{
	Q.front = Q.rear = (LNode *)malloc(sizeof(LNode));
	Q.rear->next = NULL;
	Q.length = 0;
}

bool IsEmpty(LinkQueue &Q)//判断链队是否为空 
{
	if(Q.front == Q.rear)
		return true;
	return false;
}

Status enQueue(LinkQueue &Q,ElemType x)//x入队
{
	LNode *node = (LNode *)malloc(sizeof(LNode));
	node->data = x;
	cout<<"#入队成功,入队结点数据为:"<<x<<endl;
	node->next = NULL;
	Q.rear->next = node;
	Q.rear = node;//更新队尾 
	Q.length++;
	return OK;
}

Status deQueue(LinkQueue &Q,ElemType &x)//出队队顶并赋值给x
{
	if(IsEmpty(Q))
	{
		cout<<"#队列为空,出队失败"<<endl;
		return ERROR;
	}
	LNode *node = Q.front->next;//记录要释放的队头结点,front不指向数据结点,即头结点无数据 
	x = node->data;
	cout<<"#出队成功,出队结点数据为:"<<x<<endl;
	Q.front->next = node->next;
	free(node);	
	Q.length--; 
	return OK;
} 

int LinkQueueLength(LinkQueue &Q)//返回队的长度,即元素个数
{
	return Q.length;	
} 

void LinkQueueTraverse(LinkQueue &Q)//遍历链队 
{
	LNode *node = Q.front->next;
	while(node!=NULL)
	{
		cout<<node->data<<" ";
		node = node->next;
	}
	cout<<endl;
}

void LinkQueueDestroy(LinkQueue &Q)//销毁链队
{
	if(Q.front == Q.rear)
	{
		cout<<"#队列为空"<<endl;
	}
	LNode *p = Q.front;
	LNode *temp;
	while(p!=NULL)
	{
		temp = p;
		p = p->next;
		free(temp);
	}
} 
/*
Status LinkQueueInit(LinkQueue &Q);//链队初始化
bool IsEmpty(LinkQueue &Q);//判断链队是否为空 
Status enQueue(LinkQueue &Q,ElemType x);//x入队
Status deQueue(LinkQueue &Q,ElemType &x);//出队队顶并赋值给x
int LinkQueueLength(LinkQueue &Q);//返回队的长度,即元素个数 
void LinkQueueTraverse(LinkQueue &Q);//遍历链队 
void LinkQueueDestroy(LinkQueue &Q);//销毁链队 
*/
/******************主函数验证******************/
int main()
{
	ElemType denum;
	LinkQueue Q;
	LinkQueueInit(Q);
	cout<<"*1、初始化队列数据为1-10"<<endl;
	for(int i=1;i<11;i++)
	{
		enQueue(Q,i);
	}
	cout<<"#此时队列中数有:"<<endl;
	LinkQueueTraverse(Q);
	cout<<"#此时队列长度为:"<<LinkQueueLength(Q)<<endl;
	cout<<endl;
	
	cout<<"*2、向队列中插入数据2019"<<endl;
	enQueue(Q,2019);
	cout<<"#此时队列中数有:"<<endl;
	LinkQueueTraverse(Q);
	cout<<"#此时队列长度为:"<<LinkQueueLength(Q)<<endl;
	cout<<endl;
	
	cout<<"*3、队列出队两次"<<endl;
	deQueue(Q,denum);
	deQueue(Q,denum);
	cout<<"#出队的第二个元素为:"<<denum<<endl;
	cout<<"#此时队列中数有:"<<endl;
	LinkQueueTraverse(Q);
	cout<<"#此时队列长度为:"<<LinkQueueLength(Q)<<endl;
	cout<<endl;
	
	cout<<"*4、去向队列中插入数据111、222、333、444、,不会越界(链表无界)"<<endl;
	deQueue(Q,denum);deQueue(Q,denum);
	enQueue(Q,111);enQueue(Q,222);enQueue(Q,333);enQueue(Q,444);
	cout<<"#此时队列中数有:"<<endl;
	LinkQueueTraverse(Q);
	cout<<"#此时队列长度为:"<<LinkQueueLength(Q)<<endl;
	cout<<endl;
	 
	LinkQueueDestroy(Q);
	return 0;
}

测试截图

链队测试截图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李霁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值