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

数据结构代码分享-5 链式栈

linkstack.c

#include<stdio.h>
#include<stdlib.h>
#include"linkstack.h"
//1.创建一个空的栈
void CreateEpLinkStack(linkstack_t **ptop)
{*ptop = NULL;
}
//2.入栈,ptop是传入的栈针的地址,data是入栈的数据
int pushLinkStack(linkstack_t **ptop, datatype data)
{//创建新节点保存入站数据linkstack_t *penw = (linkstack_t *)malloc(sizeof(linkstack_t));if(penw == NULL){printf("pushLinkStack err\n");return -1;}//申请空间是为了存放data,并初始化penw->data = data;//插入penw->next = *ptop;*ptop = penw;return 0;
}
//3.判断栈是否为空
int isEmptyLinkStack(linkstack_t *top)
{return top==NULL;
}
//4.出栈
datatype popLinkStack(linkstack_t **ptop)
{//linkstack_t *pdel = NULL;if(isEmptyLinkStack(*ptop)){printf("popLinkStack err\n");return -1;}pdel =*ptop;datatype data = pdel->data;(*ptop)=(*ptop)->next;free(pdel);pdel = NULL;}
//5.清空栈
void ClearLinkStack(linkstack_t **ptop)
{while(!isEmptyLinkStack(*ptop))popLinkStack(*ptop);
}
//6.求栈的长度
int LengthLinkStack(linkstack_t *top)
{int len=0;while(top!=NULL){len++;top=top->next;}return len;
}
//7.获取栈顶数据,不是出栈,不需要移动main函数中的top,所以用一级指针
datatype getTopLinkStack(linkstack_t *top)
{if(isEmptyLinkStack(top)){return top->data;}else{return -1;}}
int main(int argc, char const *argv[])
{return 0;
}

linkstack.h

#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__
typedef int datatype;
typedef struct linkstack
{datatype data;struct linkstack *next;
} linkstack_t;
//1.创建一个空的栈
void createEmptyLinkStack(linkstack_t **ptop);
//2.入栈,ptop是传入的栈针的地址,data是入栈的数据
int pushLinkStack(linkstack_t **ptop, datatype data);
//3.判断栈是否为空
int isEmptyLinkStack(linkstack_t *top);
//4.出栈
datatype popLinkStack(linkstack_t **ptop);
//5.清空栈
void clearLinkStack(linkstack_t **ptop);
//6.求栈的长度
int lengthLinkStack(linkstack_t *top);
//7.获取栈顶数据,不是出栈,不需要移动main函数中的top,所以用一级指针
datatype getTopLinkStack(linkstack_t *top);
#endif

http://www.lryc.cn/news/626216.html

相关文章:

  • Consul- acl机制!
  • latex|算法algorithm宏包和注意事项
  • 区块链 + 域名Web3时代域名投资的新风口(下)
  • RWA加密金融高峰论坛星链品牌全球发布 —— 稳定币与Web3的香港新篇章
  • CTFshow系列——命令执行web34-37
  • 厂区能源管理智能化改造物联网解决方案
  • 驱动开发系列65 - NVIDIA 开源GPU驱动open-gpu-kernel-modules 目录结构
  • week2-[一维数组]最大元素
  • 数据仓库OLTPOLAP维度讲解
  • 传统防火墙
  • LG P3710 方方方的数据结构 Solution
  • Windows/Centos 7下搭建Apache服务器
  • Android RxJava数据库操作:响应式改造实践
  • 006.Redis 哨兵(Sentinel)架构实战
  • C++入门自学Day14-- deque类型使用和介绍(初识)
  • 【运维进阶】Ansible 角色管理
  • 用poll改写select
  • RabbitMQ:SpringAMQP Direct Exchange(直连型交换机)
  • 在Excel和WPS表格中为多个数字同时加上相同的数值
  • 如何解析PDF中的复杂表格数据
  • UniApp 实现pdf上传和预览
  • Go语言快速入门指南(面向Java工程师)
  • 智慧校园中IPTV融合对讲:构建高效沟通新生态
  • DHCP详解
  • sqlite-gui:一款开源免费、功能强大的SQLite开发工具
  • Netty 集成 protobuf
  • 代码随想录刷题——字符串篇(七)
  • 机械原理的齿轮怎么学?
  • Transformer中的编码器和解码器是什么?
  • ubuntu安装kconfig-frontends提示报错