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

p2p网站建设教程陇城科技网站建设

p2p网站建设教程,陇城科技网站建设,做属于自己公司的网站,网站主页建设格式队列 队列概念与结构队列的实现头文件Queue.h实现Queue.c队列与结点结构打印初始化销毁入队队列判空出队取队头元素取队尾元素队列有效数据个数 测试文件test.c 概念与结构 概念:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表&am…

队列

  • 队列
    • 概念与结构
    • 队列的实现
      • 头文件`Queue.h`
      • 实现`Queue.c`
        • 队列与结点结构
        • 打印
        • 初始化
        • 销毁
        • 入队
        • 队列判空
        • 出队
        • 取队头元素
        • 取队尾元素
        • 队列有效数据个数
      • 测试文件`test.c`

概念与结构

概念:只允许在⼀端进⾏插⼊数据操作,在另⼀端进⾏删除数据操作的特殊线性表,队列具有先进先出FIFO(First In First Out) 的原则。

⼊队列:进⾏插⼊操作的⼀端称为队尾

出队列:进⾏删除操作的⼀端称为队头

队列底层结构选型

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

队列的实现

头文件Queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>//队列结点结构
typedef int QDataType;
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;//队列结构
typedef struct Queue
{QueueNode* phead;	//队头QueueNode* ptail;	//队尾int size;			//记录有效数据个数
}Queue;
//打印队列
void QueuePrint(Queue* pq);
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestory(Queue* pq);
//入队
void QueuePush(Queue* pq, QDataType x);
//队列判空
bool QueueEmpty(Queue* pq);
//出队
void QueuePop(Queue* pq);
//取队头元素
QDataType QueueFront(Queue* pq);
//取队尾元素
QDataType QueueBack(Queue* pq);
//队列有效数据个数
int QueueSize(Queue* pq);

实现Queue.c

队列与结点结构
//队列结点结构
typedef int QDataType;
typedef struct QueueNode
{QDataType data;struct QueueNode* next;
}QueueNode;//队列结构
typedef struct Queue
{QueueNode* phead;	//队头QueueNode* ptail;	//队尾int size;			//记录有效数据个数
}Queue;
打印
//打印队列
void QueuePrint(Queue* pq)
{assert(pq);QueueNode* pcur = pq->phead;while (pcur){printf("%d ", pcur->data);pcur = pcur->next;}printf("\n");
}
初始化
//初始化
void QueueInit(Queue* pq)
{assert(pq);pq->phead = pq->ptail = NULL;pq->size = 0;
}
销毁
//销毁
void QueueDestory(Queue* pq)
{assert(pq);	QueueNode* pcur = pq->phead;while (pcur){QueueNode* next = pcur->next;free(pcur);pcur = next;}pq->phead = pq->ptail = NULL;
}
入队
//入队
void QueuePush(Queue* pq, QDataType x)
{assert(pq);QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));if (newNode == NULL){perror("malloc fail");exit(1);}newNode->data = x;newNode->next = NULL;//队列为空if (pq->phead == NULL){pq->phead = pq->ptail = newNode;}else{//不为空pq->ptail->next = newNode;pq->ptail = pq->ptail->next;}++pq->size;
}
队列判空
//队列判空
bool QueueEmpty(Queue* pq)
{assert(pq);return pq->phead == 0;
}
出队
//出队
void QueuePop(Queue* pq)
{assert(!QueueEmpty(pq));//出队的是头结点//当队列中只有一个结点时if (pq->phead == pq->ptail){free(pq->phead);pq->phead = pq->ptail = NULL;}else {QueueNode* next = pq->phead->next;free(pq->phead);pq->phead = next;}--pq->size;
}
取队头元素
//取队头元素
QDataType QueueFront(Queue* pq)
{assert(pq);return pq->phead->data;
}
取队尾元素
//取队尾元素
QDataType QueueBack(Queue* pq)
{assert(pq);return pq->ptail->data;
}
队列有效数据个数

可以选择遍历队列,但是使用size次数较多,循环遍历,额外浪费时间
也可以在定义队列结构时,加上int size作为计数器

//队列有效数据个数
//int QueueSize(Queue* pq)
//{
//	int size = 0;
//	QueueNode* pcur = pq->phead;
//	while (pcur)
//	{
//		++size;
//		pcur = pcur->next;
//	}
//	return size;
//}
int QueueSize(Queue* pq)
{return pq->size;
}

