一个数组实现两个栈

本文介绍了一种使用单一数组同时实现两个栈的数据结构方法,并提供了详细的C++代码实现。通过调整栈顶指针的位置,实现了两个栈之间的空间共享,有效地利用了内存资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片讲解


一个数组实现两个栈

代码实现


#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include <assert.h>
//一个数组实现两个栈(OneArrayToBothStack)
//自定义两个标记:i(i=1代表栈1,i=2代表栈2)

const int SizeStack = 20;//数组大小
template<class T>
class BothStack
{
public:
    BothStack() :count1(0), count2(0)
    {
        top1 = -1;
        top2 = SizeStack;
        //Capacity();
    }
    /*void Capacity()
    {
        if (count1 + count2 == SizeStack)
        {
            realloc(data, sizeof(int)*SizeStack * 2);
        }
    }*/
    ~BothStack()
    {}

    void Push(int i, T x);//插入
    void Pop(int i);//删除
    T GetTop(int i);//得到栈顶元素
    bool Empty(int i);//判断栈是否为空
    void Print(int i);//先打印栈1,再打印栈2
    T Size(int i);//返回栈1/2的有效元素个数
protected:
    T data[SizeStack];
    int top1, top2;//top1标记的是栈1的栈顶;top2标记的是栈2的栈顶
    int count1, count2;//记录栈有效元素个数
};
template<class T>
void BothStack<T>::Push(int i, T x)
{
    if (top1 == top2 - 1)
    {
        throw "上溢";//该栈溢出(开辟的空间不够)
    }
    if (i == 1)
    {
        data[++top1] = x;
        ++count1;
    }
    else//if (i == 2)
    {
        data[--top2] = x;
        ++count2;
    }
}
template<class T>
void BothStack<T>::Pop(int i)
{
    if (i == 1)
    {
        //assert(top1 != -1);
        if (top1 == -1)
        {
            throw "下溢";//该top1栈内暂时没有存入元素
        }
        top1 =--top1;
        --count1;
    }
    else//if (i == 2)
    {
        //assert(top2 != SizeStack);
        if (top2 == SizeStack)
        {
            throw "下溢";//该top2栈内暂时没有存入元素
        }
        top2 = ++top2;
        --count2;
    }
}
template<class T>
T BothStack<T>::GetTop(int i)
{
    if (i == 1)
    {
        //assert(top1 != -1);
        if (top1 != -1)
        {
            return data[top1];
        }
    }
    else//if (i == 2)
    {
        //assert(top2!=SizeStack);
        if (top2 != SizeStack)
        {
            return data[top2];
        }
    }
    return -1;//栈内没有存入元素
}
template<class T>
T BothStack<T>::Size(int i)
{
    if (i == 1)
    {
        return count1;
    }
    else
    {
        return count2;
    }
}
template<class T>
bool BothStack<T>::Empty(int i)
{
    if (i == 1)
    {
        if (top1 == -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else//if (i == 2)
    {
        if (top2 == SizeStack)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
template <class T>
void BothStack<T>::Print(int i)
{
    if (i == 1)
    {
        assert(top1 != -1);
        int temp1 = top1;
        for (int i = 0; i < count1; ++i)
        {
            cout << data[temp1--]<<" ";
        }
        cout << endl;
    }
    else
    {
        assert(top2 != -1);
        int temp2 = top2;
        for (int i = 0; i < count2; ++i)
        {
            cout << data[temp2++] << " ";
        }
        cout << endl;
    }
}

void TestFun()
{
    BothStack<int> bs;

    bs.Push(1, 1);
    bs.Push(1, 2);
    bs.Push(1, 3);
    bs.Push(1, 4);
    bs.Push(1, 5);

    bs.Push(2, 6);
    bs.Push(2, 7);
    bs.Push(2, 8);
    bs.Push(2, 9);
    bs.Push(2, 10);
    bs.Push(2, 20);
    bs.Push(2, 30);
    cout << "栈1: ";
    bs.Print(1);
    cout << "栈2: ";
    bs.Print(2);

    cout <<"栈1有效元素:"<<bs.Size(1)<<"  "<< "栈1顶元素:" << bs.GetTop(1) << endl;
    cout << "栈2有效元素:" << bs.Size(2)<<"  " << "栈2顶元素:" << bs.GetTop(2) << endl;

    bs.Pop(1);
    bs.Print(1);

    bs.Pop(2);
    bs.Print(2);
}
int main()
{
    TestFun();
    system("pause");
    return 0;
}

实现结果


这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值