数据结构:链式栈

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;
} 

结果

相关推荐
2401_892070982 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
CoderCodingNo5 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
网安INF6 小时前
数据结构第三章:栈、队列和数组
数据结构
yuannl107 小时前
数据结构----双端队列实现
数据结构
无限进步_7 小时前
【C++】只出现一次的数字 II:位运算的三种解法深度解析
数据结构·c++·ide·windows·git·算法·leetcode
qq_454245038 小时前
通用引用管理框架
数据结构·架构·c#
lcj25119 小时前
【C语言】数据在内存中的存储
c语言·数据结构
旖-旎9 小时前
哈希表(字母异位次分组)(5)
数据结构·c++·算法·leetcode·哈希算法·散列表
paeamecium10 小时前
【PAT甲级真题】- All Roads Lead to Rome (30)
数据结构·c++·算法·pat考试·pat
PD我是你的真爱粉10 小时前
Redis 数据类型与底层实现:从 SDS、Quicklist 到 ZSet 跳表彻底讲透
数据结构·redis