用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

  1. // @Time : 2023-10-23 20:09:23
  2. // @FileName: model.h
  3. // @Author : 423A35C7
  4. // @Software: VSCode
  5. #ifndef _MODEL
  6. #define _MODEL
  7. #include "constants.h"
  8. #include <bits/stdc++.h>
  9. // using namespace std; // 会导致byte冲突
  10. class Model {
  11. // virtual void init(...) = 0;
  12. };
  13. template <class T, class Ref, class Ptr>
  14. struct __SingleQueueModel_iterator;
  15. template <typename T>
  16. class SingleQueueModel : Model {
  17. public: // 这里用保护属性会在view的resfresh里报错
  18. class Node;
  19. private:
  20. // unique_ptr<Node> head(new Node()); // 这样会报错error: expected identifier before 'new'
  21. // vector内对一个对象可能有多个引用,所以可能使用了unique_ptr就无法放置在vector里
  22. std::shared_ptr<Node> head = std::make_shared<Node>(); // 好像用unique_ptr直接初始化会报错
  23. Node *tail = head.get();
  24. typedef __SingleQueueModel_iterator<T, const T &, const T *> const_iterator;
  25. protected:
  26. int_ length = 0;
  27. public:
  28. void init(){};
  29. void push(const T &data) {
  30. this->tail->next = new Node(data); // head指向的头结点无值
  31. this->tail = this->tail->next; // tail指向的始终有值
  32. this->length ++;
  33. }
  34. // Status pop(T&) = 0;
  35. T shift() {
  36. if (this->head->next == this->tail) {
  37. this->tail = this->head.get(); // 只剩下一个元素的情况下删除后尾指针应指向头结点
  38. }
  39. Node node = *this->head->next; // 这个临时的结点在退出此函数时也会调用析构函数
  40. delete this->head->next; // 这里会调用析构函数
  41. this->head->next = node.next;
  42. this->length --;
  43. return node.data;
  44. }
  45. T &top() {
  46. return this->head->next->data;
  47. }
  48. int_ get_length() {
  49. return this->length;
  50. }
  51. // https://blog.csdn.net/qq_54851255/article/details/123939684
  52. const_iterator begin() const {
  53. return const_iterator(this->head->next);
  54. }
  55. const_iterator end() const {
  56. return const_iterator(this->tail->next);
  57. }
  58. };
  59. template <class T, class Ref, class Ptr>
  60. struct __SingleQueueModel_iterator {
  61. typedef class SingleQueueModel<T>::Node Node;
  62. typedef __SingleQueueModel_iterator<T, Ref, Ptr> self;
  63. Node *_node;
  64. __SingleQueueModel_iterator(Node *x)
  65. : _node(x) {}
  66. Ref operator*() {
  67. return _node->data;
  68. }
  69. Ptr operator->() {
  70. return &_node->data;
  71. }
  72. self &operator++() {
  73. _node = _node->next;
  74. return *this;
  75. }
  76. self operator++(int) {
  77. self tmp(*this);
  78. _node = _node->next;
  79. return tmp;
  80. }
  81. bool operator!=(const self& it) const
  82. {
  83. return _node != it._node;
  84. }
  85. };
  86. // template <typename T>
  87. // class SimpleQueueModel : public SingleQueueModel<T> {
  88. // class Node; // 这一行必须得加上,表示子类需要重写父类的成员,否则在外部定义时会报错invalid class name in declaration of 'class SimpleQueueModel<T>::Node'
  89. // };
  90. template <typename T>
  91. class SingleQueueModel<T>::Node {
  92. public:
  93. T data;
  94. Node *next = NULL;
  95. Node() {}
  96. // Node(Node *) {}
  97. Node(const T &data) {
  98. this->data = data;
  99. }
  100. #if _DEBUG
  101. ~Node() {
  102. TRACE_CMH_1;
  103. std::cout << "Node " << data << " at " << this << " destructed" << std::endl;
  104. }
  105. #endif
  106. };
  107. template <typename T>
  108. class ComplexSingleQueueModel : SingleQueueModel<T> {
  109. public:
  110. Status push(T) = 0;
  111. Status pop(T &) = 0;
  112. Status shift(T &) = 0;
  113. };
  114. template <typename SingleQueueModel>
  115. class MultiQueueModel : Model {
  116. public:
  117. Status init(int_ queue_num) = 0;
  118. Status get(int_ index, SingleQueueModel &) = 0;
  119. Status move(int_ start, int_ destination) = 0;
  120. };
  121. struct Customer {
  122. int_ number;
  123. long cash;
  124. };
  125. std::ostream &operator<<(std::ostream &out, const Customer &customer) {
  126. out << customer.number << " " << customer.cash;
  127. return out;
  128. };
  129. #endif