数据结构:链式栈

stack.h

cs 复制代码
/*===============================================
*   文件名称: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

cs 复制代码
/*===============================================
*   文件名称: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;
    else
        return 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

cs 复制代码
/*===============================================
*   文件名称: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;
} 

结果

相关推荐
Code小翊1 小时前
归并排序基础理解
数据结构·算法·排序算法
.小小陈.1 小时前
数据结构2:单链表
c语言·开发语言·数据结构·笔记·学习方法
草莓工作室1 小时前
数据结构4:线性表3-链式存储的线性表
数据结构
雾时之林1 小时前
数据结构--单链表
数据结构
Camel卡蒙1 小时前
数据结构——二叉搜索树Binary Search Tree(介绍、Java实现增删查改、中序遍历等)
java·开发语言·数据结构
2401_841495641 小时前
【数据结构】基于Floyd算法的最短路径求解
java·数据结构·c++·python·算法··floyd
立志成为大牛的小牛1 小时前
数据结构——二十三、并查集的终极优化(王道408)
开发语言·数据结构·笔记·学习·程序人生·考研
.YM.Z2 小时前
数据结构——链表(二)
数据结构·链表
勇闯逆流河3 小时前
【C++】用红黑树封装map与set
java·开发语言·数据结构·c++
一只鱼^_3 小时前
第 167 场双周赛 / 第 471 场周赛
数据结构·b树·算法·leetcode·深度优先·近邻算法·迭代加深