408数据结构线性表——顺序栈、链栈的实现

顺序栈

#include <iostream>

using namespace std;
#define maxSize 10

typedef struct {
    int data[maxSize];
    int top;
}SqStack;

void InitStack(SqStack &S) {
    S.top = -1; //初始化栈顶指针
}

bool StackEmpty(SqStack S) {
    if (S.top == -1) return true;

    return false;
}

bool Push(SqStack &S,int x) {
    if (S.top == maxSize-1) return false;

    S.data[++S.top] = x;
    return true;
}

bool Pop(SqStack &S,int &x) {
    if (S.top == -1) return false;

    x = S.data[S.top--];
    return true;
}

bool GetTop(SqStack S,int &x) {
    if (S.top == -1) return false;

    x = S.data[S.top];
    return true;
}

void PrintStack(SqStack S) {
    if (S.top == -1) {
        cout<<"顺序栈为空!"<<endl;
    } else {
        for (int i = 0; i < S.top + 1; i++) {
            cout<<S.data[i]<<" ";
        }
        cout<<endl;
    }   
}

int main() {
    SqStack S;
    InitStack(S);

    Push(S,1);
    Push(S,3);
    Push(S,2);
    PrintStack(S);
    int x;
    GetTop(S,x);
    cout<<"栈顶元素为:"<<x<<endl;

    Pop(S,x);
    cout<<"弹出元素为:"<<x<<endl;
    Pop(S,x);
    cout<<"弹出元素为:"<<x<<endl;
    system("pause");
    return 0;
}

在这里插入图片描述

链栈

#include <iostream>

using namespace std;

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

//带头结点的实现方式。
void InitStack(LinkStack &S) {
    S = (LinkStack)malloc(sizeof(Linknode)); //创建头结点
    // 也可以写成这样 S = (Linknode *)malloc(sizeof(Linknode)); 
    S->next = NULL;
}

bool StackEmpty(LinkStack S) {
    if (S->next == NULL) return true;

    return false;
}

bool Push(LinkStack &S,int x) {
    Linknode *p = (Linknode *)malloc(sizeof(Linknode));
    p->data = x;
    p->next = S->next;
    S->next = p;
    return true;
}

bool Pop(LinkStack &S,int &x) {
    if (S->next == NULL) return false;

    Linknode *p;
    x = S->next->data;
    p = S->next;
    S->next = p->next;
    free(p);
    return true;
}

bool GetTop(LinkStack S,int &x) {
    if (S->next == NULL) return false;

    x = S->next->data;
    return true;
}

void DestroyStack(LinkStack &S) {
    Linknode *p;
    while(S->next != NULL) {
        p = S->next;
        S->next = p->next;
        free(p);
    }
    free(S);
}

void PrintStack(LinkStack S) {
    if (S->next == NULL) {
        cout<<"链栈为空!"<<endl;
    } else {
        while(S->next != NULL) {
            cout<<S->next->data<<" ";
            S = S->next;
        }
        cout<<endl;
    }   
}

//不带头结点的实现方式。
void InitStack2(LinkStack &S) {
    S = NULL;
}

bool StackEmpty2(LinkStack S) {
    if (S == NULL) return true;

    return false;
}

bool Push2(LinkStack &S,int x) {
    Linknode *p = (Linknode *)malloc(sizeof(Linknode));

    p->data = x;
    p->next = S;
    S = p;
    return true;
}

bool Pop2(LinkStack &S,int &x) {
    if (S == NULL) return false;

    Linknode *p;
    x = S->data;
    p = S;
    S = p->next;
    free(p);
    return true;
}

bool GetTop2(LinkStack S,int &x) {
    if (S == NULL) return false;

    x = S->next->data;
    return true;
}

void DestroyStack2(LinkStack &S) {
    Linknode *p;
    while(S != NULL) {
        p = S->next;
        S = p->next;
        free(p);
    }
    free(S);
}

void PrintStack2(LinkStack S) {
    if (S == NULL) {
        cout<<"链栈为空!"<<endl;
    } else {
        while(S!= NULL) {
            cout<<S->data<<" ";
            S = S->next;
        }
        cout<<endl;
    }   
}


int main() {
    // 带头结点测试
    // LinkStack S;
    // InitStack(S);

    // if (StackEmpty(S)) cout<<"链栈为空!"<<endl;
    // else PrintStack(S);

    // Push(S,1);
    // Push(S,3);
    // Push(S,2);

    // if (StackEmpty(S)) cout<<"链栈为空!"<<endl;
    // else PrintStack(S);

    // int x;
    // for (int i = 0; i < 5; i++) {
    //     if (Pop(S,x)) {
    //         cout<<"弹出栈顶元素为:"<<x<<endl;
    //     } else {
    //         cout<<"弹出栈顶元素失败!"<<endl;
    //     }
    // }
    // DestroyStack(S);

    // 不带头结点测试
    LinkStack S;
    InitStack2(S);

    if (StackEmpty2(S)) cout<<"链栈为空!"<<endl;
    else PrintStack2(S);

    Push2(S,1);
    Push2(S,3);
    Push2(S,2);

    if (StackEmpty2(S)) cout<<"链栈为空!"<<endl;
    else PrintStack2(S);

    int x;
    for (int i = 0; i < 5; i++) {
        if (Pop2(S,x)) {
            cout<<"弹出栈顶元素为:"<<x<<endl;
        } else {
            cout<<"弹出栈顶元素失败!"<<endl;
        }
    }
    DestroyStack2(S);
    
    system("pause");
    return 0;
}

带头结点

在这里插入图片描述

不带头结点

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值