测试文件test.c

#define _CRT_SECURE_NO_WARNINGS
#include"Queue.h"void test1()
{Queue q;//初始化QueueInit(&q);//入队QueuePush(&q,1);QueuePush(&q,2);QueuePush(&q,3);QueuePush(&q,4);//打印队列QueuePrint(&q);出队//QueuePop(&q);//QueuePop(&q);//QueuePop(&q);//取队头元素QDataType head = QueueFront(&q);printf("队头元素:%d\n", head);//取队头元素QDataType tail = QueueBack(&q);printf("队尾元素:%d\n", tail);//队列有效数据个数int size = QueueSize(&q);printf("有效数据个数:%d\n", size);//销毁QueueDestory(&q);
}int main()
{test1();return 0;
}

文章转载自:

http://HOPfhQuO.gwmnr.cn
http://17TroYEJ.gwmnr.cn
http://rY3YrgHd.gwmnr.cn
http://cFlfbRss.gwmnr.cn
http://nh4mcv3V.gwmnr.cn
http://OupgmFia.gwmnr.cn
http://UxzqTofq.gwmnr.cn
http://J7yx5Hrh.gwmnr.cn
http://4jVdxPVj.gwmnr.cn
http://sqb6QHzN.gwmnr.cn
http://y2qXq8u4.gwmnr.cn
http://BSi00efx.gwmnr.cn
http://zaiiPF7u.gwmnr.cn
http://KoyZpTDT.gwmnr.cn
http://WiGWviw6.gwmnr.cn
http://rmV3rdUg.gwmnr.cn
http://MNqAvE9m.gwmnr.cn
http://ov193bZ2.gwmnr.cn
http://pLG6wOFn.gwmnr.cn
http://FPrphJyu.gwmnr.cn
http://XHLNoMIa.gwmnr.cn
http://cYaIsyZg.gwmnr.cn
http://fjbRR462.gwmnr.cn
http://TDsqCFtj.gwmnr.cn
http://pNo0JMci.gwmnr.cn
http://C4qXM2Ha.gwmnr.cn
http://ROojAdEH.gwmnr.cn
http://5IQKcmjw.gwmnr.cn
http://tCKkbKri.gwmnr.cn
http://G6q6Wogo.gwmnr.cn
http://www.dtcms.com/wzjs/719053.html

相关文章:

  • 推广网站的方法有搜索引擎wordpress列表页添加页码
  • 电子产品玩具东莞网站建设钢铁网站建设
  • 做自适应网站点击软件
  • 中国建设银行昆山支行网站长春招聘网智联
  • 怎样做后端数据传输前端的网站常德seo招聘
  • 淘宝客网站建设分类商标设计一般多少钱
  • 石家庄网站推广专家长沙免费旅游景点大全
  • php网站建设案例教程行政单位建设网站方案
  • 遵义官网网站建设重庆好玩还是成都好玩
  • 网站消耗流量做健身类小程序的网站
  • 提高审美的网站推荐网站开发团队取什么名字好
  • 自己创建个人免费网站wordpress function
  • 安卓系统上怎样做网站前端开发微信公众号推广网站
  • 茶叶seo网站推广与优化方案会展相关app和网站的建设情况
  • 网站建设技术进行开发网址解析ip地址
  • 站长工具查询入口上海外贸服装尾货市场
  • 手怎么搭建网站wordpress做外贸网站的劣势
  • 北京建设网站活动图片绍兴网站建设冯炳良
  • 网站设计术语营销伎巧
  • 品牌网站建设荐选蝌蚪wordpress前后登录
  • 高校建设主流网站河北正规网站建设比较
  • 专业杭州网站建设微信指数
  • 品牌网站 响应式网站欲思 wordpress
  • 怎么用电脑给域名做网站普通网站和门户网站的区别
  • 怎么做app网站ui原型深圳网站设计推荐柚米
  • 做生存分析的网站做网站要先做商标吗
  • 狠狠做新网站广州越秀发布
  • 哪家企业网站做的好wordpress+4.2.1
  • 做外汇 虚拟网站做网站的员工怎么设置绩效考核
  • 湖州高端网站建设公司seo怎么学