C++ 栈和队列的简单封装(9.3)

1.栈的封装

代码

#include <iostream>

using namespace std;

typedef int datatype;
class Stack
{
private:
    datatype *data;
    int max_size;       //栈的大小
    int the_top;        //栈顶
public:
    Stack()
    {
        data= new int[50];
        max_size = 50;
        the_top = -1;
    }
    Stack(int a)
    {
        data= new int[a];
        max_size = a;
        the_top = -1;
    }
    ~Stack()
    {
        delete[] data;
    }

    //访问栈顶元素
    datatype &top()
    {
        return *(data+the_top);
    }
    //判空
    bool empty()
    {
        return the_top==-1;
    }
    //求栈的大小
    int size()
    {
        return the_top+1;
    }
    //插入元素
    void push(datatype e)
    {
        if(the_top==max_size-1)
        {
            cout<<"栈满,入栈失败"<<endl;
        }else
        {
            data[++the_top]=e;
        }
    }
    //删除元素
    void pop()
    {
        if(the_top==-1)
        {
            cout<<"删除失败"<<endl;
        }else
        {
            the_top--;
        }
    }
    //赋值运算符
    Stack &operator=(const Stack& other)
    {
        //自赋值检查
        if(this==&other)
        {
            return *this;
        }
        //释放旧数据
        delete [] data;
        data = nullptr;
        the_top = -1;
        //分配新数据
        max_size = other.max_size;
        data = new datatype[max_size];

        //复制数据
        the_top = other.the_top;
        for (int i = 0; i <= the_top; ++i)
        {
            data[i] = other.data[i];
        }

        // 返回当前对象的引用
        return *this;
    }

    void show()const
    {
        if(the_top==-1)
        {
            cout << "栈为空" << endl;
            return;
        }else
        {
            cout << "栈内容(从栈底到栈顶): ";
            for (int i = the_top; i >= 0; --i)
            {
                cout << data[i] << " ";
            }
            cout << endl;
        }
    }

};

int main()
{
    //插入演示
    Stack s1(10);
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.show();
    cout<<"******************"<<endl;

    //访问栈顶元素演示
    cout<<s1.top()<<endl;
    cout<<"******************"<<endl;

    //求栈的大小演示
    cout<<s1.size()<<endl;
    cout<<"******************"<<endl;

    //删除演示
    s1.pop();
    s1.show()
;    cout<<"******************"<<endl;

    //赋值=演示
    Stack s2(5);
    s2=s1;
    s2.show();
    cout<<"******************"<<endl;
    return 0;
}

 运行结果:

 

 

2.队列的封装

 代码:

#include <iostream>

using namespace std;

typedef int datatype; // 队列中存储的数据类型

class Queue
{
private:
    datatype* data;
    int front;          // 队首元素的索引
    int rear;           // 队尾元素的下一个位置的索引
    int max_size;       // 队列的最大容量
    int c_size;         // 队列当前的大小
public:
    //有参构造
    Queue(int n):front(0),rear(0),max_size(n),c_size(0)
    {
        data = new datatype[max_size];
    }
    //析构函数
    ~Queue()
    {
        delete [] data;
    }
    //访问第一个元素 my_front
    datatype my_front()
    {
        if(empty())
        {
            cout<<"队列为空"<<endl;
            return 0;
        }else
        {
            return data[front];
        }
    }
    //访问最后一个元素 back
    datatype back()
    {
        if(empty())
        {
            cout<<"队列为空"<<endl;
            return 0;
        }else
        {
            return data[(rear - 1 + max_size) % max_size]; // 循环队列处理
        }
    }
    //判空 empty
    bool empty()
    {
        return c_size==0;
    }
    //返回容纳的元素数 size
    int size()const
    {
        return c_size;
    }
    //队尾插入元素 push
    void push(datatype e)
    {
        if(c_size == max_size)  //判满
        {
            cout<<"队列已满"<<endl;
        }else
        {
          data[rear] = e;
          rear = (rear + 1) % max_size; // 循环队列处理
          ++c_size;
        }
    }
    //删除首个元素 pop
    void pop()
    {
        if(empty())
        {
            cout<<"队列为空"<<endl;
        }else
        {
            front = (front + 1) % max_size; // 循环队列处理
            --c_size;
        }
    }
    //赋值 operator=
    Queue& operator=(const Queue& other)
    {
        if (this != &other)
        {
            delete[] data; // 删除旧数据
            max_size = other.max_size;
            data = new datatype[max_size];

            // 复制数据
            c_size = other.c_size;
            front = other.front;
            rear = other.rear;
            for (int i = 0; i < c_size; ++i)
            {
                data[(front + i) % max_size] = other.data[(other.front + i) % other.max_size];
            }
        }
        return *this;
    }

    //展示函数
    void show()const
    {
        if(c_size==0)
        {
            cout<<"队列为空"<<endl;
            return;
        }else
        {
            cout << "队列内容: ";
            for (int i = 0; i < c_size; i++)
            {
                cout<<data[(front +i)% max_size]<<" ";
            }
            cout << endl;
        }
    }
};

int main()
{
    Queue q1(5);
    //插入演示
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.show();
    cout<<"******************"<<endl;

    //访问第一个元素
    cout<<q1.my_front()<<endl;
    cout<<"******************"<<endl;

    //访问最后一个元素
    cout<<q1.back()<<endl;
    cout<<"******************"<<endl;

    //返回size大小演示
    cout<<q1.size()<<endl;
    cout<<"******************"<<endl;

    //删除演示
    q1.pop();
    q1.show();
    cout<<"******************"<<endl;

    //赋值=演示
    Queue q2(6);
    q2=q1;
    q2.show();
    cout<<"******************"<<endl;

    return 0;
}

运行结果:

 

 

 

3.继承  思维导图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值