news 2026/1/3 17:49:30

数据结构(栈和队列)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
数据结构(栈和队列)

一、栈

用数组实现栈

#include <stdio.h> #define MaxSize 5 typedef struct Stack{ int data[MaxSize]; int pre; }Stack; //初始栈 void Init(Stack *stack){ stack->pre = -1; } //入栈操作 void Push(Stack *stack,int x){ //判断栈是否已满 if(stack->pre == MaxSize-1){ printf("栈已满\n"); return; } stack->pre = stack->pre+1;//指针向上走一位 stack->data[stack->pre] = x; } //出栈操作 void Pop(Stack *stack){ if(stack->pre == -1){ printf("栈已空\n"); return; } printf("%d\n",stack->data[stack->pre]); stack->pre = stack->pre-1; } int main() { Stack stack; Init(&stack); Push(&stack,5); Push(&stack,7); Push(&stack,4); Push(&stack,2); Pop(&stack); Pop(&stack); Pop(&stack); Pop(&stack); return 0; }

用链表实现栈

#include <stdio.h> #include <stdlib.h> typedef struct Node{ int data; struct Node *next; }LinkNode; //初始化链表(栈)的头节点 void Init(LinkNode *node){ node->next = NULL;//头节点的next初始化为NULL,表示栈为空 } //入栈操作(在链表头部插入新节点) void Push(LinkNode *head,int value){ //创建新的节点 LinkNode *newNode = (LinkNode *)malloc(sizeof(LinkNode)); newNode->data = value; newNode->next = NULL; //将新节点插入到头部(栈顶) newNode->next = head->next; head->next = newNode; } //出栈操作(删除链表头部节点并打印其值) void Pop(LinkNode *head){ if(head->next == NULL){//栈为空是直接返回 return; } LinkNode *temp = head->next;//临时保存栈顶节点 printf("%d\n",temp->data);//打印栈顶元素 head->next = temp->next;//移除栈顶节点 free(temp); } int main() { LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode));//为头节点分配内存 Init(head);//初始化头节点 Push(head,1); Push(head,2); Push(head,3); Pop(head); Pop(head); return 0; }

二、队列

用数组实现队列

#include <stdio.h> #define MaxSize 5 typedef struct Queue{ int arr[MaxSize]; int r; //出 int p; //入 }Queue; void init(Queue *queue){ queue->r = -1; queue->p = -1; } //数据添加 void insert(Queue *queue,int value){ if(queue->p - queue->r == MaxSize){ printf("队列已满\n"); return; } queue->p= queue->p+1; queue->arr[queue->p] = value; } //取出数据 void chu(Queue *queue){ if(queue->p - queue->r == 0){ printf("队列已空\n"); return; } queue->r= queue->r+1; printf("%d\n",queue->arr[queue->r]); } int main(){ Queue queue; init(&queue); insert(&queue,5); insert(&queue,7); insert(&queue,4); insert(&queue,2); insert(&queue,0); insert(&queue,3); chu(&queue); chu(&queue); chu(&queue); chu(&queue); chu(&queue); chu(&queue); }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/3 4:14:59

2025年五大AI Wiki系统横评:从功能到场景的深度解析

在知识管理领域&#xff0c;Wiki系统早已从简单的文档存储工具进化为集创作、协作、智能问答于一体的知识中枢。2025年&#xff0c;随着AI技术的深度整合&#xff0c;新一代Wiki系统正在重塑企业知识管理的方式。本文将深度对比当前市场上五款主流AI Wiki系统&#xff0c;帮助您…

作者头像 李华
网站建设 2025/12/30 6:41:22

信创环境下的 “构建” 之痛:如何解决复杂项目依赖管理与制品库的国产化适配难题?

在信创改造的全流程中&#xff0c;“构建环节” 是连接研发与交付的核心枢纽&#xff0c;却常因依赖关系错综复杂、制品库国产化适配不足陷入效率瓶颈与合规风险。当企业将传统架构迁移至麒麟 / 统信操作系统、鲲鹏 / 飞腾芯片、达梦 / OceanBase 数据库等国产环境时&#xff0…

作者头像 李华
网站建设 2025/12/29 14:37:13

EasyGBS解锁公共场所视频监控新模式

在智慧城市建设加速推进的背景下&#xff0c;公共场所作为人员密集、流动性强的核心场景&#xff0c;其安全防控、秩序管理、应急处置需求日益严苛。视频监控作为公共场所安全保障的“眼睛”&#xff0c;已从传统的“事后追溯”向“事前预警、事中干预”升级。而国标GB28181算法…

作者头像 李华
网站建设 2026/1/2 17:14:40

56、IP 过滤与防火墙技术解析

IP 过滤与防火墙技术解析 1. IP 过滤基础 在网络通信中,IP 过滤和防火墙起着至关重要的作用。TRACK 和 NF_IP_PRI_NAT_DST 可以针对相同的钩子编号和协议族进行注册,这意味着它们能够依据优先级存在于同一链中。其中,优先级为 NF_IP_PRI_CONNTRACK 的钩子会首先被处理,因…

作者头像 李华
网站建设 2026/1/3 14:18:37

47、Linux内核路由表与缓存的实现及管理

Linux内核路由表与缓存的实现及管理 1. 路由查找与源IP选择 当路由查找没有返回结果时,会在第1180行直接调用 inet_select_addr() 函数,以找到具有通用范围的源IP(同时也会使用该路由的网关信息)。这是因为出于管理原因,接口可能配置了不同的源IP。最后在第1182行,将识…

作者头像 李华
网站建设 2026/1/3 17:30:48

物理化学数学国际期刊征稿

期刊名称: 现代物理学报ISSN印刷版: 3078-9443研究领域: 物理 出版周期: 1-2个月出刊检索版面要求: 5000-12000字符/篇期刊收录: 维普VIP 出版社: 香港✥世纪中文期刊名称: 物理科学与技术研究ISSN印刷版: 2755-1075研究领域: 物理 出版周期: 6-7个月出刊检索版面要求: 5000…

作者头像 李华