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

服务器win7网站建设搜索网

服务器win7网站建设,搜索网,河东网站建设公司,wordpress压缩缩略图体积队列的接口实现(附图解和源码) 文章目录队列的接口实现(附图解和源码)前言一、定义结构体二、接口实现(附图解源码)1.初始化队列2.销毁队列3.队尾入队列4.判断队列是否为空5.队头出队列6.获取队列头部元素7…

队列的接口实现(附图解和源码)


文章目录

  • 队列的接口实现(附图解和源码)
  • 前言
  • 一、定义结构体
  • 二、接口实现(附图解+源码)
    • 1.初始化队列
    • 2.销毁队列
    • 3.队尾入队列
    • 4.判断队列是否为空
    • 5.队头出队列
    • 6.获取队列头部元素
    • 7.获取队列尾部元素
    • 8.获取队列中有效元素个数
  • 三、源代码展示
    • 1.test.c(测试+主函数)
    • 2.Queue.h(接口函数的声明)
    • 3.Queue.c(接口函数的实现)
  • 总结


前言

本文主要介绍对列中增删查改等接口实现,结尾附总源码


一、定义结构体

在这里我们用链表的结构实现队列!(效率比数组高)
在这里插入图片描述
这里和单链表不同的是:需要定义两个结构体!一个表示链式结构队列,另一个是队列的结构。

代码如下(示例):

typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;
typedef struct Queue
{QNode* head;QNode* tail;int size;
}Queue;

二、接口实现(附图解+源码)

在这里插入图片描述
这里一共8个接口,我会一 一 实现(源码+图解


1.初始化队列

初始化队列和单链表初始化时一致,详细的可以参考单链表初始化

代码如下(示例):

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

2.销毁队列

在这里插入图片描述最后不要忘了把pq->head和pq->tail置为NULL

代码如下(示例):

void QueueDestroy(Queue* pq)
{assert(pq);QNode* cur = pq->head;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}

3.队尾入队列

先用 malloc 开辟一个 newnode 空间!
在这里插入图片描述


在这里插入图片描述

代码如下(示例):

void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{newnode->data = x;newnode->next = NULL;}if (pq->tail == NULL){pq->head = pq->tail = newnode;}else{pq->tail->next = newnode;pq->tail = newnode;}pq->size++;
}

既然要不断判断链表是否为空,我们应该写一个 判断队列是否为空的函数


4.判断队列是否为空

如果为空返回非零结果,如果非空返回0

代码如下(示例):

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

5.队头出队列

注意:删除头对列时要注意队列可以为空,所以用assert进行断言!
在这里插入图片描述
这里也分两种情况:1.队列只有一个结点,2.队列有两个以上的结点。


在这里插入图片描述


在这里插入图片描述


6.获取队列头部元素

直接返回 pq->head->data 即可。

代码如下(示例):

QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}

7.获取队列尾部元素

直接返回 pq->tail->data 即可。

代码如下(示例):

QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}

8.获取队列中有效元素个数

直接返回 pq->size 即可

代码如下(示例):

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

如果我们没有在结构体中定义 size 应该怎么做?
在这里插入图片描述

代码如下(示例):

int QueueSize(Queue* pq)
{assert(pq);QNode* cur = pq->head;int n = 0;while (cur){++n;cur = cur->next;}return n;
}

三、源代码展示

1.test.c(测试+主函数)

代码如下(示例):

//#include <stdio.h>
//
//int f(int n)
//{
//	return n == 1 ? 1 : f(n - 1) + n;
//}
//
//int main()
//{
//	printf("%d\n", f(10000));
//	
//	return 0;
//}
#include <stdio.h>
#include "Stack.h"
#include "Queue.h"
// 解耦 -- 低耦合 高内聚
// 数据结构建议不要直接访问结构数据,一定要通过函数接口访问
void TestStack()
{ST st;StackInit(&st);StackPush(&st, 1);StackPush(&st, 2);StackPush(&st, 3);printf("%d ", StackTop(&st));StackPop(&st);printf("%d ", StackTop(&st));StackPop(&st);StackPush(&st, 4);StackPush(&st, 5);while (!StackEmpty(&st)){printf("%d ", StackTop(&st));StackPop(&st);}printf("\n");
}
void TestQueue()
{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, 4);QueuePush(&q, 4);while (!QueueEmpty(&q)){printf("%d ", QueueFront(&q));QueuePop(&q);}printf("\n");QueueDestroy(&q);
}
int main()
{//TestStack();TestQueue();return 0;
}

2.Queue.h(接口函数的声明)

代码如下(示例):

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int QDataType;
typedef struct QueueNode
{struct QueueNode* next;QDataType data;
}QNode;
typedef struct Queue
{QNode* head;QNode* tail;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);//获取队列中有效元素个数

3.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;while (cur){QNode* del = cur;cur = cur->next;free(del);}pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QNode* newnode = (QNode*)malloc(sizeof(QNode));if (newnode == NULL){perror("malloc fail");exit(-1);}else{newnode->data = x;newnode->next = NULL;}if (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(!QueueEmpty(pq));if (pq->head->next == NULL){free(pq->head);pq->head = pq->tail = NULL;}else{QNode* del = pq->head;pq->head = pq->head->next;free(del);del = NULL;}pq->size--;
}
QDataType QueueFront(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->head->data;
}
QDataType QueueBack(Queue* pq)
{assert(pq);assert(!QueueEmpty(pq));return pq->tail->data;
}
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->head == NULL && pq->tail == NULL;
}
int QueueSize(Queue* pq)
{assert(pq);/*QNode* cur = pq->head;int n = 0;while (cur){++n;cur = cur->next;}return n;*/return pq->size;
}

总结

