顺序栈、共享栈、链式存储结构的栈——数据结构
顺序栈、共享栈、链式存储结构的栈——数据结构

顺序栈、共享栈、链式存储结构的栈——数据结构

#include <iostream>
using namespace std;
#define Maxsize 50
#define element int

//顺序栈
class Stack
{
private:
    int top=-1;
    element data[Maxsize];
public:
    //栈空
    bool Isempty()
    {
        if(top<0)return true;
        return false;
    }

    //栈满
    bool Isfull()
    {
        if(top>=Maxsize)return true;
        return false;
    }

    //压栈
    void Push(element a)
    {
        if (Isfull()){
            printf("栈空");
            return;
        }
        data[++top]=a;
    }

    //出栈
    element Pop()
    {
        if(Isempty()){
            printf("栈空!");
            return-1;
        }
        return data[top--];
    }

};

//共享栈
class Share_Stack
{
private:
    element data[Maxsize];
    int top1=-1;
    int top2=Maxsize;
public:
    bool Isempty()
    {
        if(top1<0&&top2>=Maxsize)return true;
        return false;
    }

    bool Isfull()
    {
        if(top1>=top2)
        {
            printf("栈满");
            return true;
        }
        return false;
    }

    void Push1(element a)
    {
        if(Isfull()){
            cout<<"栈满";
            return;
        }
        data[++top1]=a;
    }

    void Push2(element a)
    {
        if(Isfull()){
            cout<<"栈满";
            return;
        }
        data[--top2]=a;
    }

    element pop1()
    {
        if(top1<0){
            cout<<"栈空";
            return;
        }
        return data[top1--];
    }

    element pop2()
    {
        if (top2>Maxsize+1)
        {
            cout<<"栈空";
            return;
        }
        return data[top2++];
    }
};

//链式存储结构栈
class Stack_link
{
    private:
        element data;
        Stack_link *next;
    public:
        bool Isempty()
        {
            if(next==NULL)return true;
            return false;
        }

        void Push(element a)
        {
            Stack_link *p=new Stack_link;
            p->data=a;
            p->next=next;
            next=p;
        }

        element Pop()
        {
            if(Isempty()){
                cout<<"栈空";
                return -1;
            }
            Stack_link *temp=next;
            int a=temp->data;
            next=next->next;
            delete temp;
            return a;
        }
};

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注