c++day8list、stack、queue的类模板

顺序表

#ifndef LIST_HPP
#define LIST_HPP

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

//typedef int T;            //类型重命名
template <typename T>

//封装一个顺序表
class SeqList
{
private:
    T *ptr;                //指向堆区空间的起始地址
    int size;              //总长度
    int len = 0;           //当前顺序表实际长度

public:
    void init(int n)
    {
        this->ptr = new T[n];
        this->len = 0;
        this->size = n;
    }

    //判空
    bool empty()
    {
        return this->len == 0;
    }
    //判满
    bool full()
    {
        return this->len == this->size;
    }
    //尾插
    void push_back(T e)
    {
        if(this->full())
        {
            return ;
        }

        this->ptr[len++] = e;
    }
    //插入
    void insert(T index)
    {
        if(this->full())
        {
            return ;
        }
        int i,m;
        cout << "\n请输入要插入的位置:";
        cin >> m;
        for(i=this->len; i>=m; i--)
        {
            this->ptr[i] = this->ptr[i-1];
        }
        this->ptr[i] = index;
        this->len++;
    }
    //位置删除
    void erase(int index)
    {
        if(empty() == 1)
        {
            return;
        }
        int i;
        for(i = index - 1; i < this->len; i++)
        {
            this->ptr[i] = this->ptr[i+1];
        }
        this->len--;
    }
    //尾删
    void pop_back(int index)
    {
        if(empty() == 1)
        {
            return;
        }
        int i;
        for(i = 0; i < index; i++)
        {
            this->ptr[this->len--] = 0;
        }
    }
    //求长度
    void my_size()
    {
        cout << "长度为:" << this->len <<endl;
    }
    //获取任意位置元素
    T & my_at(int index)
    {
        static T num;
        num = this->ptr[index-1];
        return num;
    }
    //将顺序表进行排序
    void my_sort(bool flag)
    {
        int i,j;
        if(flag)
        {
            for(i = 0; i < this->len; i++)
            {
                for(j = 0; j < this->len - 1- i;j++)
                {
                    if(this->ptr[j]>this->ptr[j+1])
                    {
                        T temp = this->ptr[j];
                        this->ptr[j] = this->ptr[j+1];
                        this->ptr[j+1] = temp;
                    }
                }
            }
        }
        else
        {
            for(i = 0; i < this->len; i++)
            {
                for(j = 0; j < this->len - 1- i;j++)
                {
                    if(this->ptr[j]<this->ptr[j+1])
                    {
                        T temp = this->ptr[j];
                        this->ptr[j] = this->ptr[j+1];
                        this->ptr[j+1] = temp;
                    }
                }
            }
        }
    }
    //定义展示函数
    void show()
    {
        //判空
        if(empty() == 1)
        {
            return;
        }
        cout<<"当前顺序表中的元素分别是:";
        for(int i=0; i<this->len; i++)
        {
            cout<<this->ptr[i]<<" ";
        }
        cout<<endl;
    }
};

#endif // LIST_HPP

#ifndef STACK_HPP
#define STACK_HPP

#include <iostream>
using namespace std;

template <typename T>
class my_stack {
private:
    T *data;        // 存储栈元素的动态数组
    int capacity;   // 栈的容量
    int top;        // 栈顶元素的索引

public:
    // 构造函数
    my_stack(int size = 10) : capacity(size), top(-1)
    {
        data = new T[capacity];
    }


    // 入栈操作
    void push(const T& value)
    {
        if (top + 1 >= capacity)
        {
            resize();
        }
        data[++top] = value;
    }

    // 出栈操作
    void pop()
    {
        if (!empty())
        {
            --top;
        }
        else
        {
            cout << "Stack is empty. Cannot pop." << endl;
        }
    }

    // 查看栈顶元素
    T& peek()
    {
        if (!empty())
        {
            return data[top];
        }
        else
        {
            throw out_of_range("Stack is empty. Cannot peek.");
        }
    }

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

