Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include "List.h"
  2. #include <iterator>
  3. #include <utility>
  4.  
  5. #include <stdexcept>
  6.  
  7. struct List::Node
  8. {
  9. Node() = default;
  10. Node(int v, Node* p, Node* n)
  11. : value{v}, prev{p}, next{n} {}
  12. int value {};
  13. Node * prev {};
  14. std::unique_ptr<Node> next {};
  15. };
  16.  
  17. List::List()
  18. : head{new Node{}}, tail{}, sz{}
  19. {
  20. head->next= std::make_unique<Node>(0,head,nullptr);
  21. tail=head->next.get();
  22.  
  23. }
  24.  
  25. // List::List(List const & other)//kopieringskonstruktor
  26. // : List{}
  27. // {
  28. // for (Node * tmp {other.head->next}; tmp != other.tail ; )
  29. // {
  30. // push_back(tmp->value);
  31. // tmp = tmp->next;
  32. // }
  33. // }
  34. // List::List(List && tmp) noexcept//flyttkonstruktor
  35. // :List{}
  36. // {
  37. // swap(tmp);
  38. // }
  39. List::List(std::initializer_list<int> lst)
  40. : List{}
  41. {
  42. for ( auto val : lst )
  43. {
  44. push_back(val);
  45. }
  46. }
  47.  
  48. // void List::push_front(int value)
  49. // {
  50. // Node * old_first { head->next };
  51. // head->next = new Node{value, head, head->next};
  52. // old_first->prev = head->next;
  53. // ++sz;
  54. // }
  55. void List::push_back(int value)
  56. {
  57. Node * old_last { tail->prev };
  58. old_last->next = std::make_unique<Node>(value, old_last, tail);
  59. tail->prev = old_last->next.get();
  60. ++sz;
  61. }
  62.  
  63. // bool List::empty() const noexcept
  64. // {
  65. // return head->next == tail;
  66. // }
  67.  
  68. int List::back() const noexcept
  69. {
  70. return tail->prev->value;
  71. }
  72. int & List::back() noexcept
  73. {
  74. return tail->prev->value;
  75. }
  76.  
  77. int List::front() const noexcept
  78. {
  79. return head->next->value;
  80. }
  81. int & List::front() noexcept
  82. {
  83. return head->next->value;
  84. }
  85.  
  86. int & List::at(int idx)
  87. {
  88. return const_cast<int &>(static_cast<List const *>(this)->at(idx));
  89. }
  90. int const & List::at(int idx) const
  91. {
  92. if (idx >= sz)
  93. throw std::out_of_range{"Index not found"};
  94. Node * tmp {head->next.get()};
  95. while ( idx > 0 )
  96. {
  97. tmp = tmp->next.get();
  98. --idx;
  99. }
  100. return tmp->value;
  101. }
  102.  
  103. int List::size() const noexcept
  104. {
  105. return sz;
  106. }
  107.  
  108. // void List::swap(List & other) noexcept
  109. // {
  110. // using std::swap;
  111. // swap(head, other.head);
  112. // swap(tail, other.tail);
  113. // swap(sz, other.sz);
  114. // }
  115.  
  116. // List & List::operator=(List const & rhs) &
  117. // {
  118. // List{rhs}.swap(*this);
  119. // return *this;
  120. // }
  121.  
  122. // List & List::operator=(List && rhs)& noexcept
  123. // {
  124. // swap(rhs);
  125. // return *this;
  126. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement