Advertisement
kachamaka

Untitled

Aug 13th, 2022
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using std::list;
  5. using std::cout;
  6.  
  7. template <typename T>
  8. class linked_list {
  9. struct Node {
  10. T data;
  11. Node* next;
  12. Node* prev;
  13.  
  14. Node(T val) : data(val), next(nullptr), prev(nullptr) { }
  15. };
  16.  
  17. Node* head;
  18. Node* tail;
  19.  
  20. public:
  21. linked_list() : head(nullptr), tail(nullptr) { }
  22. ~linked_list() { clear(); }
  23.  
  24. Node* front() const {
  25. return head;
  26. }
  27.  
  28. Node* back() const {
  29. return tail;
  30. }
  31.  
  32. bool empty() const {
  33. return head == nullptr;
  34. }
  35.  
  36. void push_back(const T& val) {
  37. if (empty()) {
  38. head = new Node(val);
  39. tail = head;
  40. }
  41. else if (head == tail) {
  42. tail = new Node(val);
  43. head->next = tail;
  44. tail->prev = head;
  45. }
  46. else {
  47. Node* newNode = new Node(val);
  48. tail->next = newNode;
  49. newNode->prev = tail;
  50. tail = newNode;
  51. }
  52. }
  53.  
  54. void push_front(const T& val) {
  55. if (empty()) {
  56. head = new Node(val);
  57. tail = head;
  58. }
  59. else if (head == tail) {
  60. head = new Node(val);
  61. head->next = tail;
  62. tail->prev = head;
  63. }
  64. else {
  65. Node* newNode = new Node(val);
  66. newNode->next = head;
  67. head->prev = newNode;
  68. head = newNode;
  69. }
  70. }
  71.  
  72. void pop_back() {
  73. if (empty()) return;
  74. if (head == tail) {
  75. delete head;
  76. head = tail = nullptr;
  77. }
  78. else {
  79. Node* last = tail->prev;
  80. last->next = nullptr;
  81. delete tail;
  82. tail = last;
  83. }
  84. }
  85.  
  86. void pop_front() {
  87. if (empty()) return;
  88. if (head == tail) {
  89. delete head;
  90. head = tail = nullptr;
  91. }
  92. else {
  93. Node* first = head->next;
  94. first->prev = nullptr;
  95. delete head;
  96. head = first;
  97. }
  98. }
  99.  
  100. void clear() {
  101. while (head) {
  102. Node* toDelete = head;
  103. head = head->next;
  104. delete toDelete;
  105. }
  106. }
  107.  
  108.  
  109.  
  110. };
  111.  
  112. template <typename T>
  113. class queue {
  114. T* data;
  115. unsigned begin, end;
  116. const unsigned size;
  117. unsigned capacity;
  118.  
  119. void resize() {
  120. T* newData = new T[capacity * 2];
  121. for (unsigned i = begin; i < last; ++i) newData[i] = data[i];
  122. //std::swap(newData, m_arr);
  123. delete[] data;
  124. data = newData;
  125. capacity *= 2;
  126. }
  127. public:
  128. queue(unsigned sz = 16) : data(new T[sz + 1]), begin(0), end(0), size(sz + 1) { }
  129. };
  130.  
  131. int main() {
  132. linked_list<int> l;
  133. cout << l.empty() << "\n";
  134. l.push_back(1);
  135. l.push_back(2);
  136. l.push_back(3);
  137. l.push_back(4);
  138. l.push_back(5);
  139. l.push_back(6);
  140. l.push_back(7);
  141. l.push_front(0);
  142. l.push_front(5);
  143. cout << l.front()->data << "\n";
  144. //cout << l.back()->data << "\n";
  145. l.pop_front();
  146. l.pop_front();
  147. l.pop_front();
  148. l.pop_front();
  149. l.pop_front();
  150. l.clear();
  151. cout << l.empty() << "\n";
  152. //l.pop_back();
  153. //l.pop_back();
  154. //l.pop_back();
  155. //l.pop_back();
  156. //l.pop_back();
  157. //cout << l.front()->data << "\n";
  158.  
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement