当前位置:   article > 正文

c++相关的数据结构

c++相关的数据结构

单链表,模板加智能指针 

  1. #include <iostream>
  2. #include <memory>
  3. // 定义链表节点结构
  4. template <typename T>
  5. struct Node {
  6. T data;
  7. std::shared_ptr<Node<T>> next;
  8. Node(const T& value) : data(value), next(nullptr) {}
  9. };
  10. // 定义链表类
  11. template <typename T>
  12. class LinkedList {
  13. private:
  14. std::shared_ptr<Node<T>> head;
  15. public:
  16. LinkedList() : head(nullptr) {}
  17. // 在链表末尾插入节点
  18. void insert(const T& value) {
  19. std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
  20. if (head == nullptr) {
  21. head = newNode;
  22. } else {
  23. std::shared_ptr<Node<T>> temp = head;
  24. while (temp->next != nullptr) {
  25. temp = temp->next;
  26. }
  27. temp->next = newNode;
  28. }
  29. }
  30. // 打印链表元素
  31. void print() {
  32. std::shared_ptr<Node<T>> temp = head;
  33. while (temp != nullptr) {
  34. std::cout << temp->data << " ";
  35. temp = temp->next;
  36. }
  37. std::cout << std::endl;
  38. }
  39. };
  40. int main() {
  41. LinkedList<int> list;
  42. list.insert(1);
  43. list.insert(2);
  44. list.insert(3);
  45. list.insert(4);
  46. list.print(); // 输出: 1 2 3 4
  47. return 0;
  48. }

单链表加完美转发进阶版

  1. #include <iostream>
  2. #include <memory>
  3. #include <utility>
  4. template <typename T>
  5. class LinkedList {
  6. private:
  7. struct Node {
  8. T data;
  9. std::shared_ptr<Node> next;
  10. Node(const T& value) : data(value), next(nullptr) {}
  11. };
  12. std::shared_ptr<Node> head;
  13. public:
  14. LinkedList() : head(nullptr) {}
  15. //头插法
  16. template <typename U>
  17. void insert(U&& data) {
  18. std::shared_ptr<Node> newNode = std::make_shared<Node>(std::forward<U>(data));
  19. newNode->next = head;
  20. head = newNode;
  21. }
  22. void print() const {
  23. std::shared_ptr<Node> current = head;
  24. while (current != nullptr) {
  25. std::cout << current->data << " ";
  26. current = current->next;
  27. }
  28. std::cout << std::endl;
  29. }
  30. };
  31. int main() {
  32. LinkedList<int> list;
  33. list.insert(3);
  34. list.insert(5);
  35. list.insert(7);
  36. list.print(); // 输出链表内容: 7 5 3
  37. return 0;
  38. }

双链表  

  1. #include <iostream>
  2. #include <memory>
  3. // 定义链表节点结构
  4. template <typename T>
  5. struct Node {
  6. T data;
  7. std::shared_ptr<Node<T>> prev;
  8. std::shared_ptr<Node<T>> next;
  9. Node(const T& value) : data(value), prev(nullptr), next(nullptr) {}
  10. };
  11. // 定义链表类
  12. template <typename T>
  13. class DoublyLinkedList {
  14. private:
  15. std::shared_ptr<Node<T>> head;
  16. std::shared_ptr<Node<T>> tail;
  17. public:
  18. DoublyLinkedList() : head(nullptr), tail(nullptr) {}
  19. // 在链表末尾插入节点
  20. void insert(const T& value) {
  21. std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
  22. if (head == nullptr) {
  23. head = newNode;
  24. tail = newNode;
  25. }
  26. else {
  27. tail->next = newNode;
  28. newNode->prev = tail;
  29. tail = newNode;
  30. }
  31. }
  32. // 打印链表元素
  33. void print() {
  34. std::shared_ptr<Node<T>> temp = head;
  35. while (temp != nullptr) {
  36. std::cout << temp->data << " ";
  37. temp = temp->next;
  38. }
  39. std::cout << std::endl;
  40. }
  41. };
  42. int main() {
  43. DoublyLinkedList<int> list;
  44. list.insert(1);
  45. list.insert(2);
  46. list.insert(3);
  47. list.insert(4);
  48. list.print(); // 输出: 1 2 3 4
  49. return 0;
  50. }

