坐牢第三十四天(c++)

一.作业

1.栈的手写

#include <iostream>
using namespace std;
// 封装一个栈
class stcak
{
private:
    int *data;    //
    int max_size; // 最大容量
    int top;      // 下标
public:
    // 无参构造函数
    stcak();
    // 有参构造函数
    stcak(int size);
    // 拷贝构造函数
    stcak(const stcak &other);
    // 析构函数
    ~stcak();
    // 判空函数
    bool empty();
    // 判满函数
    bool full();
    // 扩容函数
    void resize(int new_size);
    // 返回元素个数函数
    int size();
    // 向栈顶插入元素函数
    void push(int value);
    // 删除栈顶元素函数
    int pop();
    // 访问栈顶元素函数
    int get_top();
    // 赋值重载函数
    stcak &operator=(const stcak &other);
    // 遍历栈里元素函数
    void show();
};
// 无参构造函数
stcak::stcak() : max_size(10)
{
    data = new int[10];
    max_size = 10;
    top = -1;
    cout << "无参构造" << endl;
}
// 有参构造函数
stcak::stcak(int size)
{
    data = new int[size];
    max_size = size;
    top = -1;
    cout << "有参构造" << endl;
}
// 拷贝构造函数
stcak::stcak(const stcak &other)
{
    max_size = other.max_size;
    top = other.top;
    data = new int[max_size];
    for (int i = 0; i <= top; i++)
    {
        data[i] = other.data[i];
    }
    cout << "拷贝构造" << endl;
}
// 析构函数
stcak::~stcak()
{
    delete[] data;
    cout << "析构函数" << endl;
}
// 判空函数
bool stcak::empty()
{
    return top == -1;
}
// 判满函数
bool stcak::full()
{
    return top == max_size - 1;
}
// 扩容函数
void stcak::resize(int new_size)
{
    int *new_data = new int[new_size];
    for (int i = 0; i <= top; i++)
    {
        new_data[i] = data[i];
    }
    delete[] data;
    data = new_data;
    max_size = new_size;
}
// 返回元素个数函数
int stcak::size()
{
    return top + 1;
}
// 向栈顶插入元素函数
void stcak::push(int value)
{
    if (full())
    {
        // 调用扩容函数
        resize(max_size * 2);
    }
    data[++top] = value;
}
// 删除栈顶元素函数
int stcak::pop()
{
    if (empty())
    {
        cout << "栈是空的";
        return -1;
    }
    return data[top--]; // 出栈
}
// 访问栈顶元素函数
int stcak::get_top()
{
    if (empty())
    {
        cout << "栈是空的";
        return -1;
    }
    return data[top]; // 出栈
}
// 赋值重载函数
stcak &stcak::operator=(const stcak &other)
{
    if (this == &other)
    {
        return *this;
    }
    delete[] data;
    max_size = other.max_size;
    top = other.top;
    data = new int[max_size];
    for (int i = 0; i <= top; i++)
    {
        data[i] = other.data[i];
    }
    return *this;
}
// 遍历栈里元素函数
void stcak::show()
{
    if (empty())
    {
        cout << "栈是空的";
        return;
    }
    cout << "栈里元素有:"<<endl;
    for (int i = 0; i <= top; i++)
    {
        cout<< data[i] <<'\t'; 
    }
    cout <<endl;
}
/******************主函数*********************/ 
int main()
{
    stcak s1(20);
    cout << s1.size() << endl;
    s1.push(1);
    s1.push(2);
    s1.show();
    cout << s1.size() << endl;
    stcak s2 = s1;
    return 0;
}

效果图:

2.队列的手写

