--杂项2--

将之前实现的顺序表、栈、队列都更改成模板类

#include <iostream>
#include <string.h>
using namespace std;

template <typename T>
class Stack {
private:
    T* a;
    int top;
    int size1;

public:
    Stack(int c) : a(new T[c]), top(-1), size1(c) {}
    ~Stack() { delete[] a; }

    T front() const {
        if (empty()) {
            throw out_of_range("Stack is empty");
        }
        return a[top];
    }

    T back() const {
        if (empty()) {
            throw out_of_range("Stack is empty");
        }
        return a[0];
    }

    bool empty() const {
        return top == -1;
    }

    int size() const {
        return top + 1;
    }

    void push(const T& e) {
        if (top == size1 - 1) {
            throw overflow_error("Stack is full");
        }
        a[++top] = e;
    }

    void pop() {
        if (empty()) {
            throw underflow_error("Stack underflow");
        }
        --top;
    }

    void Swap(int n, int m) {
        if (n < 1 || n > size() || m < 1 || m > size()) {
            throw out_of_range("Invalid indices for swap");
        }
        swap(a[n-1], a[m-1]);
    }

    void print() const {
        cout << "Stack: ";
        for (int i = top; i >= 0; --i) {
            cout << a[i] << " ";
        }
        cout << std::endl;
    }
};

int main() {
    try {
        Stack<int> s1(15);
        cout << "Is empty: " << (s1.empty() ? "Yes" : "No") << endl;

        s1.push(5);
        s1.push(2);
        s1.push(9);
        s1.push(1);

        s1.print();
        cout << "Size: " << s1.size() << endl;
        cout << "Front: " << s1.front() << endl;
        cout << "Back: " << s1.back() <<endl;

        s1.pop();
        cout << "After pop: ";
        s1.print();

        s1.Swap(1, 3);
        cout << "After swap(1,3): ";
        s1.print();

        // 测试其他类型
        Stack<string> s2(5);
        s2.push("World");
        s2.push("Hello");
        s2.print();
    }
    catch (const exception& e) {
        cerr << "Error: " << e.what() << endl;
    }

    return 0;
}

#include <iostream>
#include <string.h>
using namespace std;
template <typename T>
class Queue {
private:
    T* a;
    int cap;
    int size1;
    int front1;
    int rear;

public:
    Queue(int c) : a(new T[c]), cap(c), size1(0), front1(0), rear(-1) {}
    ~Queue() { delete[] a; }

    T front() const {
        if (empty()) {
            throw out_of_range("Queue is empty");
        }
        return a[front1];
    }

    T back() const {
        if (empty()) {
            throw out_of_range("Queue is empty");
        }
        return a[rear];
    }

    bool empty() const {
        return size1 == 0;
    }

    bool full() const {
        return size1 == cap;
    }

    int size() const {
        return size1;
    }

    void push(const T& e) {
        if (full()) {
            throw std::overflow_error("Queue is full");
        }
        rear = (rear + 1) % cap;
        a[rear] = e;
        size1++;
    }

    T pop() {
        if (empty()) {
            throw std::underflow_error("Queue is empty");
        }
        T e = a[front1];
        front1 = (front1 + 1) % cap;
        size1--;
        return e;
    }

    void print() const {
        if (empty()) {
            cout << "Queue is empty" <<endl;
            return;
        }
        cout << "Queue: ";
        int count = 0;
        int index = front1;
        while (count < size1) {
            cout << a[index] << " ";
            index = (index + 1) % cap;
            count++;
        }
        cout << endl;
    }
};

int main() {
    try {
        Queue<int> q1(5);
        cout << "Is empty: " << (q1.empty() ? "Yes" : "No") <<endl;

        q1.push(5);
        q1.push(2);
        q1.push(9);
        q1.push(1);

        q1.print();
        cout << "Size: " << q1.size() << endl;
        cout << "Front: " << q1.front() << endl;
        cout << "Back: " << q1.back() << endl;

        q1.pop();
        cout << "After pop: ";
        q1.print();

        // 测试其他类型
        Queue<string> q2(3);
        q2.push("Hello");
        q2.push("World");
        q2.print();

        // 测试异常
        q2.push("!");
        try {
            q2.push("Overflow");
        } catch (const std::exception& e) {
            cout << "Caught exception: " << e.what() << endl;
        }
    }
    catch (const std::exception& e) {
        cerr << "Error: " << e.what() << endl;
    }

    return 0;
}

手动将unique_ptr类功能实现出来

template <typename T,typename D=default_delete<T>>
class unique_ptr
{
private:
    T * ptr;
public:
    explicit unique_ptr(T*p=nullptr):ptr(p){}
    ~unique_ptr()noexcept{};
    T& operator *()const{return *ptr;}
    T* operator->()const{return ptr;}
    unique_ptr(const unique_ptr &)=delete;
    unique_ptr& operator=(const unique_ptr &)=delete;
    unique_ptr(unique_ptr && other) noexcept:ptr(other.ptr)
    {
        other.ptr=nullptr;
    }
    unique_ptr& operator =(unique_ptr &&other)noexcept
    {
        if(this!=&other)
        {
            reset(other.ptr);
            other.ptr=nullptr;
        }
        return *this;
    }
    T * get()const{return ptr;}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值