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

数据结构(栈和队列)

一、栈

用数组实现栈

#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); }
http://www.cnnetsun.cn/news/63260.html

相关文章:

  • 2025年五大AI Wiki系统横评:从功能到场景的深度解析
  • 信创环境下的 “构建” 之痛:如何解决复杂项目依赖管理与制品库的国产化适配难题?
  • EasyGBS解锁公共场所视频监控新模式
  • 56、IP 过滤与防火墙技术解析
  • 47、Linux内核路由表与缓存的实现及管理
  • 物理化学数学国际期刊征稿
  • 好写作AI:给你的键盘装上“三头六臂”
  • 好写作AI:你的赛博翻译官,让中文写作秒变国际范儿!
  • 好写作AI:别让“逻辑刺客”背刺你的论文!用AI练就“最强嘴替”
  • 新型高级钓鱼工具包利用AI与MFA绕过技术大规模窃取凭证
  • 快造Snapmaker U1测评:让人眼前一亮的四头3D打印机,重新定义多色
  • 管家婆辉煌软件账套开账前需要录入哪些信息
  • 绕过 Web 应用程序防火墙 (WAF) 的 5 种方法
  • 中国AI创新被低估了吗?
  • 【数据操作与可视化】Serborn绘图-类别散点图和热力图
  • 你的RAG为什么总答非所问?问题可能出在混淆了“语义理解”与“语义检索”!
  • PDF文本提取的“杀手锏”!DeepSeek-OCR+Python,让表格、段落分毫不差!
  • 万能电子画册源码系统,打造专业级在线展示平台
  • ADC的采样频率对于信号检测的影响
  • 36、函数式输入输出编程指南
  • 41、函数式解决常见问题及 XML 读取程序的函数式转换
  • 揭秘Apollo技术:壁画修复与保护的智能透视眼
  • 基于VUE的社区投诉建议处理与评价系统 [VUE]-计算机毕业设计源码+LW文档
  • Transmission Docker 容器化部署指南
  • 9、Ansible Container 构建与定制 MariaDB 容器指南
  • 交通银行广西区分行共谱“金融+文旅+体育”新篇章
  • 冒充密码管理器的钓鱼攻击机制与纵深防御策略研究
  • DTIIA 5.5、辅助和配套设备配置方式
  • 17、基于 Azure Event Grid 的响应式架构实践
  • 如何创建自己的Gitee实现国内镜像