原创

栈的链式存储结构-链栈的基本操作

温馨提示:
本文最后更新于 2021年02月23日,已超过 1,381 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

栈的基本操作

链栈的表示

file

/*定义链栈*/
typedef struct StackNode{
    int data;
    struct StackNode *next;
}StackNode,*LinkStack;

链栈的初始化

/*链栈的初始化*/
int InitStack(LinkStack &S){
    S = NULL; 
    return 0;
}

进栈操作

/*链栈进栈*/ 
int Push(LinkStack &S,int e){
    LinkStack p  = (LinkStack)malloc(sizeof(StackNode));
    if(!p) return -1;
    p->data = e;
    p->next = S;
    S = p;
    printf("元素【%d】进栈成功\n",e); 
    return 0;
}

出栈操作

/*链栈出栈*/
int Pop(LinkStack &S,int e){
    if(!S) return -1;
    LinkStack p = S;
    e = p->data;
    S = S->next;
    free(p);
    printf("元素【%d】出栈成功\n",e); 
    return 0;
}

获取链栈的长度

/*获取链栈的长度*/ 
int GetLength_S(LinkStack S){
    int n = 0;
    LinkStack p = S;
    while(p){
        p = p->next;
        n++;
    }
    return n;
}

判断栈空

/*链栈是否为空*/
int StackEmpty(LinkStack S){
    if(S){
        printf("链栈不为空\n");
        return 1;
    } else{
        printf("链栈为空\n");
        return 0;  //链栈为空返回0 
    }
}

清空链栈

/*清空链栈*/
int ClearStack(LinkStack &S){
    if(!S) return -1;
    LinkStack q;
    while(S) {
        q = S;
        S = S->next;
        free(q);
    }
    return 0;
}

销毁链栈

/*销毁栈*/ 
int DestroyStack(LinkStack &S){
    if(!S) return -1;
    LinkStack q;
    while(S) {
        q = S;
        S = S->next;
        free(q);
    }
    return 0;
}

读取栈顶元素

/*取链栈栈顶元素*/ 
int GetTop(LinkStack S ){
    if(!S) return -1;
    return S->data;
}

完整代码

#include<stdio.h> 
#include<stdlib.h> 

typedef struct StackNode{
    int data;
    struct StackNode *next;
}StackNode,*LinkStack;

/*链栈的初始化*/
int InitStack(LinkStack &S){
    S = NULL; 
    return 0;
} 
/*链栈是否为空*/
int StackEmpty(LinkStack S){
    if(S){
        printf("链栈不为空\n");
        return 1;
    } else{
        printf("链栈为空\n");
        return 0;  //链栈为空返回0 
    }
} 
/*链栈进栈*/ 
int Push(LinkStack &S,int e){
    LinkStack p  = (LinkStack)malloc(sizeof(StackNode));
    if(!p) return -1;
    p->data = e;
    p->next = S;
    S = p;
    printf("元素【%d】进栈成功\n",e); 
    return 0;
} 
/*链栈出栈*/
int Pop(LinkStack &S,int e){
    if(!S) return -1;
    LinkStack p = S;
    e = p->data;
    S = S->next;
    free(p);
    printf("元素【%d】出栈成功\n",e); 
    return 0;
} 
/*取链栈栈顶元素*/ 
int GetTop(LinkStack S ){
    if(!S) return -1;
    return S->data;
}
/*获取链栈的长度*/ 
int GetLength_S(LinkStack S){
    int n = 0;
    LinkStack p = S;
    while(p){
        p = p->next;
        n++;
    }
    return n;
}
/*清空链栈*/
int ClearStack(LinkStack &S){
    if(!S) return -1;
    LinkStack q;
    while(S) {
        q = S;
        S = S->next;
        free(q);
    }
    return 0;
} 
int main(){
    int initStatus;
    int popElem; 
    LinkStack S; 
    //链栈初始化 
    initStatus = InitStack(S);
    if(initStatus==0){
        printf("链栈初始化成功!\n");
    }else{
        printf("链栈初始化失败!\n");
    }
    printf("链栈的长度为:%d\n",GetLength_S(S));
    //判断链栈是否为空
    StackEmpty(S); 

    //链栈进栈
    Push(S,1);

    //判断链栈是否为空
    StackEmpty(S); 

    Push(S,2);
    Push(S,3);
    Push(S,4);
    //获取栈的元素 
    printf("栈顶元素为:%d\n",GetTop(S));
    //链栈出栈
    Pop(S,popElem);
    //获取栈的元素 
    printf("栈顶元素为:%d\n",GetTop(S));
    printf("链栈的长度为:%d\n",GetLength_S(S));
    //清空链栈
    ClearStack(S); 
    //判断链栈是否为空
    StackEmpty(S); 
    //链栈进栈
    Push(S,1);
    //获取栈的元素 
    printf("栈顶元素为:%d\n",GetTop(S));
}

运行结果

file

正文到此结束
本文目录