fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // 12. Complete the definition of list from §20.4.1–2 and get the high() example to run.
  6. // Allocate a Link to represent one past the end.
  7.  
  8. template<typename Elem>
  9. struct Link
  10. {
  11. Link* prev; // previous link
  12. Link* succ; // successor (next) link
  13. Elem val; // the value
  14. };
  15.  
  16. template<typename Elem>
  17. class list {
  18. // representation and implementation details
  19. Link<Elem>* first;
  20. Link<Elem>* last; // one beyond the last link
  21. public:
  22. list() { Link<Elem>* node = new Link<Elem>; first = node; last = node; }
  23.  
  24. class iterator; // member type: iterator
  25.  
  26. iterator begin(); // iterator to first element
  27. iterator end(); // iterator to one beyond last element
  28.  
  29. iterator insert(iterator p, const Elem& v); // insert v into list after p
  30. iterator erase(iterator p); // remove p from the list
  31.  
  32. void push_back(const Elem& v); // insert v at end
  33. void push_front(const Elem& v); // insert v at front
  34. void pop_front(); // remove the first element
  35. void pop_back(); // remove the last element
  36.  
  37. Elem& front(); // the first element
  38. Elem& back(); // the last element
  39.  
  40. // . . .
  41. };
  42.  
  43. //--------------------------------------------------------------------------------------------
  44.  
  45. // this push_front() function isn't working as it is supposed to; there's no link from first and last to it
  46. template <typename Elem>
  47. void list<Elem>::push_front(const Elem& v)
  48. {
  49. Link<Elem>* newNode = new Link<Elem>;
  50. newNode->val = v;
  51. newNode->prev = nullptr;
  52. newNode->succ = first;
  53. first = newNode;
  54. }
  55.  
  56. template <typename Elem>
  57. typename list<Elem>::iterator list<Elem>::begin()
  58. {
  59. return first;
  60. }
  61.  
  62. template <typename Elem>
  63. typename list<Elem>::iterator list<Elem>::end()
  64. {
  65. return last;
  66. }
  67.  
  68.  
  69. //---------------------------------Exercise 19--------------------------------------------
  70.  
  71. template<typename Elem> // requires Element<Elem>() (§19.3.3)
  72. class list<Elem>::iterator {
  73. Link<Elem>* curr; // current link
  74. public:
  75. iterator(Link<Elem>* p) :curr{p} { }
  76.  
  77. iterator& operator++()
  78. {
  79. if (curr->succ == nullptr)
  80. {
  81. // error("Can't increment; pointer out of scope.");
  82. cout << "Can't increment; pointer out of scope." << endl;
  83. }
  84. curr = curr->succ;
  85. return *this;
  86.  
  87. } // forward
  88. iterator& operator--()
  89. {
  90. if (curr->prev == nullptr)
  91. {
  92. // error("Can't decrement; pointer out of scope.");
  93. cout << "Can't increment; pointer out of scope." << endl;
  94. }
  95. curr = curr->prev;
  96. return *this;
  97. } // backward
  98. Elem& operator*() { return curr->val; } // get value (dereference)
  99.  
  100. bool operator==(const iterator& b) const { return curr==b.curr; }
  101. bool operator!= (const iterator& b) const { return curr!=b.curr; }
  102. };
  103.  
  104. //---------------------------------Exercise 19--------------------------------------------
  105.  
  106. int main()
  107. {
  108. list<int> l;
  109. l.push_front(2);
  110. l.push_front(3);
  111. l.push_front(4);
  112. l.push_front(5);
  113. list<int>::iterator it = l.begin();
  114. for (; it != l.end(); ++it)
  115. {
  116. cout << *it << endl;
  117. }
  118. cout << "-------------------------------------------" << endl;
  119. ++it;
  120. cout << *it << endl;
  121. ++it;
  122. cout << *it << endl;
  123. ++it;
  124. cout << *it << endl;
  125. ++it;
  126. cout << *it << endl;
  127. ++it;
  128. cout << *it << endl;
  129. return 0;
  130. }
  131.  
Runtime error #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
5
4
3
2
-------------------------------------------
Can't increment; pointer out of scope.