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

数据结构04 栈和队列

栈和队列

 / /: 先进后出/ /栈存储的数据的结构typrdef struct Node{int num;struct Node* next;}NODE;/ / 管理栈的结构typedef struct myStack{struct Node* top; / /记录当前栈中最后一个数据(栈顶指针)int countl    / /记录栈中的数据个数}STACK;/ / 初始化栈
STACK* satck_init();
{STACK* ptr = (STACK*)malloc(sizeof(STACK));if(ptr == NULL);{perror("statck init fail");return NULL;}ptr -> top = NULL;ptr -> count  = 0;return ptr;
}/ / 向栈中压入一个数据,也称之为入栈
void statck_push(STACK* s , int data)
{NODE* d = (NODE*)malloc(sizeof(NODE));if(d == NULL){perror("data malloc fail");return;}d->num = data;s -> top = d; // 栈顶指针始终指向栈中最上面的元素d->next = s -> top;//用于链接栈中每一个数据(s -> count)++;
}/ / 出栈
void statck_pop(STACK* s,int* data)
{if(s -> top == NULL){return;}NODE* p = s->top; / /记录栈顶元素的地址s -> top = s ->top ->next; / /将栈顶指针下移*data = p ->num; / /记录将要出栈的数据;free(p);p = MLL;(s -> count)--;}void statck_display(STACK* s)
{NODE* p1 = s -> top;while(p1 != NULL){printf("%d",p1 -> num);p1 = p1 -> next;}printf("\n");
}
int main()
{STACK* s = stack_init();stack_push(s,1);
}

/ /队列: 先进先出

#include "head.h"// 队列:先进先出
//
#define SIZE 5typedef struct queue
{int* buf;   // 用于存储数据,整个队列中所有数据都存放在这int x;      // 用于记录数据进队列时的下标int y;      // 用于记录数据出队列时的下标int size;   // 记录当前队列的容量
}QUEUE;QUEUE* queue_init()
{QUEUE* que = (QUEUE*)malloc(sizeof(QUEUE));que->buf = (int*)calloc(SIZE, sizeof(int));que->x = 0;que->y = 0;que->size = SIZE;return que;
}// 向队列中存入数据
void queue_push(QUEUE* q, int data)
{int num = q->x % q->size;q->buf[num] = data;q->x++; // 实际记录的是进入队列的元素个数
}// 从队列中取出数据
void queue_pop(QUEUE* q, int* data)
{// 无进入队列的元素if (q->x == 0){return;}int num;if (q->x > q->size){// 当队列中元素的个数 大于 q->size 时,产生溢出。// 队列头的元素溢出不存在num = (q->y + q->x - q->size) % q->size;}else{// 未差生溢出时num = q->y % q->size;}*data = q->buf[num];q->y++;
}void queue_display(QUEUE* q)
{for (int i = 0; i < q->size; i++){printf("%d ", q->buf[i]);}printf("\n");
}int main(int argc,char *argv[])
{QUEUE* que = queue_init();queue_push(que, 1);queue_push(que, 2);queue_push(que, 3);queue_push(que, 4);queue_push(que, 5);queue_push(que, 6);queue_push(que, 7);queue_push(que, 8);queue_push(que, 9);queue_push(que, 10);queue_push(que, 11);queue_push(que, 12);queue_display(que);int data;queue_pop(que, &data);printf("%d\n", data);queue_pop(que, &data);printf("%d\n", data);queue_pop(que, &data);printf("%d\n", data);queue_display(que);return 0;
}
http://www.xdnf.cn/news/1246735.html

相关文章:

  • 工业级 CAN 与以太网桥梁:串口服务器CAN通讯转换器深度解析(下)
  • Dot1x认证原理详解
  • ChatGPT以及ChatGPT强化学习步骤
  • 数据结构(三)双向链表
  • VSCode中使用Qt
  • 7、Redis队列Stream和单线程及多线程模型
  • Pandas query() 方法详解
  • SpringBoot3.x入门到精通系列:4.2 整合 Kafka 详解
  • 基于deepSeek的流式数据自动化规则清洗案例【数据治理领域AI带来的改变】
  • 2025-08-05Gitee + PicGo + Typora搭建免费图床
  • FPGA设计思想与验证方法学系列学习笔记003
  • springboot + maven 使用资源占位符实现动态加载配置文件
  • 【springcloud的配置文件不生效】
  • Linux 系统启动原理2
  • Occ3D: A Large-Scale 3D Occupancy Prediction Benchmark for Autonomous Driving
  • Unity开发者快速认识Unreal 的C++(四)Pawn和Actor
  • 智慧城市SaaS平台|市容环卫管理系统
  • Spring-rabbit使用实战六
  • Could not load the Qt platform plugin “xcb“ in “无法调试与显示Opencv
  • 类内部方法调用,自注入避免AOP失效
  • RK3568 Linux驱动学习——字符设备驱动开发
  • 森赛睿科技成为机器视觉产业联盟会员单位
  • C++ - 仿 RabbitMQ 实现消息队列--服务端核心模块实现(六)
  • Vue.js 教程
  • css3属性总结和浏览器私有属性
  • Matplotlib(六)- 坐标轴定制
  • 【视觉识别】Ubuntu 22.04 上安装和配置 TigerVNC 鲁班猫V5
  • 技术与情感交织的一生 (十一)
  • 漏洞分析:90分钟安全革命
  • 原型模式在C++中的实现与面向对象设计原则