首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >C++ 共享栈

C++ 共享栈

作者头像
Dragon水魅
发布2026-01-23 19:16:28
发布2026-01-23 19:16:28
840
举报
总结归纳
  1. 共享栈的实现,就是申请一块内存,两个栈共同使用同一块内存空间,一个从左往右存,一个从右往左存。
  2. 共享栈存满的判断条件是:S.top1 + 1 == S.top2(指针指向栈顶的情况下)。
代码实现
代码语言:javascript
复制
/*
共享栈
*/

#define MaxSize 10 // 栈中元素的最大个数

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

typedef int ElemType;

struct ShStack {
    ElemType data[MaxSize]; // 静态数组
    int top1;               // 栈顶指针
    int top2;
};

// 初始化栈
void InitStack(ShStack &S) {
    S.top1 = -1;
    S.top2 = MaxSize; // 初始化指针,指向栈顶
}

// 判断栈空
bool StackEmpty(ShStack S) {
    if (S.top1 == -1 && S.top2 == MaxSize) {
        return true;
    } else {
        return false;
    }
}

// 判断栈满
bool StackFull(ShStack S) {
    if (S.top1 + 1 == S.top2) {
        return true;
    } else {
        return false;
    }
}

// 栈1入栈
bool Push1(ShStack &S, ElemType x) {
    if (StackFull(S)) {
        return false;
    } else {
        S.top1++;
        S.data[S.top1] = x; // S.top0指向栈顶
        return true;
    }
}

// 栈2入栈
bool Push2(ShStack &S, ElemType x) {
    if (StackFull(S)) {
        return false;
    } else {
        S.top2--;
        S.data[S.top2] = x; // S.top1指向栈顶
        return true;
    }
}

// 栈1出栈
bool Pop1(ShStack &S, ElemType &x) {
    if (S.top1 == -1) { // 栈空
        return false;
    } else {
        x = S.data[S.top1--]; // 先赋值再--
        return true;
    }
}

// 栈2出栈
bool Pop2(ShStack &S, ElemType &x) {
    if (S.top2 == MaxSize) { // 栈空
        return false;
    } else {
        x = S.data[S.top2++]; // 先赋值再++
        return true;
    }
}

// 读取栈1栈顶元素
bool GetTop1(ShStack S, ElemType &x) {
    if (S.top1 == -1) {
        return false;
    } else {
        x = S.data[S.top1];
        return true;
    }
}

// 读取栈2栈顶元素
bool GetTop2(ShStack S, ElemType &x) {
    if (S.top2 == MaxSize) {
        return false;
    } else {
        x = S.data[S.top2];
        return true;
    }
}

// 栈的置空
void DestroyStack(ShStack &S) {
    S.top1 = -1;
    S.top2 = MaxSize;
}

int main() {
    // ShStack *S = (ShStack*)malloc(sizeof(ShStack)); // malloc方法申请空间,此方法需要将后面的结构体S改写为指针*S
    ShStack S;
    ElemType top1, top2, pop1, pop2;
    InitStack(S);
    cout << "栈是否为空:" << StackEmpty(S) << endl;

    for (int i = 0; i < 5; i++) { // 栈1入栈
        Push1(S, i);
    }

    GetTop1(S, top1);
    cout << "栈1栈顶元素:" << top1 << endl;

    for (int j = 10; j < 20; j++) { // 栈2入栈
        Push2(S, j);
    }

    GetTop2(S, top2);
    cout << "栈2栈顶元素:" << top2 << endl;

    cout << "栈是否存满:" << StackFull(S) << endl;

    Pop1(S, pop1);
    cout << "栈1出栈元素:" << pop1 << endl;
    Pop2(S, pop2);
    cout << "栈2出栈元素:" << pop2 << endl;

    GetTop1(S, top1);
    cout << "栈1栈顶元素:" << top1 << endl;

    cout << "栈是否存满:" << StackFull(S) << endl;

    DestroyStack(S);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-01-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 总结归纳
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档