当前位置: 首页 > news >正文

内设网站青岛公司网站制作

内设网站,青岛公司网站制作,咨询公司企业文化,管理咨询服务合同范本前言:上次我们已经学习了数据结构中一个重要的线性表—栈,那么我们这一次就来学习另外一个重要的线性表—队列。 目录: 一、 队列的概念 二、 队列的实现: 1.队列的创建 三、 队列的操作 1.初始化队列 2.队尾入队列 3.队头出队列…

前言:上次我们已经学习了数据结构中一个重要的线性表—栈,那么我们这一次就来学习另外一个重要的线性表—队列。

在这里插入图片描述

目录:

一、
队列的概念
二、
队列的实现:
1.队列的创建
三、
队列的操作
1.初始化队列
2.队尾入队列
3.队头出队列
4.获取队列头部元素
5.获取队列队尾元素
6.获取队列中有效元素个数
7.检测队列是否为空,如果为空返回非零结果,如果非空返回0
8.销毁队列
四、
完整代码展示

队列的概念

队列的概念及结构:队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 入队列:进行插入操作的一端称为队尾 出队列:进行删除操作的一端称为队头。
在这里插入图片描述

队列的实现

队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。
在这里插入图片描述
我们用三个文件来完成对它的操作。

队列的创建:

typedef int QDataType;
// 链式结构:表示队列
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;// 队列的结构
typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;

队列的实现:

队列的初始化:

void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}

队列里的头和尾都为空。

队尾入队列:

void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->val = x;newnode->next = NULL;if (pq->ptail == NULL){pq->ptail = pq->phead = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}

如果我们的队尾元素为空,那么我们的队尾就是newnode,如果我们的队尾不为空,我们的ptail的下一个指向newnode,现在的队尾就为newnode。

队头出队列:

void QueuePop(Queue* pq)
{assert(pq);// assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;if (pq->phead == NULL)pq->ptail = NULL;pq->size--;
}

如果我们直接删除队头元素那么我们就无法访问下一个元素,所以我们先把队头元素保存起来,让现在的队头元素为原来队头元素的下一个元素,在给原来的队头元素删除。

获取队列头部元素:

QDataType QueueFront(Queue* pq)
{assert(pq);// assert(pq->phead);return pq->phead->val;
}

获取队列队尾元素:

QDataType QueueBack(Queue* pq)
{assert(pq);// assert(pq->ptail);return pq->ptail->val;
}

获取队列中有效元素个数:

int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}

size就是我们有效元素的个数,这里返回size就可以了。

检测队列是否为空,如果为空返回非零结果,如果非空返回0:

bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}

队列为空返回0,不为空返回非0,后面测试代码的循环条件就是不为0,就输出,为0就跳出循环。

销毁队列:

void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}

完整代码展示:

Queue.h:

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int QDataType;
typedef struct QueueNode
{QDataType val;struct QueueNode* next;
}QNode;typedef struct Queue
{QNode* phead;QNode* ptail;int size;
}Queue;void QueueInit(Queue* pq);
void QueueDestroy(Queue* pq);
void QueuePush(Queue* pq, QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);
bool QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);

Queue.c:

#include"Queue.h"void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->phead;while (cur){QNode* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");return;}newnode->val = x;newnode->next = NULL;if (pq->ptail == NULL){pq->ptail = pq->phead = newnode;}else{pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}void QueuePop(Queue* pq)
{assert(pq);// assert(pq->phead);QNode* del = pq->phead;pq->phead = pq->phead->next;free(del);del = NULL;if (pq->phead == NULL)pq->ptail = NULL;pq->size--;
}QDataType QueueFront(Queue* pq)
{assert(pq);// assert(pq->phead);return pq->phead->val;
}QDataType QueueBack(Queue* pq)
{assert(pq);// assert(pq->ptail);return pq->ptail->val;
}bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == NULL;
}int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}

代码测试:
test.c:

#include"Queue.h"
int main()
{Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);QueuePush(&q, 3);printf("%d ", QueueFront(&q));QueuePop(&q);printf("%d ", QueueFront(&q));QueuePop(&q);QueuePush(&q, 4);QueuePush(&q, 5);while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}QueueDestroy(&q);return 0;
}

这里我们先入队1,2,3,队头就是1,队尾就是3,我们在出队,先输出1,在把1出队,这样我们就访问2,在输出2之后把2出队,入队4,5,如果我们的队列不为0就输出3,4,5。最后输出的结果如下图:
在这里插入图片描述

相信大家一定可以完美的拿捏队列,感谢各位小伙伴的支持,我们下期再见!

http://www.yayakq.cn/news/864622/

相关文章:

  • 商城网站开发流程微信上的网站怎么做的
  • 化妆品企业网站建设的缺点网络管理系统的特点
  • 网站设计的重要性百度做网站为什么上阿里云备案
  • 做侵权视频网站长泰597人才网最新招聘信息
  • 公司seo是什么级别seo描述是写什么
  • 白云网站制作石家庄
  • wordpress密码进入网站哈尔滨如何免费制作网站
  • 游戏网站建站大学生网站作品
  • 进黑龙江建设网站用哪个浏览器好各种网站的区别
  • 网站的交互设计有什么网站开发社交网络功能的作用
  • 游戏资讯网站怎么做网站系统建设目标范本
  • 歪咪小麻花官方网站怎么做零售wordpress 做手机站
  • 做网站有哪些软件ps网页设计培训班
  • 个人网站建设公wordpress推送百度
  • 查询备案号怎么查询移动端排名优化软件
  • 开天猫旗舰店网站建设电子商务网站建设报告
  • 成都网站快速排名软件国外做蛋糕的网站
  • 河南住房和城乡建设厅网站个人网页内容需要哪些
  • 东莞网站建设部落wordpress rss feed
  • 网站建设费需要列入无形资产吗加工厂网站建设论文
  • 宠物网站设计的代码重庆哪里可以做网站
  • 微网站 价格wordpress文章中写代码
  • 安徽水利建设市场信用信息平台网站阿里巴巴 网站设计
  • 天河岗顶棠下上社网站建设公司合肥建设网站哪个好
  • 济南建网站价格app软件制作网站
  • 廊坊网站建设选择青橙网络做房产网站需要了解什么东西
  • 类似凡科网的网站wordpress 注册 用户名
  • 网站排名不可有利就前怎么注册自己网站吗
  • 百度怎么收录网站装宽带一般多少钱
  • 网站建设公司哪家好智搜宝深圳企业网站建设哪家专业