#include #include "lab3llist.h" /***************************************************************** * LinkedList *****************************************************************/ // Default constructor LinkedList::LinkedList() { head = NULL; numNodes = 0; } // Destructor LinkedList::~LinkedList() { Node* cur = head; Node* prev = NULL; while (cur != NULL) { prev = cur; cur = cur->next; delete prev; } } // Insert value "val" into position "pos" void LinkedList::insert(val_t val, unsigned int pos) { Node* newNode = new Node(); newNode->val = val; numNodes++; if (pos == 0) { newNode->next = head; head = newNode; return; } Node* prev = head; Node* cur = head->next; while (pos != 1) { prev = cur; cur = cur->next; pos--; } newNode->next = cur; prev->next = newNode; } // Remove a value at position "pos" void LinkedList::remove(unsigned int pos) { if (pos == 0) { if (head) { Node* temp = head->next; delete head; head = temp; numNodes--; return; } else return; } Node* prev = head; Node* cur = head->next; while (pos != 1) { prev = cur; cur = cur->next; pos--; } numNodes--; prev->next = cur->next; delete cur; } // Get a value from the list val_t LinkedList::retrieve(unsigned int pos) { Node* cur = head; while (pos != 0) { cur = cur->next; pos--; } return cur->val; } // Quickly check if there is data bool LinkedList::isEmpty() { return head == NULL; } // Check how much data we have unsigned int LinkedList::size() { return numNodes; } // Return an iterator to the beginning of our list LinkedListIterator LinkedList::begin() { LinkedListIterator ret; ret.n = head; ret.llist = this; return ret; } // Return an iterator to the end of our list LinkedListIterator LinkedList::end() { LinkedListIterator ret; ret.n = NULL; ret.llist = this; return ret; } // If you implement your iterator and LinkedList correctly, // the LinkedList version of this function should be // identical except for the type names. ostream& operator << (ostream& outStream, LinkedList& toPrint) { // Do the special case of an empty list. if (toPrint.isEmpty()) { outStream << "[]"; return outStream; } // Get an iterator LinkedListIterator i = toPrint.begin(); // Print out [ and the first element outStream << "[" << *i; // Update the iterator ++i; // Loop through the rest of the data for (; i != toPrint.end(); ++i) { outStream << ", " << *i; } // Print out the closing ] outStream << "]"; // Return return outStream; } /***************************************************************** * LinkedListIterator *****************************************************************/ // Default constructor LinkedListIterator::LinkedListIterator() { llist = NULL; n = NULL; } // Copy constructor LinkedListIterator::LinkedListIterator(const LinkedListIterator& other) { llist = other.llist; n = other.n; } // Equality operator bool const LinkedListIterator::operator ==(LinkedListIterator& other) { return ((llist == other.llist) && (n == other.n)); } // Inequality operator bool LinkedListIterator::operator !=(const LinkedListIterator& other) { return !((llist == other.llist) && (n == other.n)); } // Dereference operator, returns the value in the list currently // "pointed at" by our iterator. val_t LinkedListIterator::operator*() { return n->val; } // Advance to the next value LinkedListIterator& LinkedListIterator::operator++() { n = n->next; return *this; } // Go back to the previous value LinkedListIterator& LinkedListIterator::operator--() { LinkedList::Node* old = n; for (n = llist->head; n->next != old; n = n->next) { } return *this; }