#include "List.H" List::List() : _head(NULL), _current(NULL) { } List::~List() { _destroyList(_head); delete _head; _current = _head = NULL; } void List::_destroyList(ListNode * l) { if (l == NULL) return; _destroyList(l->_next); delete l; return; } List::List(const List & rhs) { } List & List::operator =(const List &rhs) { return *this; } bool List::insert(int x) { if (isEmpty()) { _head = new ListNode(x, NULL); _current = _head; return true; } if (isPastEnd()) return false; // If we're inserting at the beginning of the list, // there's no previous node. if (_current == _head) { _head = _current = new ListNode(x, _head); return true; } else { ListNode * previous = _findPrevious(_current); ListNode * tmp = new ListNode(x, _current); previous->_next = tmp; _current = tmp; } return true; } ListNode * List::_findPrevious(ListNode * l) { ListNode * previous; if (l == NULL) return NULL; for (previous = _head; previous != NULL; previous = previous->_next) { if (previous->_next == l) return previous; } return NULL; } void List::remove() { if (isEmpty() || isPastEnd()) return; ListNode * previous = _findPrevious(_current); previous->_next = _current->_next; delete _current; _current = previous->_next; } void List::next() { if (!isPastEnd()) _current = _current->_next; else _current = NULL; } void List::previous() { if (!isPastEnd()) _current = _findPrevious(_current); } int List::current() { if (isPastEnd()) { cerr << "Uh oh" << endl; return -1; } else return _current->_data; } void List::reset() { _current = _head; } bool List::isEmpty() { return _head == NULL; } bool List::isPastEnd() { return _current == NULL; } ostream & operator <<(ostream & os, List & l) { ListNode * old_current = l._current; if (l.isEmpty()) return os << "()"; os << "("; bool first_pass=true; for (l.reset() ; !l.isPastEnd(); l.next()) { if (first_pass) { os << l.current(); first_pass = false; } else os << "," << l.current(); } l._current = old_current; os << ")"; return os; }