news 2026/5/13 12:58:14

数据结构-栈(核心代码)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
数据结构-栈(核心代码)

顺式结构

#define _CRT_SECURE_NO_WARNINGS 1 //栈的顺式结构 #include<stdio.h> #define MAXSIZE 100 typedef int Elemtype; //定义栈 typedef struct stack { Elemtype data[MAXSIZE]; int top; }Stack; //初始化栈 void initstack(Stack* S) { S->top = -1; } //判断栈是否为空 int isempty(Stack* S) { if (S->top == -1) { printf("空的\n"); return 1; } else { return 0; } } //进栈/压栈 int pushStack(Stack* S,Elemtype e) { if (S->top >= MAXSIZE - 1) { printf("满了\n"); return 0; } S->top++; S->data[S->top] = e; return 1; } //出栈 int pop(Stack* S, Elemtype* e) { if (S->top >= MAXSIZE - 1) { printf("满了\n"); return 0; } *e = S->data[S->top]; S->top--; return 1; } //获取栈顶元素 int get_top(Stack* S, Elemtype* e) { if (S->top == -1) { printf("空的\n"); return 0; } *e = S->data[S->top]; return 1; } int main() { Stack S; initstack(&S); pushStack(&S,10); pushStack(&S,20); pushStack(&S,30); Elemtype e; pop(&S, &e); printf("%d\n", e); get_top(&S, &e); printf("%d\n", e); return 0; } //动态内存分配定义和初始化栈 #include<stdlib.h> #define MAXSIZE 100 typedef int Elemtype; typedef struct stack { Elemtype* data; int top; }Stack; Stack* initstack(Stack* S) { Stack* S = (Stack*)malloc(sizeof(Stack)); S->data = (Elemtype*)malloc(sizeof(Elemtype)*MAXSIZE); S->top = -1; return S; }

链式结构

#define _CRT_SECURE_NO_WARNINGS 1 //栈的链式结构 #include<stdio.h> #include<stdlib.h> typedef int Elemtype; #define MAXSIZE 100 //定义栈 typedef struct stack { Elemtype data; struct stack* Next;//栈顶 }Stack; //初始化栈 Stack* initstack() { Stack* S = (Stack*)malloc(sizeof(Stack)); S->data = 0; S->Next = NULL; return S; } //判断栈是否为空 int isEmpty(Stack* S) { if (S->Next == NULL) { printf("空的\n"); return 1; } else { return 0; } } //进展/压栈(相当于头插法,链式结构的栈只能头插法不能尾插法) int push(Stack* S, Elemtype e) { Stack* P = (Stack*)malloc(sizeof(Stack)); P->data = e; P->Next = S->Next; S->Next = P; return 1; } //出栈 int pop(Stack* S, Elemtype* e) { if (S->Next == NULL) { printf("空的\n"); return 0; } *e = S->Next->data; Stack* Q = S->Next; S->Next = Q->Next; free(Q); return 1; } //获取栈顶元素 int get_top(Stack* S, Elemtype* e) { if (S->Next == NULL) { printf("空的\n"); return 0; } *e = S->Next->data; return 1; } int main() { Stack* S = initstack(); isEmpty(S); push(S, 10); push(S, 20); push(S, 30); push(S, 40); Elemtype e; pop(S, &e); printf("%d\n", e); get_top(S, &e); printf("%d\n", e); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 22:40:37

小猫影视 - 影视播放器

链接&#xff1a;https://pan.quark.cn/s/6eb332dc88cb支持平台&#xff1a;#Windows #macOS #Linux #Android一款影视播放器&#xff0c;支持视频点播&#xff08;VOD&#xff09;和JavaScript扩展源。可以通过简单的操作快速找到并播放自己喜欢的影视内容。支持多种视频格式&…

作者头像 李华
网站建设 2026/5/12 5:20:48

哔哩下载姬DownKyi:专业B站视频管理工具深度体验

哔哩下载姬DownKyi&#xff1a;专业B站视频管理工具深度体验 【免费下载链接】downkyi 哔哩下载姬downkyi&#xff0c;哔哩哔哩网站视频下载工具&#xff0c;支持批量下载&#xff0c;支持8K、HDR、杜比视界&#xff0c;提供工具箱&#xff08;音视频提取、去水印等&#xff09…

作者头像 李华
网站建设 2026/4/30 22:46:11

Shell脚本部署——8day

Shell脚本部署课程目标掌握shell脚本的思路完成shell脚本的编写课程实验shell脚本的思路shell脚本的编写课堂引入部署项目中&#xff0c;有很多都是固定的模式与内容&#xff0c;所有的东西都需要人工去操作的话&#xff0c;成本就比较高&#xff0c;也比较麻烦&#xff0c;能不…

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

AI 自动生成报表

1. AI 生成图表 向上汇报经常需要制作图表&#xff0c;如果没有顺手的工具&#xff0c;可能需要花费大量的时间消耗在报表的制作上。随着AI的迅猛发展&#xff0c;我们通过一个简单的对话来低成本实现报表的生成。 2. 使用到的工具 MCP 平台&#xff1a;https://www.modelscope…

作者头像 李华
网站建设 2026/4/30 22:46:18

Unity学习笔记(十二)碰撞中的刚体和碰撞器

碰撞产生的必要条件 两个物体都有碰撞器&#xff0c;至少有一个物体有刚体刚体1.它就是模拟力的作用2.Mass质量 默认为KG 质量越大 阻性越大3.Drag空气阻力 根据力移动对象时影响对象的空气阻力大小。影响移动&#xff0c;0表示没有空气阻力4.Use Gravity 是否受重力影响5.Angu…

作者头像 李华