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

中国建筑官网站网上宣传方法有哪些

中国建筑官网站,网上宣传方法有哪些,网站制作建设案例,中国风风格网站模板数据结构——队列 一、队列的概念二、队列的实现方式三、队列所需要的接口四、接口的详细实现4.1初始化4.2销毁4.3入队4.5出队4.6获取队头元素4.7获取队尾元素4.8获取队列元素个数4.9判空 五、完整代码5.1Queue.h5.2Queue.c5.3test.c 一、队列的概念 队列:只允许在…

数据结构——队列

  • 一、队列的概念
  • 二、队列的实现方式
  • 三、队列所需要的接口
  • 四、接口的详细实现
    • 4.1初始化
    • 4.2销毁
    • 4.3入队
    • 4.5出队
    • 4.6获取队头元素
    • 4.7获取队尾元素
    • 4.8获取队列元素个数
    • 4.9判空
  • 五、完整代码
    • 5.1Queue.h
    • 5.2Queue.c
    • 5.3test.c

一、队列的概念

队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,
队列具有先进先出FIFO(First In First Out) 
入队列:进行插入操作的一端称为队尾 。
出队列:进行删除操作的一端称为队头。

在这里插入图片描述

二、队列的实现方式

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

三、队列所需要的接口

//初始化
void QueueInit(Queue* pq);//销毁
void QueueDestory(Queue* pq);//入队
void QueuePush(Queue* pq, Queuedatatype x);//出队
void QueuePop(Queue* pq);//获取队头元素
Queuedatatype QueueFront(Queue* pq);//获取队尾元素
Queuedatatype QueueBack(Queue* pq);//获取队列元素个数
int Queuesize(Queue* pq);//判空
bool QueueEmpty(Queue* pq);

四、接口的详细实现

4.1初始化

初始化:我们需要将pq->phead和pq->ptail都置为NULL,并且将pq->size置为0;

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

4.2销毁

销毁:首先定义一个cur指针保存头节点phead的地址,接下来利用cur!=NULL使得循环往下走,在循环内定义一个next的指针来更新地址,并且用free来释放内存,出循环后,将pq->phead,pq->ptail都置为NULL,并且将pq->size置为0。

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

在这里插入图片描述

4.3入队

入队:入队有两种情况。第一种,队内无其他元素;第二种,队内有其他元素。
①、队内无其他元素:直接让pq->phead = pq->ptail = newnode;
在这里插入图片描述
②、队内有其它元素:如果队列不为NULL,我们需要让pq->ptail->next指向newnode,并且最后再让pq->ptail指向newnode.
在这里插入图片描述

oid QueuePush(Queue* pq, Queuedatatype x)
{assert(pq);Queuenode* newnode = (Queuenode*)malloc(sizeof(Queuenode));if (newnode == NULL){perror("malloc fail");return;}newnode->data = x;newnode->next = NULL;//队内无其它元素if (pq->phead == NULL){assert(pq->ptail == NULL);pq->phead = pq->ptail = newnode;}else{//链接pq->ptail->next = newnode;pq->ptail = newnode;}pq->size++;
}

4.5出队

出队:出队列大体上分为两种情况:有节点和无节点。
①、如果队列中没有节点,就不能进行出队操作,我们这时可以用assert(!QueueEmpty(pq)); 来进行判断。
②、队列中有节点时,又可以分为一个节点和多个节点之分,如果队列中只有一个节点时,我们直接用free 置空;如果队列中有多个节点时,首先、创建一个next用来保存phead的下一个节点的地址,我们free(phead),再让phead等于我们的next。

