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

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

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; }
http://www.jsqmd.com/news/88290/

相关文章:

  • vue基于Spring Boot框架的技术的网上购物商城系统开发商家_9ah8o18s
  • Tarjan全家桶系列--割点
  • Flink SQL 模式识别用 MATCH_RECOGNIZE 把 CEP 写成 SQL
  • [编程杂谈]这题怎么这么难!!!(上)
  • Flink SQL Time Travel用 FOR SYSTEM_TIME AS OF 查询历史快照
  • AI:深度学习的前向传播和反向传播
  • 31、脚本编程进阶:Here文档、自上而下设计与流程控制
  • 基于SSM的高校大学生就业平台的设计与实现
  • vue基于Spring Boot框架的数字乡村旅游景点预约平台的设计与实现_ax346a6i
  • 计算机毕业设计springboot高考志愿智能推荐系统 基于SpringBoot的考后择校智慧匹配平台 面向新高考的SpringBoot个性化志愿辅助决策系统
  • AI:深度学习中反向传播中的链式法则和梯度
  • 英语_阅读_2019 Young Scientist Challenge_待读
  • 《Ascend C 进阶实战:高性能通用 Softmax 算子设计、数值稳定性与多轴支持》
  • 29、《pkg-config与GNU Autotools使用指南》
  • 计算机毕业设计springboot汽车智慧检修系统 基于SpringBoot的智能汽车故障预测与维修管理平台 融合IoT的SpringBoot车辆健康监测与维修决策系统
  • 蓝牙连接例程/蓝牙收发信号引出
  • 题目集 4~5 总结性 Blog
  • Java-TestNG——.xml文件的tests
  • 销售助手-推荐系统
  • 腾讯ACE误封禁
  • 兜兜英语每日短语:逃单篇
  • 【update 更新数据语法合集】.NET开源ORM框架 SqlSugar 系列 - 教程
  • 基于微信小程序的驾校模拟考试系统的设计与实现 - 详解
  • 你写的不是代码,是生存的底气|从“制造思维”到“生长思维”的范式革命
  • Octo论文详解
  • 移动应用开发实验室大一上考核
  • DAY 8 打卡训练
  • 详细介绍:Java集合框架概述
  • 瑞萨推出M33内核WiFi6双频(2.4G+5G) + BLE蓝牙芯片RA6W2/W1,同时还将推出现成模组
  • 修改kubuntu下matlab2025b系统界面的大小