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

队列的实现(单链表)

目录

一、queue.h(头文件)

二、queue.c(调用函数)

三、test.c(主程序)

一、queue.h(头文件)

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>typedef int QDataType;
typedef struct Queuenode
{QDataType a;struct Queuenode* next;
}Qnode;typedef struct Queue
{Qnode* tail;Qnode* head;int size;
}Queue;//初始化队列
void QueueInit(Queue* ps);
//销毁队列
void QueueDestroy(Queue* ps);
//入队列
void Queuepush(Queue* ps, QDataType x);
//出队列
void Queuepop(Queue* ps);
//查看队列中的元素个数
int Queuesize(Queue* ps);
//判断队列是否为空
bool QueueEmpty(Queue* ps);
//查看队首
QDataType QueueFront(Queue* ps);
//查看队尾
QDataType QueueBack(Queue* ps);

二、queue.c(调用函数)

#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"//创建新节点
Qnode* Creatnode(QDataType x)
{Qnode* node = (Qnode*)malloc(sizeof(Qnode));if (node == NULL){perror("malloc:");return NULL;}node->a = x;node->next = NULL;return node;
}//初始化
void QueueInit(Queue* ps)
{assert(ps);ps->head = NULL;ps->size = 0;ps->tail = NULL;
}//判断队列是否为空
bool QueueEmpty(Queue* ps)
{assert(ps);return ps->size == 0;
}//销毁队列
void QueueDestroy(Queue* ps)
{assert(ps);Qnode* cur = ps->head;while (cur){Qnode* next = cur->next;free(cur);cur = next;}ps->head = ps->tail = NULL;ps->size = 0;
}//入队列
void Queuepush(Queue* ps, QDataType x)
{assert(ps);Qnode* newnode = Creatnode(x);if (ps->head == NULL){ps->head = ps->tail = newnode;}else{ps->tail->next = newnode;ps->tail = newnode;}ps->size++;
}//出队列
void Queuepop(Queue* ps)
{assert(ps);//断言看ps里面是否有数据,非空执行,空的话报错assert(!QueueEmpty(ps));if (ps->head == ps->tail){free(ps->head);ps->head = ps->tail = NULL;ps->size--;}else{Qnode* cur = ps->head;ps->head = ps->head->next;free(cur);cur = NULL;ps->size--;}
}
//查看队列中的元素个数
int Queuesize(Queue* ps)
{assert(ps);return ps->size;
}//查看队首
QDataType QueueFront(Queue* ps)
{assert(ps);assert(!QueueEmpty(ps));return ps->head->a;
}
//查看队尾
QDataType QueueBack(Queue* ps)
{assert(ps);assert(!QueueEmpty(ps));return ps->tail->a;
}

三、test.c(主程序)

#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"void Print(Queue* ps)
{printf("队头数据为->%d\n", QueueFront(ps));printf("队尾数据为->%d\n", QueueBack(ps));
}
int main()
{//创建队列Queue queue;//初始化QueueInit(&queue);//入队列Queuepush(&queue, 1);Queuepush(&queue, 2);Queuepush(&queue, 3);Queuepush(&queue, 4);Print(&queue);//出队列Queuepop(&queue);//查看队列中的元素个数int n = Queuesize(&queue);printf("%d\n", n);Print(&queue);Queuepush(&queue, 5);n = Queuesize(&queue);printf("%d\n", n);Print(&queue);//销毁队列QueueDestroy(&queue);return 0;
}

http://www.mrgr.cn/news/60332.html

相关文章:

  • NSSCTF刷题篇web部分
  • Typora一款极简Markdown文档编辑器和阅读器,实时预览,序列号生成!免费!最新可用!
  • 顺序表总结
  • uniapp 引入了uview-ui后,打包错误,主包过大解决方案
  • 【Linux】线程池详解及其基本架构与单例模式实现
  • H3m-Blog
  • 重采样方法(交叉验证法)——基于glm与LOOCV法(Weekly数据集分析)
  • C++-继承
  • 快速上手 Rust——环境配置与项目初始化
  • 天地图实现海量聚合marker--uniapp后端详细实现
  • 2024年推荐最详解析项目管理平台与敏捷开发的五个阶段
  • Redis内部数据结构Dict结构详解
  • DevTools 中的新滚动徽章
  • mac下使用docker安装php7.4环境
  • Spring MVC:响应结果和设置
  • Python Flask内存泄漏分析定位
  • Vue前端开发:数据绑定方法
  • php伪协议和move_uploaded_file、rename、copy等文件操作
  • 从0到1,搭建vue3项目
  • 单应性矩阵与相机内外参之间的关系
  • [Mv]_× = M [v]_× M^T的证明
  • flowable 去掉自带的登录权限
  • 基本法代码阅读
  • UDS诊断刷写–跳转执行
  • Sigrity Power SI Noise coupling analysis模式如何观测PDN系统的交流耦合噪声以及平面间电压波动操作指导(二)
  • 未定义项目JDK 解决办法