C++(9.25)

stack:

cpp 复制代码
#include <iostream>
using namespace std;
class my_stack {
private:
    int* data;       // 动态数组,用于存储栈的元素
    int len;         // 当前栈中元素的个数
    int size;    // 栈的最大容量

public:
    // 默认构造函数,初始化容量为 10
    my_stack(int cap = 10) : len(0), size(cap) {
        data = new int[size];
    }

    // 析构函数,释放动态分配的内存
    ~my_stack() {
        delete[] data;
    }

    // 复制构造函数
    my_stack(const my_stack& other) : len(other.len), size(other.size) {
        data = new int[size];
        for (int i = 0; i < len; ++i) {
            data[i] = other.data[i];
        }
    }

    // 赋值运算符重载
    my_stack& operator=(const my_stack& other) {
        if (this != &other) {
            delete[] data;
            len = other.len;
            size = other.size;
            data = new int[size];
            for (int i = 0; i < len; ++i) {
                data[i] = other.data[i];
            }
        }
        return *this;
    }

    // 返回栈中元素的个数
    int size() const {
        return len;
    }

    // 判断栈是否为空
    bool empty() const {
        return len == 0;
    }

    // 返回栈顶元素
    int& top() {
        if (empty()) {
             return -1;
        }
        return data[len - 1];
    }

    // 向栈顶插入元素
    void push(int value) {
        if (len == size) {
            return -1;
        }
        data[len++] = value;
    }

    // 删除栈顶元素
    void pop() {
        if (empty()) {
           return -1;
        }
        len--;
    }

    // 交换两个栈的内容
    void swap(my_stack& other) {
        swap(data, other.data);
        swap(len, other.len);
        swap(size, other.size);
    }
};

int main() {
    my_stack s1(5);  // 初始化栈容量为 5
    s1.push(10);
    s1.push(20);
    s1.push(30);

    cout << "容量: " << s1.size() << endl;
    cout << "栈顶: " << s1.top() << endl;

    s1.pop();
    cout << "删除后,栈顶元素 " << s1.top() << endl;

    my_stack s2(5);
    s2.push(100);
    s2.push(200);

    cout << "交换后" << endl;
    s1.swap(s2);

    cout << "栈顶: " << s1.top() << endl;
   cout << "交换后: " << s2.top() << endl;

    return 0;
}

queue:

cpp 复制代码
#include <iostream>
#include <stdexcept>
using namespace std;
class my_queue {
private:
    int* data;      // 
    int len;        // 当前队列中的元素个数
    int size;       // 队列的最大容量
    int front;   // 队首元素的索引
    int back;    // 队尾元素的索引

public:
    // 初始化容量为 10
    my_queue(int cap = 10) : len(0), size(cap), front(0), back(-1) {
        data = new int[size];
    }

    // 析构函数,释放动态分配的内存
    ~my_queue() {
        delete[] data;
    }

    // 复制构造函数
    my_queue(const my_queue& other) : len(other.len), size(other.size), front(other.front), back(other.back) {
        data = new int[size];
        for (int i = 0; i < len; i++) {
            data[i] = other.data[(front + i) % size];
        }
    }

    // 赋值运算符重载
    my_queue& operator=(const my_queue& other) {
        if (this != &other) {
            delete[] data;
            len = other.len;
            size = other.size;
            front = other.front;
            back = other.back;
            data = new int[size];
            for (int i = 0; i < len; i++) {
                data[i] = other.data[(front + i) % size];
            }
        }
        return *this;
    }

    // 返回队列中元素的个数
    int the_size() const {
        return len;
    }

    // 判断队列是否为空
    bool empty() const {
        return len == 0;
    }

    // 返回队首元素
    int& front() {
        if (empty()) {
            throw out_of_range("queue is empty");
        }
        return data[front];
    }

    // 返回队尾元素
    int& back() {
        if (empty()) {
            throw out_of_range("queue is empty");
        }
        return data[back];
    }

    // 向队列尾部添加元素
    void push(int value) {
        if (len == size) {
            throw overflow_error("queue is full");
        }
        back = (back + 1) % size;
        data[back] = value;
        ++len;
    }

    // 从队列头部删除元素
    void pop() {
        if (empty()) {
            throw out_of_range("Queue is empty");
        }
        front = (front + 1) % size;
        len--;
    }

    // 交换两个队列的内容
    void swap(my_queue& other) {
        swap(data, other.data);
       swap(len, other.len);
        swap(size, other.size);
        swap(front, other.fron);
       swap(back, other.back);
    }
};

int main() {
    my_queue q1(5);  // 初始化队列容量为 5
    q1.push(10);
    q1.push(20);
    q1.push(30);

    cout << q1.the_size() << endl;
    cout  << q1.front() << endl;
    cout  << q1.back() << endl;

    q1.pop();
    cout q1.front() << endl;

    my_queue q2(5);
    q2.push(100);
    q2.push(200);

    cout  << endl;
    q1.swap(q2);

   cout << q1.front() << endl;
    cout << q2.front() << endl;

    return 0;
}
相关推荐
John_ToDebug2 小时前
浏览器扩展延迟加载优化实战:如何让浏览器启动速度提升50%
c++·chrome·windows
是娇娇公主~2 小时前
C++ 中 std::deque 的原理?它内部是如何实现的?
开发语言·c++·stl
SuperEugene2 小时前
Axios 接口请求规范实战:请求参数 / 响应处理 / 异常兜底,避坑中后台 API 调用混乱|API 与异步请求规范篇
开发语言·前端·javascript·vue.js·前端框架·axios
Fly Wine2 小时前
Leetcode之有效字母异位词
算法·leetcode·职场和发展
xuxie993 小时前
N11 ARM-irq
java·开发语言
程序员夏末4 小时前
【LeetCode | 第七篇】算法笔记
笔记·算法·leetcode
wefly20174 小时前
从使用到原理,深度解析m3u8live.cn—— 基于 HLS.js 的 M3U8 在线播放器实现
java·开发语言·前端·javascript·ecmascript·php·m3u8
luanma1509804 小时前
PHP vs C++:编程语言终极对决
开发语言·c++·php
寂静or沉默4 小时前
2026最新Java岗位从P5-P7的成长面试进阶资源分享!
java·开发语言·面试
csdn_aspnet5 小时前
C/C++ 两个凸多边形之间的切线(Tangents between two Convex Polygons)
c语言·c++·算法