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

学做网站论坛vip账户北京营销公司比较好的

学做网站论坛vip账户,北京营销公司比较好的,毕设web网站开发,南京网站维护公司有哪些1.队列的概念以及结构 队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFo(Frist in Frist out)的特性 入队列:进行插入才操作的一端称为队尾 出队列:进行删除操作的一…

1.队列的概念以及结构

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出FIFo(Frist in Frist out)的特性

队列:进行插入才操作的一端称为队尾

队列:进行删除操作的一端称为队头

2.队列的实现

队列也可以使用数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会很低

队列常见的基本操作:

//初始化
void QueueInit(Queue* pq);
//清空队列成员
void QueueDestroy(Queue* pq);
//队尾插入元素
void QueuePush(Queue* pq, QDataType x);
//删除队队头元素,队列先进先出
void QueuePop(Queue* pq);
//获取队头元素
int QueueFront(Queue * pq);
//获取队尾元素
int QueueBack(Queue* pq);
//获取队列中有效与元素个数
int QueueSize(Queue* pq);
//查看队列是否为空
bool QueueEmpty(Queue* pq);

每个功能的实现以及解释

实现队列这里我们使用的是动态顺序表

->1.初始化队列

//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}

->2.清空队列成员

//清空队列成员
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;//QNode* cur = pq->head->next;while (cur){/*free(pq->head);pq->head = cur;cur = cur->next;*/QNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;pq->size = 0;
}

->3.队尾插入元素

//队尾插入元素
void QueuePush(Queue* pq, QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (NULL == newnode){perror("QueuePsuh::malloc");return;}newnode->data = x;newnode->next = NULL;if (pq->head == NULL){assert(pq->tail == NULL);pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}

->4.删除队队头元素,队列先进先出

//删除队列成员,队列先进先出
void QueuePop(Queue* pq)
{assert(pq);assert(pq->head != NULL);//第一种方法//Queue* cur = pq->head;//if (cur->next == NULL)//{//	free(cur);//	pq->head = pq->tail = NULL;//}/*else{pq->head = cur->next;free(cur);cur = NULL;}*///第二种方法QNode* next = pq->head->next;free(pq->head);pq->head = next;if (pq->head == NULL){pq->tail = NULL;}pq->size--;
}

->5.获取队头元素

//获取队头成员
int QueueFront(Queue* pq)
{assert(pq);assert(pq->head);return pq->head->data;
}

->6.获取队尾元素

//获取队尾成员
int QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}

->7.获取队列中有效元素个数

//获取队列中有效元素个数
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}

->8.查看队列是否为空

//查看队列是否为空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0; //pq->head == NULL && pq->tail == NULL
}

3.完整代码

Queue.h

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>typedef int QDataType;typedef struct QListNode 
{struct QListNode* next;QDataType data;
}QNode;typedef struct Queue
{QNode* head;QNode* tail;QDataType size;
}Queue;//初始化
void QueueInit(Queue* pq);
//清空队列成员
void QueueDestroy(Queue* pq);
//队尾插入队列
void QueuePush(Queue* pq, QDataType x);
//删除队队头元素,队列先进先出
void QueuePop(Queue* pq);
//获取队头元素
int QueueFront(Queue * pq);
//获取队尾元素
int QueueBack(Queue* pq);
//获取队列中有效与元素个数
int QueueSize(Queue* pq);
//查看队列是否为空
bool QueueEmpty(Queue* pq);

Queue.c

#include "queue.h"//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->head = pq->tail = NULL;pq->size = 0;
}//销毁队列
void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;//QNode* cur = pq->head->next;while (cur){/*free(pq->head);pq->head = cur;cur = cur->next;*/QNode* next = cur->next;free(cur);cur = next;}pq->head = pq->tail = NULL;pq->size = 0;
}//插入队列成员
void QueuePush(Queue* pq, QDataType x)
{QNode* newnode = (QNode*)malloc(sizeof(QNode));if (NULL == newnode){perror("QueuePsuh::malloc");return;}newnode->data = x;newnode->next = NULL;if (pq->head == NULL){assert(pq->tail == NULL);pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}//删除队列成员,队列先进先出
void QueuePop(Queue* pq)
{assert(pq);assert(pq->head != NULL);//Queue* cur = pq->head;//if (cur->next == NULL)//{//	free(cur);//	pq->head = pq->tail = NULL;//}/*else{pq->head = cur->next;free(cur);cur = NULL;}*/QNode* next = pq->head->next;free(pq->head);pq->head = next;if (pq->head == NULL){pq->tail = NULL;}pq->size--;
}//获取队头成员
int QueueFront(Queue* pq)
{assert(pq);assert(pq->head);return pq->head->data;
}//获取队列中有效元素个数
int QueueSize(Queue* pq)
{assert(pq);return pq->size;
}//获取队尾成员
int QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}//查看队列是否为空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0; //pq->head == NULL && pq->tail == NULL
}

test.c

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

测试结果:

http://www.hrbkazy.com/news/54950.html

相关文章:

  • wordpress博客有手机版百度竞价关键词优化
  • 软件开发专业技能抖音seo排名
  • 网站模板制作流程网站快速排名优化
  • 网页素材下载合肥网络公司seo建站
  • 广州网站建设设计厂家2020年关键词排名
  • 北京开公司一年费用seo诊断报告
  • 公司网站简介深圳网站设计公司排行
  • 专门做服装批发的网站吗东莞网站快速排名提升
  • 一个网站做局打水百度网盘官网网页版
  • 企业建设网站的目的是的搜索引擎优化
  • 网站程序超市百度引擎
  • 没有网站可以做cpa广告么搜索引擎关键词怎么选
  • 网站开发 承接手机网页制作
  • 网架加工安装一体的公司seo职业培训学校
  • 大丰做网站找哪家好网站访问量查询工具
  • 公司网站开发怎么做账市场营销计划书模板
  • wordpress修改版权seo优化方案项目策划书
  • 网站栏目怎么做单独的搜索框外贸企业网站推广
  • 新格建站湖北网络推广公司
  • wordpress网站恢复搜狗搜索网
  • 青岛城市建设集团网站上海网络推广公司网站
  • 濮阳网站怎么做seo优化大师软件下载
  • 仿牌外贸网站建设网络营销与直播电商怎么样
  • 做网站就上微赞网windows优化大师官网
  • 可信网站认证必须做国家职业技能培训学校
  • 北京市网络科技有限公司seo关键词如何布局
  • 网站建设服务包括什么seo是指什么职位
  • 温州网站推广cilimao磁力猫搜索引擎
  • 顺义网站建设公司专业网站推广软件
  • 机械厂做网站关键词优化排名要多少钱