数据结构--栈

目录

1 . 顺序栈

代码运行结果: ​

2 . 共享栈

代码运行结果:​

 3 . 链栈(不带头结点)

代码运行结果:​

 4 . 链栈(带头结点)

代码运行结果:​


1 . 顺序栈

#include <iostream>

using namespace std;
#define 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 x) {
    if(S.top == MaxSize - 1) { //栈满 报错
        return false;
    }
    S.top += 1;
    S.data[S.top] = x;
    //    S.data[++S.top] = x;
    return true;
}
//元素出栈
bool Pop(SqStack &S, ElemType &x) {
    if(S.top == -1) { //栈空
        return false;
    }
    x = S.data[S.top];
    S.top -= 1;
    //    x = S.data[S.top--];
    return true;
}
//读取栈顶元素
bool GetPop(SqStack &S, ElemType &x) {
    if(S.top == -1) { //栈空
        return false;
    }
    x = S.data[S.top];
    return true;
}
void test() {
    SqStack S;
    InitStack(S);
    for (int i = 0; i < 10; i++) {
        Push(S, i);
    }
    if(!StackEmpty(S)) {
        cout << "Not Empty!" << endl;
    } else {
        cout << "Is Empty!" << endl;
    }
    ElemType k;
    while(!StackEmpty(S)) {
        Pop(S, k);
        cout << k << " ";
    }
    cout << endl;
}
int main() {
    test();
    return 0;
}

代码运行结果: 

2 . 共享栈

#include <iostream>

using namespace std;
#define MaxSize 50

typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];//最大容量
    //栈顶指针
    int top0;
    int top1;
} ShStack;
//初始化栈
void InitStack(ShStack &S) {
    //初始化栈顶指针
    S.top0 = -1;
    S.top1 = MaxSize;
}
//判断栈空
bool StackEmpty(ShStack S) {
    if(S.top0 == -1 && S.top1 == MaxSize) {
        return true;
    } else {
        return false;
    }
}
//判断栈满
bool StackFull(ShStack S) {
    if(S.top0 + 1 == S.top1) {
        return true;
    } else {
        return false;
    }
}
//新元素入栈
bool Push0(ShStack &S, ElemType x) {
    if(StackFull(S)) { //栈满 报错
        return false;
    }
    S.data[++S.top0] = x;
    return true;
}
bool Push1(ShStack &S, ElemType x) {
    if(StackFull(S)) { //栈满 报错
        return false;
    }
    S.data[--S.top1] = x;
    return true;
}
//元素出栈
bool Pop0(ShStack &S, ElemType &x) {
    if(S.top0 == -1) { //栈空
        return false;
    }
    x = S.data[S.top0--];
    return true;
}
bool Pop1(ShStack &S, ElemType &x) {
    if(S.top1 == MaxSize) { //栈空
        return false;
    }
    x = S.data[S.top1++];
    return true;
}
//读取栈顶元素
bool GetPop0(ShStack &S, ElemType &x) {
    if(S.top0 == -1) { //栈空
        return false;
    }
    x = S.data[S.top0];
    return true;
}
bool GetPop1(ShStack &S, ElemType &x) {
    if(S.top1 == MaxSize) { //栈空
        return false;
    }
    x = S.data[S.top1];
    return true;
}
void test() {
    ShStack S;
    InitStack(S);
    for (int i = 0; i < 10; i++) {
        Push0(S, i);
    }
    for (int i = 0; i < 10; i++) {
        Push1(S, i);
    }
    ElemType x;
    if(StackEmpty(S)) {
        cout << "Is Empty!" << endl;
    } else {
        cout << "Not Empty!" << endl;
    }
    while(GetPop0(S, x)) {
        Pop0(S, x);
        cout << x << " ";
    }
    cout << endl;
    GetPop1(S, x);
    if(StackEmpty(S)) {
        cout << "Is Empty!" << endl;
    } else {
        cout << "Not Empty!" << endl;
    }
    while(GetPop1(S, x)) {
        Pop1(S, x);
        cout << x << " ";
    }
    cout << endl;
    if(StackEmpty(S)) {
        cout << "Is Empty!" << endl;
    } else {
        cout << "Not Empty!" << endl;
    }
}
int main() {
    test();
    return 0;
}