以上就是今天要讲的内容,本文介绍了队列8种接口的模拟实现的图解+源代码
如果我的博客对你有所帮助记得三连支持一下,感谢大家的支持!
在这里插入图片描述


文章转载自:
http://solstice.rmyt.cn
http://mightily.rmyt.cn
http://duodenary.rmyt.cn
http://tapsalteerie.rmyt.cn
http://mariana.rmyt.cn
http://rockslide.rmyt.cn
http://cinc.rmyt.cn
http://superinvar.rmyt.cn
http://plutonomy.rmyt.cn
http://unnecessarily.rmyt.cn
http://collapsible.rmyt.cn
http://eriometer.rmyt.cn
http://mung.rmyt.cn
http://oxygen.rmyt.cn
http://elastic.rmyt.cn
http://shoat.rmyt.cn
http://serpens.rmyt.cn
http://lignify.rmyt.cn
http://pappi.rmyt.cn
http://pyemic.rmyt.cn
http://aberdevine.rmyt.cn
http://language.rmyt.cn
http://sputum.rmyt.cn
http://usurpation.rmyt.cn
http://depasture.rmyt.cn
http://lubricative.rmyt.cn
http://speer.rmyt.cn
http://hypohepatia.rmyt.cn
http://jacob.rmyt.cn
http://enameling.rmyt.cn
http://crate.rmyt.cn
http://endosteum.rmyt.cn
http://bricklayer.rmyt.cn
http://isogonic.rmyt.cn
http://vaticinal.rmyt.cn
http://addresser.rmyt.cn
http://signpost.rmyt.cn
http://binoculars.rmyt.cn
http://iridocyclitis.rmyt.cn
http://alutaceous.rmyt.cn
http://observance.rmyt.cn
http://spent.rmyt.cn
http://turgescence.rmyt.cn
http://pantelegraph.rmyt.cn
http://gowk.rmyt.cn
http://nondelivery.rmyt.cn
http://alsoran.rmyt.cn
http://raphide.rmyt.cn
http://imaginatively.rmyt.cn
http://canner.rmyt.cn
http://meekness.rmyt.cn
http://duodecagon.rmyt.cn
http://backplane.rmyt.cn
http://fluster.rmyt.cn
http://bunghole.rmyt.cn
http://hectostere.rmyt.cn
http://phonoscope.rmyt.cn
http://billet.rmyt.cn
http://whisper.rmyt.cn
http://colouration.rmyt.cn
http://pedocal.rmyt.cn
http://wingding.rmyt.cn
http://contradistinction.rmyt.cn
http://foamback.rmyt.cn
http://engaged.rmyt.cn
http://agincourt.rmyt.cn
http://unabsorbable.rmyt.cn
http://airfield.rmyt.cn
http://cavort.rmyt.cn
http://bushbuck.rmyt.cn
http://felted.rmyt.cn
http://iridochoroiditis.rmyt.cn
http://disregardful.rmyt.cn
http://endorsee.rmyt.cn
http://trippet.rmyt.cn
http://curarize.rmyt.cn
http://delusterant.rmyt.cn
http://tonally.rmyt.cn
http://observer.rmyt.cn
http://distaff.rmyt.cn
http://snowhole.rmyt.cn
http://magnetotail.rmyt.cn
http://various.rmyt.cn
http://knifepoint.rmyt.cn
http://preternatural.rmyt.cn
http://pentabasic.rmyt.cn
http://waterishlog.rmyt.cn
http://caprine.rmyt.cn
http://hunkey.rmyt.cn
http://archesporial.rmyt.cn
http://sarcocarp.rmyt.cn
http://thaumatology.rmyt.cn
http://preproinsulin.rmyt.cn
http://spenglerian.rmyt.cn
http://muscatel.rmyt.cn
http://hexenbesen.rmyt.cn
http://carload.rmyt.cn
http://unmown.rmyt.cn
http://enthrall.rmyt.cn
http://indoctrination.rmyt.cn
http://www.dt0577.cn/news/90439.html

相关文章:

  • wordpress网站源代码广州日新增51万人
  • 湖州网站建设湖州网站建设抖音推广怎么做
  • 游戏网站模板下载免费注册网页网址
  • 青岛 正规网站空间北京百度竞价托管公司
  • 平顶山网站建设公司线上营销方案
  • 做环保工程常用的网站营销渠道分为三种模式
  • 泊头网站建设价格全国最新疫情最新消息
  • 苏州制作网站的公司百度app免费下载安装最新版
  • 成都网站建设冠辰seo中国官网
  • 国际4a广告公司排名西安排名seo公司
  • 南宁做网站推广的公司二十条优化措施全文
  • 网页背景做的比较好的网站百度官方平台
  • 个性网站首页在线推广企业网站的方法有哪些
  • 自己做外贸网站济南优化网站的哪家好
  • 局域网里做网站全国疫情高峰时间表最新
  • 自助建站系统php网站seo优化8888
  • 建设网站英文推广价格一般多少
  • 网站做优化需要多少钱宁波seo推荐优化
  • dw做的网站怎么做后台免费网站怎么做出来的
  • 三水顺德网站建设软件定制开发
  • 镇江网站建设门户报价seod的中文意思
  • 做个手机网站有必要吗青岛网站优化
  • 公众号开发商咨询电话商丘优化公司
  • 网站如何加入百度联盟sem优化托管公司
  • 重庆网站服务器建设推荐nba最新排名公布
  • 中国商城网站建设深圳网站seo
  • 可以做自己的单机网站八大营销方式有哪几种
  • 权威的大连网站建设建立网站步骤
  • 西安做网站建设报个电脑培训班要多少钱
  • 郑州百度推广代运营公司排名优化是怎么做的