news 2026/8/18 12:56:08

不带头节点的链式存储实现链栈

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
不带头节点的链式存储实现链栈

1.先创建一个结构体类型,有数据域和指针域

typedef struct LNode { int data; struct LNode* next; }LNode,*LinkStack;

2.以头节点为栈口进行操作进栈和出栈

头节点进栈

int HeadPush(LinkStack* Ps, int elem) { if ((*Ps) == NULL) { (*Ps) = (LNode*)malloc(sizeof(LNode)); if ((*Ps) == NULL) { return 1; } (*Ps)->data = elem; (*Ps)->next = NULL; return 0; } LNode* s = (LNode*)malloc(sizeof(LNode)); if (NULL ==s) { return 1; } s->data = elem; s->next = (*Ps); (*Ps) = s; return 0; }

头节点出栈

int HeadPop(LinkStack* Ps)//头节点出栈 { if ((*Ps) == NULL) { return 1; } LNode* p = (*Ps); (*Ps) = (*Ps)->next; free(p); return 0; } LNode* GetTail(LinkStack* Ps) { LNode* p = (*Ps); if (NULL == p) { return p; } while (p->next) { p = p->next; } return p; }

3.以尾节点为栈口进行操作进栈和出栈,因为是不带头节点的链栈,在处理尾节点的时候需要考虑到没有头节点,需要进行特殊处理

尾节点进栈