    // 获取栈的大小
    int size() const
    {
        return top + 1;
    }

    // 析构函数
    ~my_stack()
    {
        delete[] data;
    }
private:
    // 二倍扩容
    void resize()
    {
        capacity *= 2;
        T* newData = new T[capacity];
        for (int i = 0; i <= top; ++i)
        {
            newData[i] = data[i];
        }
        delete[] data;
        data = newData;
    }
};
#endif // STACK_HPP

#ifndef QUEUE_HPP
#define QUEUE_HPP

#include <iostream>
#include <stdexcept>
using namespace std;

template <typename T>
class my_queue {
private:
    T* data;        // 存储队列元素的动态数组
    int capacity;   // 队列的容量
    int front;      // 队头元素的索引
    int rear;       // 队尾元素的索引
    int count;      // 当前元素数量

public:
    // 构造函数
    my_queue(int size = 10) : capacity(size), front(0), rear(-1), count(0)
    {
        data = new T[capacity];
    }
    // 拷贝构造函数
    my_queue(const my_queue& other)
    {
        capacity = other.capacity;
        front = other.front;
        rear = other.rear;
        count = other.count;
        data = new T[capacity];
        for (int i = 0; i < count; ++i)
        {
            data[(front + i) % capacity] = other.data[(front + i) % capacity];
        }
    }
    // 拷贝赋值运算符
    my_queue& operator=(const my_queue& other)
    {
        if (this != &other)
        {
            delete[] data;                                           // 释放旧内存
            capacity = other.capacity;
            front = other.front;
            rear = other.rear;
            count = other.count;
            data = new T[capacity];
            for (int i = 0; i < count; ++i)
            {
                data[(front + i) % capacity] = other.data[(front + i) % capacity];
            }
        }
        return *this;
    }
    // 析构函数
    ~my_queue()
    {
        delete[] data;
    }
    // 返回队头元素
    T &my_front()
    {
        if (empty())
        {
            throw out_of_range("队列为空,无法查看队头元素。");
        }
        return data[front];
    }
    // 返回队尾元素
    T& back()
    {
        if (empty())
        {
            throw out_of_range("队列为空,无法查看队尾元素。");
        }
        return data[rear];
    }
    // 检查队列是否为空
    bool empty() const
    {
        return count == 0;
    }
    // 获取队列的大小
    int size() const
    {
        return count;
    }
    // 入队操作
    void push(const T& value)
    {
        if (count >= capacity)
        {
            resize();
        }
        rear = (rear + 1) % capacity;                       // 循环队列
        data[rear] = value;
        count++;
    }
    // 直接构造元素(emplace)
    template <typename... Args>
    void emplace(Args&&... args)
    {
        if (count >= capacity)
        {
            resize();
        }
        rear = (rear + 1) % capacity;                       // 循环队列
        data[rear] = T(std::forward<Args>(args)...);        // 使用完美转发构造元素
        count++;
    }
    // 出队操作
    void pop() {
        if (empty())
        {
            cout << "队列为空,无法出队。" << endl;
            return;
        }
        front = (front + 1) % capacity;                     // 循环队列
        count--;
    }
    // 交换两个队列
    void swap(my_queue& other)
    {
        std::swap(data, other.data);
        std::swap(capacity, other.capacity);
        std::swap(front, other.front);
        std::swap(rear, other.rear);
        std::swap(count, other.count);
    }
private:
    // 扩容函数
    void resize()
    {
        capacity *= 2;
        T* newData = new T[capacity];
        for (int i = 0; i < count; ++i)
        {
            newData[i] = data[(front + i) % capacity];      // 复制元素
        }
        delete[] data;
        data = newData;
        front = 0;                                          // 重置队头索引
        rear = count - 1;                                   // 更新队尾索引
    }
};

#endif // QUEUE_HPP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值