自学内容网 自学内容网

学习C++的第八天!

#include <iostream>
template <typename T>// 顺序表模板类
class SequenceList 
{
private:
    T* data;
    int capacity;
    int size;

public:
    SequenceList(int initialCapacity = 10) : capacity(initialCapacity), size(0) {
        data = new T[capacity];
    }

    ~SequenceList() {
        delete[] data;
    }

    void insert(int index, T element) {
        if (index < 0 || index > size) {
            std::cout << "Invalid index." << std::endl;
            return;
        }
        if (size == capacity) {
            expandCapacity();
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = element;
        size++;
    }

    void remove(int index) {
        if (index < 0 || index >= size) {
            std::cout << "Invalid index." << std::endl;
            return;
        }
        for (int i = index; i < size - 1; i++) {
            data[i] = data[i + 1];
        }
        size--;
    }

    T get(int index) const {
        if (index < 0 || index >= size) {
            std::cout << "Invalid index." << std::endl;
            return T();
        }
        return data[index];
    }

    int getSize() const {
        return size;
    }

private:
    void expandCapacity() {
        T* newData = new T[capacity * 2];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        delete[] data;
        data = newData;
        capacity *= 2;
    }
};
template <typename T>// 栈模板类
class Stack {
private:
    SequenceList<T> list;

public:
    void push(T element) {
        list.insert(0, element);
    }

    void pop() {
        list.remove(0);
    }

    T top() const {
        return list.get(0);
    }

    bool empty() const {
        return list.getSize() == 0;
    }
};
template <typename T>// 队列模板类
class Queue {
private:
    SequenceList<T> list;

public:
    void enqueue(T element) {
        list.insert(list.getSize(), element);
    }

    void dequeue() {
        list.remove(0);
    }

    T front() const {
        return list.get(0);
    }

    bool empty() const {
        return list.getSize() == 0;
    }
};
int main() 
{
    // 测试顺序表
    SequenceList<int> sl;
    sl.insert(0, 10);
    sl.insert(1, 20);
    sl.insert(2, 30);
    std::cout << "顺序表元素:";
    for (int i = 0; i < sl.getSize(); i++) 
    {
        std::cout << sl.get(i) << " ";
    }
    std::cout << std::endl;
    // 测试栈
    Stack<int> stack;
    stack.push(100);
    stack.push(200);
    stack.push(300);
    std::cout << "栈顶元素:" << stack.top() << std::endl;
    stack.pop();
    std::cout << "新栈顶元素:" << stack.top() << std::endl;
    // 测试队列
    Queue<int> queue;
    queue.enqueue(500);
    queue.enqueue(600);
    queue.enqueue(700);
    std::cout << "队首元素:" << queue.front() << std::endl;
    queue.dequeue();
    std::cout << "新队首元素:" << queue.front() << std::endl;
    return 0;
}

原文地址:https://blog.csdn.net/MRDL9/article/details/142603219

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!