news 2026/6/10 18:44:08

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

作者头像

张小明

前端开发工程师

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/6/9 19:38:19

Windows小工具,守护你的电脑

今天我给大家推荐三款实用的Windows小工具&#xff0c;分别是一款进程守护工具、一款弹窗信息获取软件和一款进程管理工具。 ONE 进程守护 这款软件叫“守护服务”&#xff0c;但是我更喜欢称呼它为“进程守护”&#xff0c;因为它可以添加你想要守护的进程。即使进程崩溃&am…

作者头像 李华
网站建设 2026/6/10 2:02:26

PostgreSQL(Postgres)全面介绍

PostgreSQL&#xff08;常简称 Postgres&#xff09;是一款开源免费、功能强大的企业级关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;诞生于 1986 年的加州大学伯克利分校&#xff0c;经过数十年的迭代&#xff0c;现已成为兼具可靠性、扩展性和兼容性的数据…

作者头像 李华
网站建设 2026/6/10 11:03:57

Wan2.2-T2V-A14B在航天任务动画演示中的高精度要求满足情况

Wan2.2-T2V-A14B在航天任务动画演示中的高精度要求满足情况 在航天工程领域&#xff0c;一个看似不起眼的动画视频&#xff0c;往往承载着远超视觉呈现本身的价值。它不仅是向公众科普“天问探火”“嫦娥奔月”的窗口&#xff0c;更是任务设计团队验证轨道逻辑、姿态控制与应急…

作者头像 李华
网站建设 2026/6/10 12:49:18

阿里云渠道商:轻量应用服务器连接常见问题与解决指南

一、为什么你的轻量服务器连不上&#xff1f;当新购的阿里云轻量应用服务器首次远程连接失败时&#xff0c;90% 的故障集中在以下场景&#xff1a; Windows 系统卡在 “远程桌面连接” 黑屏 Linux 系统 SSH 报错 "Connection timed out" 宝塔面板 / 应用镜像无法访问…

作者头像 李华