自学内容网 自学内容网

c++九月27日

1.顺序表

#ifndef ARRAYLIST_H
#define ARRAYLIST_H

#include <iostream>
#include <stdexcept>

template <typename T>
class ArrayList {
private:
    T* data;          // 存储数据的数组
    int capacity;     // 数组容量
    int size;         // 当前元素数量

public:
    ArrayList(int cap = 10);
    ~ArrayList();
    void add(const T& value);
    T get(int index) const;
    int getSize() const;
};

#include "ArrayList.cpp" // 包含实现文件
#endif // ARRAYLIST_H
#include "ArrayList.h"

template <typename T>
ArrayList<T>::ArrayList(int cap) : capacity(cap), size(0) {
    data = new T[capacity];
}

template <typename T>
ArrayList<T>::~ArrayList() {
    delete[] data;
}

template <typename T>
void ArrayList<T>::add(const T& value) {
    if (size >= capacity) {
        throw std::overflow_error("ArrayList is full");
    }
    data[size++] = value;
}

template <typename T>
T ArrayList<T>::get(int index) const {
    if (index < 0 || index >= size) {
        throw std::out_of_range("Index out of range");
    }
    return data[index];
}

template <typename T>
int ArrayList<T>::getSize() const {
    return size;
}

2.栈

#ifndef STACK_H
#define STACK_H

#include <iostream>
#include <stdexcept>

template <typename T>
class Stack {
private:
    T* data;          // 存储数据的数组
    int capacity;     // 数组容量
    int top;          // 栈顶索引

public:
    Stack(int cap = 10);
    ~Stack();
    void push(const T& value);
    T pop();
    T peek() const;
    bool isEmpty() const;
};

#include "Stack.cpp" // 包含实现文件
#endif // STACK_H
#include "Stack.h"

template <typename T>
Stack<T>::Stack(int cap) : capacity(cap), top(-1) {
    data = new T[capacity];
}

template <typename T>
Stack<T>::~Stack() {
    delete[] data;
}

template <typename T>
void Stack<T>::push(const T& value) {
    if (top >= capacity - 1) {
        throw std::overflow_error("Stack is full");
    }
    data[++top] = value;
}

template <typename T>
T Stack<T>::pop() {
    if (top < 0) {
        throw std::underflow_error("Stack is empty");
    }
    return data[top--];
}

template <typename T>
T Stack<T>::peek() const {
    if (top < 0) {
        throw std::underflow_error("Stack is empty");
    }
    return data[top];
}

template <typename T>
bool Stack<T>::isEmpty() const {
    return top < 0;
}

3.队列

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <stdexcept>

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

public:
    Queue(int cap = 10);
    ~Queue();
    void enqueue(const T& value);
    T dequeue();
    bool isEmpty() const;
    int getSize() const;
};

#include "Queue.cpp" // 包含实现文件
#endif // QUEUE_H
#include "Queue.h"

template <typename T>
Queue<T>::Queue(int cap) : capacity(cap), front(0), rear(0), size(0) {
    data = new T[capacity];
}

template <typename T>
Queue<T>::~Queue() {
    delete[] data;
}

template <typename T>
void Queue<T>::enqueue(const T& value) {
    if (size >= capacity) {
        throw std::overflow_error("Queue is full");
    }
    data[rear] = value;
    rear = (rear + 1) % capacity; // 循环队列
    size++;
}

template <typename T>
T Queue<T>::dequeue() {
    if (size <= 0) {
        throw std::underflow_error("Queue is empty");
    }
    T value = data[front];
    front = (front + 1) % capacity; // 循环队列
    size--;
    return value;
}

template <typename T>
bool Queue<T>::isEmpty() const {
    return size <= 0;
}

template <typename T>
int Queue<T>::getSize() const {
    return size;
}

主函数

#include "ArrayList.h"
#include "Stack.h"
#include "Queue.h"

int main() {
    // 使用顺序表
    ArrayList<int> list;
    list.add(1);
    list.add(2);
    list.add(3);
    std::cout << "ArrayList size: " << list.getSize() << std::endl;

    // 使用栈
    Stack<int> stack;
    stack.push(1);
    stack.push(2);
    std::cout << "Stack top: " << stack.peek() << std::endl;
    std::cout << "Popped from stack: " << stack.pop() << std::endl;

    // 使用队列
    Queue<int> queue;
    queue.enqueue(1);
    queue.enqueue(2);
    std::cout << "Dequeued from queue: " << queue.dequeue() << std::endl;

    return 0;
}

思维导图


原文地址:https://blog.csdn.net/KuaiKKyo/article/details/142610399

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