双链表进阶版

  1. //双链表
  2. template<typename T>
  3. class DoubleLinkList {
  4. private:
  5. struct Node {
  6. std::shared_ptr<Node> next;
  7. std::shared_ptr<Node> prev;
  8. T data;
  9. Node(const T& value): data(value),next(nullptr),prev(nullptr){}
  10. };
  11. std::shared_ptr<Node> head;
  12. std::shared_ptr<Node> tail;
  13. public:
  14. DoubleLinkList():head(nullptr),tail(nullptr){}
  15. template<typename U>
  16. void insert(U&& value) {
  17. std::shared_ptr<Node> NewNode = std::make_shared<Node>(std::forward<U>(value));
  18. if (head == nullptr) {
  19. head = NewNode;
  20. tail = NewNode;
  21. }
  22. else {
  23. //尾插法和头指针无关
  24. tail->next= NewNode;
  25. NewNode->prev = tail;
  26. tail = NewNode;
  27. }
  28. }
  29. void print() {
  30. std::shared_ptr<Node> tmp = head;
  31. while (tmp != nullptr) {
  32. std::cout << tmp->data << std::endl;
  33. tmp = tmp->next;
  34. }
  35. }
  36. };

  1. #include <iostream>
  2. #include <memory>
  3. // 定义栈节点结构
  4. template <typename T>
  5. struct Node {
  6. T data;
  7. std::shared_ptr<Node<T>> next;
  8. Node(const T& value) : data(value), next(nullptr) {}
  9. };
  10. // 定义栈类
  11. template <typename T>
  12. class Stack {
  13. private:
  14. std::shared_ptr<Node<T>> top;
  15. public:
  16. Stack() : top(nullptr) {}
  17. // 入栈操作
  18. void push(const T& value) {
  19. std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
  20. newNode->next = top;
  21. top = newNode;
  22. }
  23. // 出栈操作
  24. void pop() {
  25. if (top != nullptr) {
  26. std::shared_ptr<Node<T>> temp = top;
  27. top = top->next;
  28. temp.reset();
  29. }
  30. }
  31. // 获取栈顶元素
  32. T& peek() {
  33. if (top != nullptr) {
  34. return top->data;
  35. }
  36. throw std::runtime_error("Stack is empty.");
  37. }
  38. // 判断栈是否为空
  39. bool isEmpty() {
  40. return top == nullptr;
  41. }
  42. };
  43. int main() {
  44. Stack<int> stack;
  45. stack.push(1);
  46. stack.push(2);
  47. stack.push(3);
  48. std::cout << "Top element: " << stack.peek() << std::endl; // 输出: Top element: 3
  49. stack.pop();
  50. std::cout << "Top element: " << stack.peek() << std::endl; // 输出: Top element: 2
  51. return 0;
  52. }

升级栈 

  1. #include <iostream>
  2. #include <memory>
  3. #include <utility>
  4. //栈
  5. template<typename T>
  6. class Stack {
  7. private:
  8. struct Node {
  9. std::shared_ptr<Node> next;
  10. T data;
  11. Node(const T& value):data(value),next(nullptr) {}
  12. };
  13. std::shared_ptr<Node> top;
  14. public:
  15. Stack():top(nullptr) {}
  16. //入栈
  17. template<typename U>
  18. void push(U&& value) {
  19. std::shared_ptr<Node> NewNode = std::make_shared<Node>(std::forward<U>(value));
  20. if (top == nullptr) {
  21. top = NewNode;
  22. }
  23. else {
  24. NewNode->next = top;
  25. top = NewNode;
  26. }
  27. }
  28. //出栈
  29. void Pop() {
  30. if (top != nullptr) {
  31. std::shared_ptr<Node> tmp = top;
  32. top = top->next;
  33. tmp.reset();//很像指针的delete
  34. }else {
  35. throw std::exception("栈为空1");
  36. }
  37. }
  38. //获取栈顶元素
  39. T& Peek() const{
  40. if (top != nullptr) {
  41. return top->data;
  42. }
  43. else {
  44. throw std::exception("栈为空2");
  45. }
  46. }
  47. //判断栈是否为空
  48. bool IsEmpty() {
  49. if (top == nullptr) {
  50. return false;
  51. }
  52. else
  53. return true;
  54. }
  55. };
  56. int main() {
  57. Stack<int> stack;
  58. try {
  59. stack.push(1);
  60. /*stack.push(2);
  61. stack.push(3);*/
  62. std::cout << "Top element: " << stack.Peek() << std::endl; // 输出: Top element: 3
  63. stack.Pop();
  64. stack.Pop();
  65. std::cout << "Top element: " << stack.Peek() << std::endl; // 输出: Top element: 2
  66. }
  67. catch (const std::exception& e) {
  68. std::cout << "捕获到异常: " << e.what() << std::endl;
  69. }
  70. return 0;
  71. }

 

 队列

  1. #include <iostream>
  2. #include <memory>
  3. // 定义队列节点结构
  4. template <typename T>
  5. struct Node {
  6. T data;
  7. std::shared_ptr<Node<T>> next;
  8. Node(const T& value) : data(value), next(nullptr) {}
  9. };
  10. // 定义队列类
  11. template <typename T>
  12. class Queue {
  13. private:
  14. std::shared_ptr<Node<T>> front;
  15. std::shared_ptr<Node<T>> rear;
  16. public:
  17. Queue() : front(nullptr), rear(nullptr) {}
  18. // 入队操作
  19. void enqueue(const T& value) {
  20. std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(value);
  21. if (rear == nullptr) {
  22. front = newNode;
  23. rear = newNode;
  24. } else {
  25. rear->next = newNode;
  26. rear = newNode;
  27. }
  28. }
  29. // 出队操作
  30. void dequeue() {
  31. if (front != nullptr) {
  32. std::shared_ptr<Node<T>> temp = front;
  33. front = front->next;
  34. temp.reset();
  35. if (front == nullptr) {
  36. rear = nullptr;
  37. }
  38. }
  39. }
  40. // 获取队头元素
  41. T& peek() {
  42. if (front != nullptr) {
  43. return front->data;
  44. }
  45. throw std::runtime_error("Queue is empty.");
  46. }
  47. // 判断队列是否为空
  48. bool isEmpty() {
  49. return front == nullptr;
  50. }
  51. };
  52. int main() {
  53. Queue<int> queue;
  54. queue.enqueue(1);
  55. queue.enqueue(2);
  56. queue.enqueue(3);
  57. std::cout << "Front element: " << queue.peek() << std::endl; // 输出: Front element: 1
  58. queue.dequeue();
  59. std::cout << "Front element: " << queue.peek() << std::endl; // 输出: Front element: 2
  60. return 0;
  61. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/741514
推荐阅读
相关标签
  

闽ICP备14008679号