链栈
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define false 0 5 #define true 1 6 7 typedef int ElementType; 8 typedef int bool; 9 typedef struct SNode *PtrToSNode; 10 struct SNode 11 { 12 ElementType Data; 13 PtrToSNode Next; 14 }; 15 typedef PtrToSNode Stack; 16 17 Stack CreateStack(); //链栈的构建 18 bool IsEmpty(Stack S); //判断栈是否为空 19 void Push(Stack S, ElementType X); //入栈 20 ElementType Pop(Stack S); //出栈 21 22 23 Stack CreateStack() //链栈的构建 24 { 25 Stack S; 26 27 S = malloc(sizeof(struct SNode)); 28 S->Next = NULL; 29 return S; 30 } 31 32 bool IsEmpty(Stack S) //判断栈是否为空 33 { 34 return (S->Next == NULL); 35 } 36 37 void Push(Stack S, ElementType X) //入栈 38 { 39 PtrToSNode TmpCell; 40 41 TmpCell = (PtrToSNode)malloc(sizeof(struct SNode)); 42 TmpCell->Data = X; 43 TmpCell->Next = S->Next; 44 S->Next = TmpCell; 45 } 46 47 ElementType Pop(Stack S) //出栈 48 { 49 if(IsEmpty(S)) 50 { 51 printf("栈为空,出栈失败!\n"); 52 return false; 53 } 54 55 PtrToSNode FirstCell; 56 ElementType TopElem; 57 58 FirstCell = S->Next; 59 TopElem = FirstCell->Data; 60 S->Next = FirstCell->Next; 61 free(FirstCell); 62 return TopElem; 63 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南