news 2026/5/15 12:59:10

数据结构(栈和队列)

作者头像

张小明

前端开发工程师

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/5/15 12:58:50

智能体的感知与理解技术

《AI Agent智能体开发实践玩转FastGPT 像搭积木一样构建智能体 LLM大语言模型AI Agent开发 智能体性能优化调试部署实施方法书籍 AIAgent智能体开发实践 无规格》【摘要 书评 试读】- 京东图书 AI智能体的感知技术是其与外部世界交互的“感官系统”&#xff0c;旨在将物理世界…

作者头像 李华
网站建设 2026/5/15 12:58:51

医院不良事件的分类与影响分析

不良事件报告系统是医疗机构用于收集、分析医疗安全事件的标准化管理工具。该系统通过非惩罚性、主动报告原则&#xff0c;在24&#xff5e;48小时内识别安全隐患&#xff0c;旨在改善系统流程并防范事件复发。根本目的&#xff1a;发现安全问题&#xff0c;进行根因分析&#…

作者头像 李华
网站建设 2026/5/15 12:58:12

出海项目踩坑实录:服务器配置拉满,网站还是慢?

很多刚开始做出海项目的人&#xff0c;都会犯一个几乎一模一样的错误&#xff1a;CPU 拉满、内存拉满、SSD 拉满&#xff0c;结果网站还是慢。我自己、身边的朋友、还有不少客户&#xff0c;都踩过这个坑。 今天这篇文章&#xff0c;就从一次真实的出海项目经历&#xff0c;聊聊…

作者头像 李华
网站建设 2026/5/15 12:58:43

别甩锅给EasyGBS!VLC播不了FLV流?竟是H.265不兼容,用它秒解决

最近又收到用户反馈&#xff1a;“明明在EasyGBS上能正常看到FLV格式的视频&#xff0c;转到VLC播放器就加载失败&#xff0c;这是平台出问题了吗&#xff1f;”其实每次看到这类咨询&#xff0c;我都想跟大家说一句&#xff1a;“真不是EasyGBS的锅&#xff01;” 今天就用大白…

作者头像 李华
网站建设 2026/5/15 11:43:46

【Java毕设全套源码+文档】基于springboot的高校学生奖助学金系统设计与实现(丰富项目+远程调试+讲解+定制)

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华
网站建设 2026/5/9 1:34:52

60、Linux网络软中断与数据包收发机制详解

Linux网络软中断与数据包收发机制详解 1. Linux内核软中断概述 Linux内核2.4支持四种内置软中断,每种软中断都有其特定的用途: - HI_SOFTIRQ :用于处理高优先级任务,例如定时器小任务。 - NET_TX_SOFTIRQ :处理网络传输中断。 - NET_RX_SOFTIRQ :处理网络接收…

作者头像 李华