自己封装栈和队列

#include <iostream>

using namespace std;
class mystack
{
private:
    int *data; //存储栈的容器
    int top;  //记录栈顶元素的下标
    int size; //最大容量

public:
    //无参构造
    mystack():size(10)
    {
        data=new int [size];
        top=-1;
        size=10;
    }
    //有参构造函数
    mystack(int s)
    {
        data=new int[s];
        top=-1;
        size=s;
    }
    //向栈顶插入元素
    void push(int e)
    {
        if(top==size-1)
        {
            cout<<"栈已满"<<endl;
            return;
        }
        top++;   //偏移栈顶位置
        data[top]=e; //将数据压入栈中
        cout<<"插入成功"<<endl;
    }
    //赋值给容器适配器
    mystack& operator=(const mystack& other)
       {
           if (this != &other) // 防止自赋值
           {
               // 释放当前对象的内存
               delete[] data;

               // 复制其他对象的属性
               size = other.size;
               top = other.top;

               // 分配新内存并复制数据
               data = new int[size];
               for (int i = 0; i <= top; ++i)
               {
                   data[i] = other.data[i];
               }
           }
           return *this;
       }
    void show()
    {
        if(empty())
        {
            cout<<"栈是空的"<<endl;
        }
        for(int i=top;i>=0;i--)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }
    //判空
    bool empty()
    {
        return top==-1;
    }

    //返回容纳的元素数
    int getsize()
    {
        return size;
    }
    //访问栈顶元素
    int get()
    {
        if(empty())
        {
            cout<<"操作失败"<<endl;
        }
        return data[top];
    }
    //删除栈顶元素
    void pop()
    {
        if(empty())
        {
            cout<<"栈为空,无法删除"<<endl;
        }
        cout<<"删除成功"<<endl;
        top--;
    }


};

int main()
{
    mystack s1;
    s1.push(1);
    s1.push(3);
    s1.push(5);
    s1.show();
    cout<<"*******"<<endl;
    s1.pop();
    s1.show();
    cout<<"*******"<<endl;
    cout<<"栈顶元素为"<<s1.get()<<endl;
    mystack s2;
    s2=s1;
    s2.show();



    return 0;
}

队列


#include <iostream>

using namespace std;
class queue
{
private:
    int *data;
    int size;
    int front;
    int tail;

public:
    //无参构造
    queue():size(20)
    {
        data=new int [size];
        front=0;
        tail=0;
    }
    //有参构造
    queue(int s)
    {
        data=new int [size];
        size=s;
        front=0;
        tail=0;
    }
    ~queue()
    {
        delete []data;
    }
    //判空
    int empty()
    {
        return front==tail;
    }
    //判满
    int full()
    {
        return (tail+1)%size==front;
    }
    // 赋值给容器适配器
    queue &operator=(const queue &other)
    {
        //防止自己给自己赋值
        if(this==&other)
        {
            return *this;
        }

        //释放当前空间
        delete []data;

        //重新分配内存
        size=other.size;
        data=new int[size];

        for(int i=0;i<size;i++)
        {
            data[i]=other.data[i];
        }
        front=other.front;
        tail=other.tail;
        return *this;
    }

    //访问第一个元素
    int get()
    {
        if(empty())
        {
            cout<<"队列是空的"<<endl;
            return -1;
        }
        return data[front];
    }
    //访问最后一个元素
    int get_tail()
    {
        if(empty())
        {
            cout<<"队列是空的"<<endl;
            return -1;
        }
        return data[tail-1];
    }
    //返回容纳的元素数
    int get_size()
    {
        return size;
    }
    //向队列尾部插入元素
    void push(int e)
    {
        //判断逻辑
        if(full())
        {
            cout<<"插入失败"<<endl;
        }
        //入队逻辑
        data[tail]=e;
        //队尾后移
        tail=(tail+1)%size;
        cout<<"入队成功"<<endl;
    }
    //删除首个元素
    void pop()
    {
        if(empty())
        {
            cout<<"删除失败"<<endl;
        }
        front=(front+1)%size;
    }
    //输出栈中的元素
    void show()
    {
        //判断逻辑
        if(empty())
        {
            cout<<"输出失败"<<endl;
            return;

        }
        for(int i=front;i!=tail;i=(i+1)%size)
        {
            cout<<data[i]<<"  ";
        }
        cout<<endl;
    }
};

int main()
{
    queue q;
    q.push(3);
    q.push(4);
    q.push(5);
    q.push(7);
    q.show();
    cout<<"第一个成员是"<<q.get()<<endl;
    cout<<"最后一个成员是"<<q.get_tail()<<endl;
    cout<<"*******"<<endl;
    //删除首个元素
    q.pop();
    q.show();
    cout<<"******"<<endl;
    //赋值
    queue q1;
    q1=q;
    q1.show();

    return 0;
}

思维导图

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值