其他分享
首页 > 其他分享> > 栈的链式存储的实现

栈的链式存储的实现

作者:互联网

LINKSTACK.h


#ifndef UNTITLED4_LINKSTACK_H
#define UNTITLED4_LINKSTACK_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//链式栈的结点
typedef struct LINKNODE{
    struct LINKNODE* next;
}Linknode;
//链式栈
typedef struct LINKSTACK{
    Linknode head;
    int size;
}Linkstack;

//初始化函数
Linkstack *Initlinkstack();
//入栈
void Pushlinkstack(Linkstack* stack,Linknode* data);
//出栈
void Poplinkstack(Linkstack* stack);
//返回栈顶元素
Linknode* Toplinkstack(Linkstack* stack);
//返回栈元素个数
int Sizelinkstack(Linkstack* stack);
//清空栈
void Clearlinkstack(Linkstack* stack);
//销毁栈
void Freespace(Linkstack* stack);

#endif //UNTITLED4_LINKSTACK_H

LINKSTACK.cpp


#include "LINKSTACK.h"

//初始化函数
Linkstack * Initlinkstack(){

    Linkstack *stack = (Linkstack*) malloc(sizeof(Linkstack));
    stack->head.next=NULL;
    stack->size=0;
    return stack;

}
//入栈
void Pushlinkstack(Linkstack* stack,Linknode* data){
    if(stack == NULL){
        return;
    }
    if(data == NULL){
        return;
    }

    data->next=stack->head.next;
    stack->head.next=data;
    stack->size++;

}
//出栈
void Poplinkstack(Linkstack* stack){
    if(stack  == NULL){
        return;
    }
    if(stack->size ==  0){
        return;
    }

    //第一个有效结点
    Linknode *pNext = stack->head.next;
    stack->head.next = pNext->next;

    stack->size--;

}
//返回栈顶元素
Linknode* Toplinkstack(Linkstack* stack){
    if(stack == NULL){
        return NULL;
    }
    if(stack->size == 0){
        return NULL;
    }
    return stack->head.next;
}
//返回栈元素个数
int Sizelinkstack(Linkstack* stack){
    if(stack == NULL){
        return -1;
    }
    return stack->size;
}
//清空栈
void Clearlinkstack(Linkstack* stack){
    if(stack == NULL){
        return;
    }
    //没有创建内存 不用释放
    stack->head.next=NULL;
    stack->size=0;
}
//销毁栈
void Freespace(Linkstack* stack){
    if(stack == NULL){
        return;
    }
    free(stack);
}

main.cpp

#include "LINKSTACK.h"

typedef struct PERSON{
    Linknode node;
    char name[64];
    int age;
}Person;
int main() {
    //创建栈
    Linkstack *stack = Initlinkstack();
    //创建数据
    Person p1,p2,p3,p4,p5;
    strcpy(p1.name,"aaa");
    strcpy(p2.name,"bbb");
    strcpy(p3.name,"ccc");
    strcpy(p4.name,"ddd");
    strcpy(p5.name,"eee");

    p1.age = 10;
    p2.age = 20;
    p3.age = 30;
    p4.age = 40;
    p5.age = 50;

    //入栈
    Pushlinkstack(stack,(Linknode*)&p1);
    Pushlinkstack(stack,(Linknode*)&p2);
    Pushlinkstack(stack,(Linknode*)&p3);
    Pushlinkstack(stack,(Linknode*)&p4);
    Pushlinkstack(stack,(Linknode*)&p5);

    //输出
    while(Sizelinkstack(stack)>0){
        //取出栈顶元素
        Person *p = (Person*)Toplinkstack(stack);//强转为person*
        printf("name:%s age:%d\n",p->name,p->age);
        //弹出栈顶元素
        Poplinkstack(stack);
    }

    //销毁栈
    Freespace(stack);

    return 0;
}

在这里插入图片描述

标签:Linkstack,存储,return,实现,next,链式,NULL,Linknode,stack
来源: https://blog.csdn.net/weixin_47776194/article/details/120395706