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

1.栈的封装

代码

cpp 复制代码
#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.队列的封装

代码:

cpp 复制代码
#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 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读1 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛1 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