int TailPush(LinkStack*Ps,int elem) { if ((*Ps) == NULL) { (*Ps) = (LNode*)malloc(sizeof(LNode)); if ((*Ps) == NULL) { return 1; } (*Ps)->next = NULL; (*Ps)->data = elem; return 0; } LNode* p = GetTail(Ps);//得到了最后一个非空的节点 LNode* s = (LNode*)malloc(sizeof(LNode)); if (NULL == s) { return 1; } s->data = elem; s->next = p->next; p->next = s; return 0; }

尾节点出栈

int TailPop(LinkStack* Ps) { if ((*Ps) == NULL)//没有元素能够出栈 return 1; //需要找到倒数第二个非空的节点 if ((*Ps)->next == NULL) { LNode* temp = (*Ps);//只有一个元素 (*Ps) = (*Ps)->next; free(temp); return 0; } //if ((*Ps)->next->next == NULL) //{ // LNode* temp = (*Ps)->next; // (*Ps)->next = temp->next; // free(temp); // return 0; //} LNode* p = (*Ps); while (p->next->next) { p = p->next; } LNode* temp = p->next; p->next = temp->next; free(temp); return 0; }

4.打印链栈

void Display(LinkStack* Ps) { LNode* p = (*Ps); while (p) { printf("%d->", p->data); p = p->next; } printf("NULL\n"); }

5.判断一个链栈是不是空的,如果不是空的返回top元素

以头节点进行操作的top节点

void GetTop(LinkStack* Ps)//判断空栈和返回top元素 { if ((*Ps) == NULL)//说明是空表 { return 1; } return (*Ps)->data;//返回栈顶元素 }

以尾节点进行操作的top节点

LNode* GetTail(LinkStack* Ps) { LNode* p = (*Ps); if (NULL == p) { return p; } while (p->next) { p = p->next; } return p; }

6.全局代码,可以多测试几组,看是不是符合条件,主要是头节点和尾节点一定要考虑清楚

typedef struct LNode { int data; struct LNode* next; }LNode,*LinkStack; void InitStack(LinkStack* Ps) { (*Ps) = NULL;//头节点为空指针 } int HeadPush(LinkStack* Ps, int elem) { if ((*Ps) == NULL) { (*Ps) = (LNode*)malloc(sizeof(LNode)); if ((*Ps) == NULL) { return 1; } (*Ps)->data = elem; (*Ps)->next = NULL; return 0; } LNode* s = (LNode*)malloc(sizeof(LNode)); if (NULL ==s) { return 1; } s->data = elem; s->next = (*Ps); (*Ps) = s; return 0; } int HeadPop(LinkStack* Ps)//头节点出栈 { if ((*Ps) == NULL) { return 1; } LNode* p = (*Ps); (*Ps) = (*Ps)->next; free(p); return 0; } LNode* GetTail(LinkStack* Ps) { LNode* p = (*Ps); if (NULL == p) { return p; } while (p->next) { p = p->next; } return p; } int TailPush(LinkStack*Ps,int elem) { if ((*Ps) == NULL) { (*Ps) = (LNode*)malloc(sizeof(LNode)); if ((*Ps) == NULL) { return 1; } (*Ps)->next = NULL; (*Ps)->data = elem; return 0; } LNode* p = GetTail(Ps);//得到了最后一个非空的节点 LNode* s = (LNode*)malloc(sizeof(LNode)); if (NULL == s) { return 1; } s->data = elem; s->next = p->next; p->next = s; return 0; } int TailPop(LinkStack* Ps) { if ((*Ps) == NULL)//没有元素能够出栈 return 1; //需要找到倒数第二个非空的节点 if ((*Ps)->next == NULL) { LNode* temp = (*Ps);//只有一个元素 (*Ps) = (*Ps)->next; free(temp); return 0; } //if ((*Ps)->next->next == NULL) //{ // LNode* temp = (*Ps)->next; // (*Ps)->next = temp->next; // free(temp); // return 0; //} LNode* p = (*Ps); while (p->next->next) { p = p->next; } LNode* temp = p->next; p->next = temp->next; free(temp); return 0; } void Display(LinkStack* Ps) { LNode* p = (*Ps); while (p) { printf("%d->", p->data); p = p->next; } printf("NULL\n"); } void GetTop(LinkStack* Ps)//判断空栈和返回top元素 { if ((*Ps) == NULL)//说明是空表 { return 1; } return (*Ps)->data;//返回栈顶元素 } int main() { LinkStack S; InitStack(&S); //HeadPush(&S, 1); //HeadPush(&S, 2); //HeadPush(&S, 3); HeadPop(&S); TailPush(&S, 1); TailPush(&S, 2); TailPush(&S, 3); TailPush(&S, 4); TailPush(&S, 5); TailPop(&S); TailPop(&S); TailPop(&S); TailPop(&S); TailPop(&S); /*HeadPop(&S); HeadPop(&S); HeadPop(&S);*/ Display(&S); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/18 5:42:39

31、脚本编程进阶:Here文档、自上而下设计与流程控制

脚本编程进阶:Here文档、自上而下设计与流程控制 1. Here文档的使用 在文本输出方面,除了使用 echo 命令的两种方法外,还可以使用Here文档(Here Document)或Here脚本。Here文档是一种I/O重定向的额外形式,它允许我们将一段文本嵌入脚本,并将其作为命令的标准输入。其…

作者头像 李华
网站建设 2026/8/16 0:27:04

信捷XDH系列PLC的追剪/飞剪/电子凸轮程序模板

信捷XDH系列PLC追剪/飞剪/电子凸轮程序模板 包含了定长追剪和飞剪模式/定标飞剪/追剪模式 函数功能块无jia密,是学习信捷追/飞剪的经典模板 适合参考借鉴 包含:PLC程序信捷触摸屏HMI软元件分配表最近在搞PLC运动控制的朋友应该对信捷XDH系列不陌生&#…

作者头像 李华
网站建设 2026/8/17 11:23:47

【大模型】-LangChain--stream流式同步异步

文章目录1.同步stream流2.异步astream流3.异步astream流json输出4.异步事件astream_events流5.异步多线程1.同步stream流 import os from langchain_community.chat_models import ChatTongyios.environ["DASHSCOPE_API_KEY"] "sk-秘钥" llm ChatTongyi…

作者头像 李华
网站建设 2026/8/17 18:41:05

兜兜英语每日短语:逃单篇

1. 🍽️💨 dine and dash(吃霸王餐 / 吃完就跑) 英文:Nine diners in Chongqing dined and dashed, leaving one person behind. 中文:重庆 9 名食客吃霸王餐后逃单,只留下一人 “背锅”&#x…

作者头像 李华
网站建设 2026/8/16 7:11:51

计算机毕业设计springboot汽车智慧检修系统 基于SpringBoot的智能汽车故障预测与维修管理平台 融合IoT的SpringBoot车辆健康监测与维修决策系统

计算机毕业设计springboot汽车智慧检修系统a93520rj (配套有源码 程序 mysql数据库 论文) 本套源码可以在文本联xi,先看具体系统功能演示视频领取,可分享源码参考。 当汽车从“机械”进化为“数据孤岛”,每一次打火、每一脚刹车都…

作者头像 李华
网站建设 2026/8/17 7:15:59

python3

import osdef traverse_files(path):# 遍历当前路径下的所有文件和文件夹for item in os.listdir(path):# 拼接完整路径item_path os.path.join(path, item)if os.path.isfile(item_path):# 是文件则输出路径print("文件:", item_path)elif os.path.isdir(item_path…

作者头像 李华