队列的基本操作

2023-06-20 0 711

注意事项

1.循环队列有三种判空方法

2.链队没有队满的问题,所以入队的时候无需判断队满

循环队列的判空方法

1.增加额外的一个空间判重,当进队时用(rear+1)%MAXSIZE == front检查

2.定义增加一个size表示队列的出队

3.定义里增加一个标记tag,tag为0出队则队空,tag为1入队则溢出

队列定义

//静态队列
typedef struct {
	int data[MAXSIZE];
	int rear,front;
}SqQueue;

//链队
typedef struct LQNode{
	int data;
	struct LQNode *next;
}LQNode; 

typedef struct LQueue{
	LQNode *front,*rear;	
}*LinkQueue;

操作代码

////静态队列 
//初始化队列
void InitQueue(SqQueue &q){
	q.front = 0;
	q.rear = 0;
} 

//判队空
bool QueueEmpty(SqQueue &q){
	if(q.front == q.rear)return true;
	else return false;
} 

//入队
bool EnQueue(SqQueue &q,int x){
	if(q.rear == MAXSIZE-1)return false;
	q.data[q.rear++] = x;
	return true;
} 

//出队
bool DeQueue(SqQueue &q,int &e){
	if(q.front == q.rear)return false;
	e = q.data[q.front++]; 
	return true;
}

//读取队头元素
bool GetHead(SqQueue q,int &e){
	if(q.front==q.rear)return false;
	e = q.data[q.front]; 
	return true;
} 

////循环队列操作
//初始化队列
void InitCQueue(SqQueue &q){
	q.front = 0;
	q.rear = 0;
}

//判队空
bool QueueCEmpty(SqQueue &q){
	if(q.front == q.rear)return true;
	else return false;
} 

//入队
bool EnCQueue(SqQueue &q,int x){ 
	if((q.rear+1%MAXSIZE) == q.front)return false;
	q.data[q.rear] = x;
	q.rear = (q.rear+1)%MAXSIZE;
	return true;
} 

//出队
bool DeCQueue(SqQueue &q,int &e){
	if(q.front == q.rear) return false;
	e = q.data[q.front];
	q.front = (q.front+1)%MAXSIZE;
	return true;
}

//读取队头元素
bool GetCHead(SqQueue q,int &e){
	if(q.front==q.rear)return false;
	e = q.data[q.front]; 
	return true;
} 

////带头链队操作 
//链队初始化 
void InitLQueue(LinkQueue &q){
	q.front = q.rear = (LQNode*)malloc(sizeof(LQNode));
	q.front->next = NULL;
}

//链队判空
bool EmptyLQueue(LinkQueue &q){
	if(q.front == q.rear)return true;
	else return false;
} 

//链队进队
void EnLQueue(LinkQueue &q,int x){
	LQNode *s = (LQNode*)malloc(sizeof(LQNode));
	s->data = x;
	s->next = NULL;
	q.rear->next = s;
	q.rear = s;
} 

//链队出队
bool DeLQueue(LinkQueue &q,int &e){
	if(q.front == q.rear)return false;
	LNode *s = q.front->next;
	e = q.front->data;
	q.front->next = s->next;
	if(q.front == s){
		q.front = q.rear;
	}
	free(s)
	return true;
} 
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

本站部分资源收集自互联网,我站仅分享内容,仅供用户个人研究,不提供商业使用,如有侵权下架处理,觉得资源内容不错请您在官方渠道购买正版,您在阅读的同时下载的内容本站不承担任何法律责任!Some resources of this website are collected from the Internet. We only share the content, which is only for personal research of users, and not for commercial use. If there is infringement and off shelf processing, please purchase the authentic version of the resource content from the official channel if you think it is good. We will not bear any legal responsibility for the content you download while reading!

橙凰素材-精品源码下载|正版主题更新 学习交流 队列的基本操作 https://b.bqzmz.com/2023/06/20/%e9%98%9f%e5%88%97%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c/

为用户提供极致的体验

上一篇: 栈的基本操作
下一篇: 队列题目
常见问题
  • 正版主题是指站长在指定渠道购买的官方主题,提供作者联系,资源支持,长期更新等等内容
查看详情
  • 非常抱歉,请联系网站页脚的客服,我们将在核实后下架处理并返还给您此资源的收益(会扣去网站运营的费用),谢谢理解!
查看详情
  • 网络资源是站长从互联网分享收集而来的内容,本站不会做测试,直接分享,不包源码可用性,运营性,如有需求点击右边的联系正版,谢谢!
查看详情
  • 如果资源不是最新版本,请在资源下方评论区进行催更或提交工单处理,我们会尽快更新并通过邮箱通知您。
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务