数据结构链栈

数据结构链栈,第1张

链栈的抽象类型表示及相关运算(C语言实现) 文章目录
一.链栈抽象类型定义
二.链栈的基本运算

1.链栈的抽象类型定义

#define OK 1
#define EROOR 0
#define NULLSTACK 1
#define NULLDATA 0

#include 

typedef int ElemType;
typedef struct lickStack{
    ElemType data; // 数据域
    struct lickStack *next; //指针域
}LickStack;

2.链栈的初始化

void InitStack(LickStack *L){

    L = (LickStack *)malloc(sizeof(LickStack));

    L->next = NULL;  //置空栈

}

3.链栈判空

int EmptyStack(LickStack *L){

    if(L->next == NULL)
        return NULLSTACK;
    else
        return 0;

}

4.进栈运算

int PushStack(LinkStack *L,ElemType edata){
    
    LinkStack *temp; //初始化一个工作指针
    temp = (LinkStack *)malloc(sizeof(LinkStack));
    
    if(!temp) //初始化失败
        return EROOR;
    
    temp->data = edata; //元素进栈
    temp->next = L->next; //使指针指向原来栈顶
    L->next = temp; //置换栈顶指向新栈顶
    return OK;
    
}

5.出栈运算

int PopStack(LinkStack *L) {

    LinkStack *temp;

    if (!EmptyStack(temp)) {//栈判空 非空执行以下 *** 作

        temp = L->next; //temp指向当前栈顶
        L->next = temp->next; //使原栈顶下一个结点成为新栈顶
        free(temp); //释放原栈顶
        return OK;

    } else {
        return EROOR;
    }

}

6.取栈顶元素

ElemType GetTop(LinkStack *L){

    if(!EmptyStack(L)){ //判空
        return L->next->data;
    } else{
        return NULLDATA;
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/707500.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存