#include <iostream>
using namespace std;
class queue
{
private:
    int *data;    // 容器
    int max_size; // 最大容量
    int front;    // 头下标
    int tail;     // 尾下标
public:
    // 无参构造函数
    queue();
    // 有参构造函数
    queue(int size);
    // 拷贝构造函数
    queue(const queue &other);
    // 析构函数
    ~queue();
    // 判空函数
    bool empty();
    // 判满函数
    bool full();
    // 扩容函数
    void resize(int new_size);
    // 元素个数函数
    int size();
    // 向队列尾部插入元素函数
    void push(int value);
    // 删除首个元素函数 出队
    int pop();
    // 遍历队列元素
    void show();
    // 赋值重载函数
    queue &operator=(const queue &other);
};
// 无参构造函数
queue::queue() : max_size(10)
{
    data = new int[10];
    max_size = 10;
    front = tail = -1;
    cout << "无参构造" << endl;
}
// 有参构造函数
queue::queue(int size)
{
    data = new int[size];
    max_size = size;
    front = tail = 0;
    cout << "有参构造" << endl;
}
// 拷贝构造函数
queue::queue(const queue &other)
{
    max_size=other.max_size;
    front=other.front;
    tail=other.tail;
    data=new int[max_size];
    for (int i = front; i != tail; i = (i + 1) % max_size)
    {
        data[i]=other.data[i];
    }   
    cout << "拷贝构造" << endl;
}
// 析构函数
queue::~queue()
{
    delete[] data;
    cout << "析构函数" << endl;
}
// 判空函数
bool queue::empty()
{
    return front == tail;
}
// 判满函数
bool queue::full()
{
    return (tail + 1) % max_size == front;
}
// 元素个数函数
int queue::size()
{
    return (tail - front + max_size) % max_size;
}
// 扩容函数
void queue::resize(int new_size)
{
    int *new_data = new int[new_size];
    for (int i = front; i <= tail; i++)
    {
        new_data[i] = data[i];
    }
    data = new_data;
    max_size = new_size;
}
// 向队列尾部插入元素函数
void queue::push(int value)
{
    if (full())
    {
        // 调用扩容函数
        resize(max_size * 2);
    }
    data[tail] = value;
    tail = (tail + 1) % max_size;
}
// 删除首个元素函数 出队
int queue::pop()
{
    if (empty())
    {
        cout << "队列为空" << endl;
        return -1;
    }
    cout << data[front] << "出队" << endl;
    front = (front + 1) % max_size;
    return 0;
}
// 遍历队列元素
void queue::show()
{
    if (empty())
    {
        cout << "队列为空" << endl;
        return;
    }
    cout << "队列元素:" << endl;
    for (int i = front; i != tail; i = (i + 1) % max_size)
    {
        cout << data[i] << '\t';
    }
    cout << endl;
}
// 赋值重载函数
queue &queue::operator=(const queue &other)
{
    if (this == &other)
    {
        return *this;
    }
    delete []data;
    max_size=other.max_size;
    front=other.front;
    tail=other.tail;
    data=new int[max_size];
    for (int i = front; i != tail; i = (i + 1) % max_size)
    {
        data[i]=other.data[i];
    }
    cout << "拷贝赋值函数" <<endl;  
    return *this; 
}
/******************主函数*********************/
int main()
{
    queue s1(20);
    s1.push(1);
    s1.push(2);
    s1.show();
    // s1.pop();
    // s1.pop();
    // s1.show();
    queue s2=s1;
    queue s3;
    s3=s2;
    return 0;
}

效果图:

二.思维导图

相关推荐
爱coding的橙子1 小时前
CCF-CSP认证考试准备第十七天
数据结构·c++·算法
程序猿阿伟2 小时前
《C++移动语义:解锁复杂数据结构的高效之道》
数据结构·c++·html
594h23 小时前
PAT 甲级 1002题
数据结构·c++·算法
小周的C语言学习笔记5 小时前
鹏哥C语言33---循环语句 for
c语言·c++·算法
ZH_qaq5 小时前
【洛谷】P11062 【MX-X4-T2】「Jason-1」加法 的题解
c++·算法
9ilk5 小时前
【与C++的邂逅】--- C++的IO流
开发语言·c++
是小满满满满吗5 小时前
C++中的继承
开发语言·c++·python
程序猿练习生5 小时前
C++速通LeetCode简单第16题-买卖股票的最佳时机
开发语言·c++·leetcode
只对您心动5 小时前
【QT】实现TCP服务器,客户端之间的通信
linux·服务器·c语言·开发语言·c++·qt·tcp/ip
MaTF_6 小时前
《Effective C++》第三版——让自己习惯C++
开发语言·c++