Advertisement
Guest User

Untitled

a guest
May 27th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class List {
  6. struct Elem {
  7. T data_;
  8. Elem* next_;
  9. Elem* prev_;
  10.  
  11. Elem(T val)
  12. : data_(val),
  13. next_(0),
  14. prev_(0)
  15. {}
  16. };
  17.  
  18. Elem* head_;
  19. Elem* tail_;
  20. public:
  21. List()
  22. : head_(0),
  23. tail_(0)
  24. {}
  25.  
  26. List(const List& other) {
  27. Elem* node = other.head_;
  28. while(node != NULL) {
  29. push_back(node->data_);
  30. node = node->next_;
  31. }
  32. }
  33.  
  34. List& operator=(const List& other) {
  35. Elem* node = other.head_;
  36. while(node != NULL) {
  37. push_back(node->data_);
  38. node = node->next_;
  39. }
  40. }
  41.  
  42. ~List() {
  43. while(!empty()) {
  44. pop_front();
  45. }
  46.  
  47. delete head_;
  48. }
  49.  
  50. void push_back(const T& val) {
  51. Elem* node = new Elem(val);
  52. if(head_ == 0) {
  53. node->prev_ = NULL;
  54. head_ = node;
  55. tail_ = node;
  56. } else {
  57. node->prev_ = tail_;
  58. tail_->next_ = node;
  59. tail_ = node;
  60. }
  61.  
  62. tail_->next_ = NULL;
  63. }
  64.  
  65. void pop_back() {
  66. tail_ = tail_->prev_;
  67. tail_->next_ = NULL;
  68. }
  69.  
  70. void pop_front() {
  71. head_ = head_->next_;
  72. }
  73.  
  74. void push_front(const T& val) {
  75. Elem* node = new Elem(val);
  76. head_->next_->prev_ = node;
  77. node->next_ = head_;
  78. head_ = node;
  79. }
  80.  
  81. T& front() {
  82. return head_->data_;
  83. }
  84.  
  85. const T& front() const {
  86. return head_->data_;
  87. }
  88.  
  89. T& back() {
  90. return tail_->data_;
  91. }
  92.  
  93. const T& back() const {
  94. return tail_->data_;
  95. }
  96.  
  97. int size() const {
  98. Elem* node = head_;
  99. int size = 0;
  100.  
  101. while(node != NULL) {
  102. size++;
  103. node = node->next_;
  104. }
  105.  
  106. return size;
  107. }
  108.  
  109. bool empty() const {
  110. return size() == 0;
  111. }
  112.  
  113. class iterator {
  114. Elem* node_;
  115. iterator(Elem* ptr)
  116. : node_(ptr)
  117. {}
  118.  
  119. friend class List;
  120. public:
  121. iterator operator++() {
  122. node_ = node_->next_;
  123. return *this;
  124. }
  125.  
  126. iterator operator++(int) {
  127. iterator res = *this;
  128. operator++();
  129. return res;
  130. }
  131.  
  132. bool operator==(const iterator& other) const {
  133. return node_ == other.node_;
  134. }
  135.  
  136. bool operator!=(const iterator& other) const {
  137. return !operator==(other);
  138. }
  139.  
  140. T& operator*() {
  141. return node_->data_;
  142. }
  143.  
  144. T* operator->() {
  145. return node_->data_;
  146. }
  147. };
  148.  
  149. iterator begin() {
  150. return iterator(head_);
  151. }
  152.  
  153. iterator end() {
  154. return iterator(tail_);
  155. }
  156.  
  157. void pretty_print() {
  158. Elem* temp = head_;
  159.  
  160. while(temp != NULL) {
  161. cout << temp->data_ << " ";
  162. temp = temp->next_;
  163. }
  164.  
  165. cout << endl;
  166. }
  167. };
  168.  
  169. int main() {
  170. List<int> l;
  171. l.push_back(1);
  172. l.push_back(2);
  173. l.push_back(3);
  174. l.push_back(4);
  175. l.push_front(5);
  176. l.pretty_print();
  177.  
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement