数据结构 栈的顺序和链式结构 C++实现

顺序存储结构的栈

栈的定义

const int MAXSIZE = 50;
typedef int Elemtype;
typedef struct
{
    Elemtype data[MAXSIZE];
    int top;
} SqStack;

栈的初始化

令栈顶指针 top 为 -1 即可;

void InitStack(SqStack &S) {
    S.top = -1;
}

判断栈空

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

进栈操作

进栈前需要先判断栈是否已满,若栈已满,则进栈失败;若栈未满,则让新的元素放在当前栈顶的上面即可;操作为让当前栈顶加一,然后放入数据即可;

bool Push(SqStack &S, Elemtype e) {
    if(S.top == MAXSIZE - 1) {      //栈满
        return false;
    }
    S.data[++S.top] = e;
    return true;
}

出栈操作

当栈不为空时,使栈顶 top 减一即可,此处的出栈并非真正的将 data[top] 的元素取出,是通过 top 减一的方式来达到出栈的目的,top 减一后,栈顶就将变成原栈顶的下一个元素,与之前的栈顶无关了,是一种逻辑上的出栈;

bool Pop(SqStack &S, Elemtype &d) {
    if(StackEmpty(S)) {
        return false;
    }
    d = S.data[S.top--];
    return true;
}

读取栈顶元素

bool GetTop(SqStack S, Elemtype &e) {
    if(StackEmpty(S)) {
        return false;
    }
    e = S.data[S.top];
    return true;
}

清空栈

bool clear(SqStack &S) {
    S.top = -1;
}

测试

#include<iostream>
using namespace std;
const int MAXSIZE = 50;
typedef int Elemtype;
typedef struct
{
    Elemtype data[MAXSIZE];
    int top;
} SqStack;

void InitStack(SqStack &S) {
    S.top = -1;
}

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

bool Push(SqStack &S, Elemtype e) {
    if(S.top == MAXSIZE - 1) {      //栈满
        return false;
    }
    S.data[++S.top] = e;
    return true;
}

bool Pop(SqStack &S, Elemtype &d) {
    if(StackEmpty(S)) {
        return false;
    }
    d = S.data[S.top--];
    return true;
}

bool GetTop(SqStack S, Elemtype &e) {
    if(StackEmpty(S)) {
        return false;
    }
    e = S.data[S.top];
    return true;
}

bool clear(SqStack &S) {
    S.top = -1;
}

int main() {
    SqStack S;
    InitStack(S);
    Push(S, 10);
    Push(S, 20);
    printf("是否为空:%d\n", StackEmpty(S));
    Elemtype e;
    GetTop(S, e);
    printf("栈顶元素%d\n", e);
    Pop(S, e);
    printf("出栈元素%d\n", e);
    GetTop(S, e);
    printf("出栈后的栈顶元素%d\n", e);
    clear(S);
    printf("清空栈后 是否为空:%d\n", StackEmpty(S));
    return 0;
}

结果:

是否为空:0
栈顶元素20
出栈元素20
出栈后的栈顶元素10
清空栈后 是否为空:1

链式存储结构的栈

此处采用带头节点的链式结构

链式栈的定义

typedef int ElemType; 
typedef struct Linknode{
    ElemType data;
    struct Linknode *next;
} *LiStack;

栈的初始化

void InitStack(LiStack &S) {
    S = (LiStack)malloc(sizeof(struct Linknode));
    S->next = NULL;
}

判断栈空

bool StackEmpty(LiStack S) {
    if(S->next) {
        return false;
    }
    return true;
}

入栈

bool Push(LiStack &S, ElemType e) {
    LiStack node = (LiStack)malloc(sizeof(struct Linknode));
    node->data = e;
    node->next = S->next;
    S->next = node;
    return true;
}

出栈

bool Pop(LiStack &S, ElemType &e) {
    if(!S->next) {
        return false;
    }
    LiStack node = S->next;
    e = node->data;
    S->next = node->next;
    free(node);
    return true;
}

读取栈顶元素

ElemType GetTop(LiStack S) {
    if(S->next) {
        return S->next->data;
    }
    return -1;
}

测试代码

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef int ElemType; 
typedef struct Linknode{
    ElemType data;
    struct Linknode *next;
} *LiStack;

void InitStack(LiStack &S) {
    S = (LiStack)malloc(sizeof(struct Linknode));
    S->next = NULL;
}

bool StackEmpty(LiStack S) {
    if(S->next) {
        return false;
    }
    return true;
}


bool Push(LiStack &S, ElemType e) {
    LiStack node = (LiStack)malloc(sizeof(struct Linknode));
    node->data = e;
    node->next = S->next;
    S->next = node;
    return true;
}

bool Pop(LiStack &S, ElemType &e) {
    if(!S->next) {
        return false;
    }
    LiStack node = S->next;
    e = node->data;
    S->next = node->next;
    free(node);
    return true;
}

ElemType GetTop(LiStack S) {
    if(S->next) {
        return S->next->data;
    }
    return -1;
}

int main() {
    LiStack S;
    InitStack(S);
    printf("栈是否为空:%d\n", StackEmpty(S));
    Push(S, 3);
    Push(S, 5);
    printf("栈是否为空:%d\n", StackEmpty(S));
    ElemType e;
    Pop(S, e);
    printf("出栈:%d\n", e);
    e = GetTop(S);
    printf("此时栈顶元素:%d\n",e);
    Pop(S, e);
    printf("再次出栈:%d\n", e);
    printf("栈是否为空:%d\n", StackEmpty(S));
    return 0;
}

结果:

栈是否为空:1
栈是否为空:0
出栈:5
此时栈顶元素:3
再次出栈:3
栈是否为空:1

栈的应用:后缀表达式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值