当前位置: 首页 > news >正文

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;
}

思维导图


http://www.mrgr.cn/news/38107.html

相关文章:

  • C++学习,线程同步
  • C++编程基础:内联函数、auto关键字、基于范围的for循环和nullptr
  • 一文彻底搞懂Fine-tuning - 预训练和微调(Pre-training vs Fine-tuning)
  • 第一批学习大模型的程序员,已经碾压同事了,薪资差距都甩出一条街了...
  • [Mysql]锁总结
  • 【吊打面试官系列-MySQL面试题】实践中如何优化 MySQL?
  • 【周末推荐】替换SwitchyOmega的Chrome浏览器插件
  • 【C语言】指针篇 | 万字笔记
  • 9.26作业
  • Linux网络管理-NetworkManager管理工具nmcli命令详解
  • 【Vite】如何阻止Vite对较小图片的默认处理
  • 使用Python实现图形学的环境映射算法
  • Java底层并发:线程、volatile
  • 清华提出时间序列大模型:面向通用时序分析的生成式Transformer | ICML 2024
  • 转行大模型的必要性与未来前景:迎接智能时代的浪潮
  • element-plus中日历组件设置起始为周一
  • protobuff中的required有什么用?
  • 【深度学习】05-Rnn循环神经网络-01- 自然语言处理概述/词嵌入层/循环网络/文本生成案例精讲
  • MYSQL-查看表中字段属性语法(三)
  • 【Mysql】Mysql数据库基本操作-------DDL(下)