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

数据结构:链式栈

stack.h

/*===============================================
*   文件名称:stack.h
*   创 建 者:cxy     
*   创建日期:2024年01月18日
*   描    述:
================================================*/
#ifndef _STACK_H
#define _STACK_H#include <stdio.h>
#include <stdlib.h>typedef struct stack{int data;struct stack *top;
}Stack,*Pstack;int init(Pstack *P);
int empty(Pstack p);
int push_stack(Pstack p,int data);  //尾插
int pop_stack(Pstack p,int *data);  //只能从尾巴出栈,在一端操作(栈顶)
int clear(Pstack p);#endif

stack.c

/*===============================================
*   文件名称:stack.c
*   创 建 者:     
*   创建日期:2024年01月18日
*   描    述:
================================================*/
#include "stack.h"int init(Pstack *P)
{*P = malloc(sizeof(Stack));if(NULL == *P){perror("init err:*P");return -1;}(*P)->top = NULL;
}int empty(Pstack p)
{if(NULL == p){perror("empty err:p");return -1;}if(p->top == NULL)return 1;elsereturn 0;
}int push_stack(Pstack p,int data)
{if(NULL == p){perror("push err:p");return -1;}Pstack q = malloc(sizeof(Stack));if(NULL == q){perror("push err:q");return -1;}while(p->top != NULL)p = p->top;q->data = data;q->top = p->top;p->top = q;return 0;
}int pop_stack(Pstack p,int *data)
{if(NULL == p){perror("pop err:p");return -1;}if(empty(p)){perror("pop err:empty");return -1;}while(p->top->top != NULL)p = p->top;Pstack q = p->top;*data = q->data;p->top = q->top;free(q);return 0;
}int clear(Pstack p)
{if(NULL == p){perror("clear err:p");return -1;}Pstack q = p;while(p->top != NULL){q = p->top;p->top = q->top;free(q);}return 0;
}

mian.c

/*===============================================
*   文件名称:main.c
*   创 建 者:     
*   创建日期:2024年01月18日
*   描    述:
================================================*/
#include "stack.h"int main(int argc, char *argv[])
{ Pstack p;init(&p);int data;printf("**********empty,1为空**********\n");data = empty(p);printf("%d\n",data);printf("**********push_stack**********\n");data = 5;while(data--){push_stack(p,data);printf("入栈数据为:%d\n",data);}printf("**********empty,1为空**********\n");data = empty(p);printf("%d\n",data);printf("**********pop_stack**********\n");data = 5;int num = 0;while(data--){pop_stack(p,&num);printf("出栈数据为:%d\n",num);}printf("**********clear**********\n");clear(p);printf("**********empty,1为空**********\n");data = empty(p);printf("%d\n",data);return 0;
} 

结果

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

相关文章:

  • openssl3.2 - 官方demo学习 - mac - gmac.c
  • HugggingFace 推理 API、推理端点和推理空间相关模型部署和使用以及介绍
  • python的tabulate包在命令行下输出表格不对齐
  • LLM之幻觉(二):大语言模型LLM幻觉缓减技术综述
  • C# 使用多线程,关闭窗体时,退出所有线程
  • 数据结构实验6:图的应用
  • Spring Boot整合JUnit
  • uniapp写小程序实现清除缓存(存储/获取/移除/清空)
  • js菜单隐藏显示
  • 学习Spring的第五天(Bean的依赖注入)
  • GAN在图像数据增强中的应用
  • Git推送本地文件到仓库
  • Django笔记(一):环境部署
  • 用Pytorch实现线性回归模型
  • WordPress模板层次与常用模板函数
  • HarmonyOS应用开发者高级认证试题库(鸿蒙)
  • 系分备考计算机网络传输介质、通信方式和交换方式
  • js原生面试总结
  • 接口自动化测试框架设计
  • 详解ISIS动态路由协议
  • Linux操作系统----gdb调试工具(配实操图)
  • 去除GIT某个时间之前的提交日志
  • 4 python快速上手
  • 单元测试-spring-boot-starter-test+junit5
  • CentOS 7上安装Anaconda 详细教程
  • 2023年全球软件架构师峰会(ArchSummit深圳站):核心内容与学习收获(附大会核心PPT下载)
  • RT-Thread Studio学习(十六)定时器计数
  • 【linux进程间通信(一)】匿名管道和命名管道
  • 第11章 jQuery
  • leetcode:1736. 替换隐藏数字得到的最晚时间(python3解法)