数据结构:链式栈

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

结果

相关推荐
吴声子夜歌1 小时前
OpenCV——Mat类及常用数据结构
数据结构·opencv·webpack
笑口常开xpr2 小时前
数 据 结 构 进 阶:哨 兵 位 的 头 结 点 如 何 简 化 链 表 操 作
数据结构·链表·哨兵位的头节点
@我漫长的孤独流浪3 小时前
数据结构测试模拟题(4)
数据结构·c++·算法
YGGP7 小时前
吃透 Golang 基础:数据结构之 Map
开发语言·数据结构·golang
weixin_419658318 小时前
数据结构之栈
数据结构
图先8 小时前
数据结构第一章
数据结构
草莓熊Lotso9 小时前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
Andrew_Xzw11 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
超的小宝贝12 小时前
数据结构算法(C语言)
c语言·数据结构·算法
凤年徐14 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表