代码运行结果:

 3 . 链栈(不带头结点)

#include <iostream>

using namespace std;
//不带头结点(推荐)
typedef int ElemType;
typedef struct LinkNode {
    ElemType data;//数据域
    struct LinkNode *next;//指针域
} *LiStack, LinkNode; //栈类型定义
//初始化链栈(不带头结点)
bool InitStack(LiStack &S) {
    S = NULL;
    return true;
}
//判断链栈是否为空
bool StackEmpty(LiStack S) {
    if(S == NULL) {
        return true;
    }
    return false;
}
//入栈操作
bool Push(LiStack &S, ElemType x) {
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    if(!p) { //申请失败
        return false;
    }
    p->data = x;
    p->next = S;
    S = p;
    return true;
}
//弹栈操作
bool Pop(LiStack &S, ElemType &x) {
    if(StackEmpty(S)) { //空栈
        return false;
    }
    LinkNode *p = S;
    S = S->next;
    x = p->data;
    free(p);
    return true;
}
//读取栈顶元素
bool GetTop(LiStack S, ElemType &x) {
    if(StackEmpty(S)) { //空栈
        return false;
    }
    x = S->data;
    return true;
}
//销毁链栈
bool DestoryStack(LiStack &S) {
    while(S != NULL) {
        LinkNode *p = S;
        S = S->next;
        free(p);
    }
    free(S);
    S = NULL;
    return true;
}
void test() {
    LiStack S;
    InitStack(S);
//    入栈
    for (int i = 0; i < 10; i++) {
        Push(S, i);
    }
//    栈顶元素
    ElemType x;
    if(GetTop(S, x)) {
        cout << "GetTop:" << x << endl;
    }
    while(!StackEmpty(S)) {
        Pop(S, x);
        cout << x << " ";
    }
    cout << endl;
//    判空
    if(StackEmpty(S)) {
        cout << "Is Empty!" << endl;
    } else {
        cout << "Not Empty!" << endl;
    }
//    销毁
    DestoryStack(S);
}
int main() {
    test();
    return 0;
}

代码运行结果:

 4 . 链栈(带头结点)

#include <iostream>

using namespace std;
//带头结点
typedef int ElemType;
typedef struct LinkNode {
    ElemType data;//数据域
    struct LinkNode *next;//指针域
} *LiStack, LinkNode; //栈类型定义
//初始化链栈(带头结点)
bool InitStack(LiStack &S) {
    S = (LinkNode *)malloc(sizeof(LinkNode));
    if(!S) { //申请失败
        return false;
    }
    S->next = NULL;
    return true;
}
//判断链栈是否为空
bool StackEmpty(LiStack S) {
    if(S->next == NULL) {
        return true;
    }
    return false;
}
//入栈操作
bool Push(LiStack &S, ElemType x) {
    LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
    if(!p) { //申请失败
        return false;
    }
    p->data = x;
    p->next = S->next;
    S->next = p;
    return true;
}
//弹栈操作
bool Pop(LiStack &S, ElemType &x) {
    if(StackEmpty(S)) { //空栈
        return false;
    }
    LinkNode *p = S->next;
    x = p->data;
    S->next = p->next;
    free(p);
    return true;
}
//读取栈顶元素
bool GetTop(LiStack S, ElemType &x) {
    if(StackEmpty(S)) { //空栈
        return false;
    }
    x = S->next->data;
    return true;
}
//销毁链栈
bool DestoryStack(LiStack &S) {
    while(S->next != NULL) {
        LinkNode *p = S->next;
        S->next = p->next;
        free(p);
    }
    free(S);
    return true;
}
void test() {
    LiStack S;
    InitStack(S);
    //    入栈
    for (int i = 0; i < 10; i++) {
        Push(S, i);
    }
    //    栈顶元素
    ElemType x;
    if(GetTop(S, x)) {
        cout << "GetTop:" << x << endl;
    }
    while(!StackEmpty(S)) {
        Pop(S, x);
        cout << x << " ";
    }
    cout << endl;
    //    判空
    if(StackEmpty(S)) {
        cout << "Is Empty!" << endl;
    } else {
        cout << "Not Empty!" << endl;
    }
    //    销毁
    DestoryStack(S);
}
int main() {
    test();
    return 0;
}

代码运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我这么好看

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值