当前位置:网站首页>用单向链表实现 队列
用单向链表实现 队列
2022-07-23 05:44:00 【潜水少年请求出战】
队列特点
我们知道栈是先进后出;而队列是先进先出。
代码实现
.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;
typedef struct QueueNode
{
struct QueueNode* next;
QDataType data;
}QNode;
typedef struct
{
QNode* head;
QNode* tail;
}Queue;
//初始化
void QueueInit(Queue* pq);
//销毁
void QueueDestroy(Queue* pq);
//放数据
void QueuePush(Queue* pq, QDataType x);
//删除
void QueuePop(Queue* pq);
//长度
int QueueSize(Queue* pq);
//取头
QDataType QueueFront(Queue* pq);
//取尾
QDataType QueueBack(Queue* pq);
//判断空
bool QueueEmpty(Queue* pq);
在这里我们可以明显看出我删除和取头的时候我并没有传二级指针或返回改变后的头项的地址。原因在于作者用来一个结构体(包含了头和尾两个结构体指针)避免了这个问题。(我们这样想,我们传一个结构体地址我们就可以改变结构体里面的内容了。)
两个.c文件
#include"queue.h"
//初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
//销毁
void QueueDestroy(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
//放数据
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
//开辟空间
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
//添加数据
newnode->data = x;
newnode->next = NULL;
//链接
if (pq->tail == NULL)
{
pq->head = pq->tail = newnode;
}
else
{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
//判断空
bool QueueEmpty(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}
//删除
void QueuePop(Queue* pq)
{
assert(pq);
assert(!QueueEmpty(pq));
if (pq->head->next == NULL)
{
free(pq->head);
pq->head = pq->tail = NULL;
}
else
{
QNode* next = pq->head->next;
free(pq->head);
pq->head = next;
}
}
//取头
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;
}
//长度
int QueueSize(Queue* pq)
{
assert(pq);
QNode* cur = pq->head;
int count = 0;
while (cur)
{
++count;
cur = cur->next;
}
return count;
}
#include"queue.h"
void tectQueue()
{
Queue Q;
QueueInit(&Q);
QueuePush(&Q, 1);
QueuePush(&Q, 2);
QueuePush(&Q, 3);
QueuePush(&Q, 4);
printf("%d ", QueueFront(&Q));
QueuePop(&Q);
printf("%d ", QueueFront(&Q));
printf("\n打印\n");
while (!QueueEmpty(&Q))
{
printf("%d ", QueueFront(&Q));
QueuePop(&Q);
}
printf("\n");
QueueDestroy(&Q);
}
int main()
{
tectQueue();
return 0;
}
结果展示
边栏推荐
- switch实现表达式计算
- 高电压技术重点知识整理
- 【学习总结】
- Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 3
- How to establish data analysis thinking
- Blog Building II: next theme related settings beta
- 【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】
- 高分子物理考研概念及要点、考点总结
- Use pyod to detect outliers
- Data analysis (I)
猜你喜欢

【AUTOSAR COM 2.通信协议栈进阶介绍】

硬件知識1--原理圖和接口類型(基於百問網硬件操作大全視頻教程)

Baidu Shen Shuo: focus on the scene, deeply cultivate the industry, and bring practical results to enterprise Digitalization

【AUTOSAR CanDrive 1.学习CanDrive的功能和结构】

高分子物理名词解释归纳

Data mining scenario - false invoice

Using pycaret: low code, automated machine learning framework to solve classification problems

把LVGL所有控件整合到一个工程中展示(LVGL6.0版本)

Data analysis (II)
![[AUTOSAR candrive 1. learn the function and structure of candrive]](/img/f6/662512bddab70e75367d212029fc23.png)
[AUTOSAR candrive 1. learn the function and structure of candrive]
随机推荐
堆的实现与堆排序实现
单片机学习笔记7--SysTick定时器(基于百问网STM32F103系列教程)
硬件知識1--原理圖和接口類型(基於百問網硬件操作大全視頻教程)
Opencv library installation path (don't open this)
Enter the triangle side length and calculate the area
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 3
NLP natural language processing - Introduction to machine learning and natural language processing (I)
Uni native plug-in development -- Youmeng one click login
Installation and use of APP automated testing tool appium
博客搭建六:绑定自己域名的方法
常见排序--归并排序(递归和非递归)+计数排序
博客搭建五:图床选择
二叉树的实现-c
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 2
Knowledge structure of advanced algebra
Prometheus
Using pycaret: low code, automated machine learning framework to solve classification problems
With statement
输入三角形边长,求面积
(1)ASIO