#include <iostream>
using namespace std;
// 12. Complete the definition of list from §20.4.1–2 and get the high() example to run.
// Allocate a Link to represent one past the end.
template<typename Elem>
struct Link
{
Link* prev; // previous link
Link* succ; // successor (next) link
Elem val; // the value
};
template<typename Elem>
class list {
// representation and implementation details
Link<Elem>* first;
Link<Elem>* last; // one beyond the last link
public:
list() { Link<Elem>* node = new Link<Elem>; first = node; last = node; }
class iterator; // member type: iterator
iterator begin(); // iterator to first element
iterator end(); // iterator to one beyond last element
iterator insert(iterator p, const Elem& v); // insert v into list after p
iterator erase(iterator p); // remove p from the list
void push_back(const Elem& v); // insert v at end
void push_front(const Elem& v); // insert v at front
void pop_front(); // remove the first element
void pop_back(); // remove the last element
Elem& front(); // the first element
Elem& back(); // the last element
// . . .
};
//--------------------------------------------------------------------------------------------
// this push_front() function isn't working as it is supposed to; there's no link from first and last to it
template <typename Elem>
void list<Elem>::push_front(const Elem& v)
{
Link<Elem>* newNode = new Link<Elem>;
newNode->val = v;
newNode->prev = nullptr;
newNode->succ = first;
first = newNode;
}
template <typename Elem>
typename list<Elem>::iterator list<Elem>::begin()
{
return first;
}
template <typename Elem>
typename list<Elem>::iterator list<Elem>::end()
{
return last;
}
//---------------------------------Exercise 19--------------------------------------------
template<typename Elem> // requires Element<Elem>() (§19.3.3)
class list<Elem>::iterator {
Link<Elem>* curr; // current link
public:
iterator(Link<Elem>* p) :curr{p} { }
iterator& operator++()
{
if (curr->succ == nullptr)
{
// error("Can't increment; pointer out of scope.");
cout << "Can't increment; pointer out of scope." << endl;
}
curr = curr->succ;
return *this;
} // forward
iterator& operator--()
{
if (curr->prev == nullptr)
{
// error("Can't decrement; pointer out of scope.");
cout << "Can't increment; pointer out of scope." << endl;
}
curr = curr->prev;
return *this;
} // backward
Elem& operator*() { return curr->val; } // get value (dereference)
bool operator==(const iterator& b) const { return curr==b.curr; }
bool operator!= (const iterator& b) const { return curr!=b.curr; }
};
//---------------------------------Exercise 19--------------------------------------------
int main()
{
list<int> l;
l.push_front(2);
l.push_front(3);
l.push_front(4);
l.push_front(5);
list<int>::iterator it = l.begin();
for (; it != l.end(); ++it)
{
cout << *it << endl;
}
cout << "-------------------------------------------" << endl;
++it;
cout << *it << endl;
++it;
cout << *it << endl;
++it;
cout << *it << endl;
++it;
cout << *it << endl;
++it;
cout << *it << endl;
return 0;
}