// 出队
void QueuePop(Queue * pq)
{assert(pq);assert(!QueueEmpty(pq));//一个节点if (pq->phead->next == NULL){free(pq->phead);pq->phead = pq->ptail=NULL;}//多个节点else{//头删Queuenode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}

4.6获取队头元素

//获取队头元素
Queuedatatype QueueFront(Queue* pq)
{assert(pq);return pq->phead->data;
}

4.7获取队尾元素

//获取队尾元素
Queuedatatype QueueBack(Queue* pq)
{assert(pq);return pq->ptail->data;
}

4.8获取队列元素个数

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

4.9判空

如果pq->size==0时,便证明队列为NULL。

//判空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}

五、完整代码

5.1Queue.h

#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>typedef int Queuedatatype;
//定义队列结构
typedef struct Queuenode
{struct Queuenode* next;Queuedatatype data;
}Queuenode;typedef struct Queue
{Queuenode* phead;Queuenode* ptail;int size;
}Queue;//初始化
void QueueInit(Queue* pq);//销毁
void QueueDestory(Queue* pq);//入队
void QueuePush(Queue* pq, Queuedatatype x);//出队
void QueuePop(Queue* pq);//获取队头元素
Queuedatatype QueueFront(Queue* pq);//获取队尾元素
Queuedatatype QueueBack(Queue* pq);//获取队列元素个数
int Queuesize(Queue* pq);//判空
bool QueueEmpty(Queue* pq);

5.2Queue.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->phead = NULL;pq->ptail = NULL;pq->size = 0;
}//销毁
void QueueDestory(Queue* pq)
{assert(pq);Queuenode* cur = pq->phead;while (cur){Queue* next = cur->next;free(cur);cur = next;}pq->phead = pq->ptail = NULL;pq->size = 0;
}//入队
void QueuePush(Queue* pq, Queuedatatype x)
{assert(pq);Queuenode* newnode = (Queuenode*)malloc(sizeof(Queuenode));if (newnode == NULL){perror("malloc fail");return;}newnode->data = x;newnode->next = NULL;if (pq->phead == NULL){assert(pq->ptail == NULL);pq->phead = pq->ptail = newnode;}//链接pq->ptail->next = newnode;pq->ptail = newnode;pq->size++;
}// 出队
void QueuePop(Queue * pq)
{assert(pq);assert(!QueueEmpty(pq));//一个节点if (pq->phead->next == NULL){free(pq->phead);pq->phead = pq->ptail=NULL;}//多个节点else{//头删Queuenode* next = pq->phead->next;free(pq->phead);pq->phead = next;}pq->size--;
}//获取队头元素
Queuedatatype QueueFront(Queue* pq)
{assert(pq);return pq->phead->data;
}//获取队尾元素
Queuedatatype QueueBack(Queue* pq)
{assert(pq);return pq->ptail->data;
}//获取队列元素个数
int Queuesize(Queue* pq)
{assert(pq);return pq->size;
}//判空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->size == 0;
}

5.3test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"
void test1()
{Queue q;QueueInit(&q);QueuePush(&q, 1);QueuePush(&q, 2);QueuePush(&q, 3);QueuePush(&q, 4);printf("Size:%d\n", Queuesize(&q));while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}}
int main()
{test1();return 0;
}
http://www.15wanjia.com/news/46939.html

相关文章:

  • 北京想象力网站建设如何自己建一个网站
  • 企业网站首页设计与制作哪里有免费的网站推广软件
  • 玉溪网站建设facebook海外推广
  • 武汉市网站建设百度自媒体怎么注册
  • 京东做代码的网站企业网站建设报价
  • 扬州有做义工的地方或网站嘛百度移动权重
  • 用jsp怎么做网站关键词优化报价查询
  • win7自己电脑做网站品牌网络营销策划
  • wordpress弹幕功能seo厂商
  • 做网站会犯法吗上海seo推广
  • wordpress慢6品牌seo是什么
  • 怎么做网盘网站网络推广外包一年多少钱
  • 网站业务员怎么给客户做方案百度推广开户费用标准
  • 帝国cms做视频网站性能如何百度关键词查询
  • 如何做代购网站设计营销对企业的重要性
  • 杭州seo关键词优化哪家好seo管理与优化期末试题
  • 买虚机送网站建设网站优化是什么
  • 做网站哪家专业广州seo招聘
  • 微擎可以做网站吗最简单的网页制作
  • 专业建站公司加盟俄罗斯搜索引擎入口
  • web网站开发源代码店铺数据分析主要分析什么
  • 零基础学做网站的书网络营销论坛
  • 学做美食交流网站企业网站快速建站
  • 浙江舟山疫情通报seo黑帽教程视频
  • 国税网站建设现状软文推广代写代发
  • 做网站什么软件好最全资源搜索引擎
  • 有什么网站可以做商品展示的吗购买域名
  • WordPress生成网站地图百中搜优化软件靠谱吗
  • 淮安网站建设方案常见的营销方式有哪些
  • 如何检查网站是否被挂木马google adsense