// File: LinkedList.cpp // Ordered Template Linked List Implementations #include #include #include "LinkedList.h" // Construct empty LinkedList template LinkedList::LinkedList() : head(NULL) {} // Copy constructor. copy() does the deep copy template LinkedList::LinkedList(LinkedList &cl) {head = copy( cl.head );} // Free all nodes template LinkedList::~LinkedList() {destroy(head);} // Assignment operator: copy() does the deep copy template LinkedList &LinkedList::operator =(const LinkedList& cl) { if (this != &cl) { destroy(head); head = copy(cl.head); } return *this; } // Place x into the list in order template void LinkedList::orderedInsert(eltType x) { if (empty() || x < head->data) assert(head=new node(x,head)); else { node* p = head; node* trailp = NULL; while (p != NULL && x > p->data) { trailp = p; p = p->next; } assert((trailp->next = new node(x,p)) != NULL); } } // Is this element in the linked list? template bool LinkedList::find(eltType x) { node *p = head; while (p != NULL && p->data != x) p = p->next; return (p != NULL && p->data == x); } // Inline: Look into this. template inline bool LinkedList::empty() {return (head == NULL);} // Remove a node in an ordered list // Pre: Node will be found template void LinkedList::remove(eltType x) { assert( !empty() ); node* p = head; node* trailp = NULL; while ( p != NULL && p->data != x ) { trailp = p; p = p->next; } assert(p != NULL); // x is not in the LinkedList if (p == head) head = head->next; // x is first in the LinkedList else trailp->next = p->next; // x is farther down in the LinkedList delete p; } // Remove all nodes in the linked list, starting at l template void LinkedList::destroy(node *l) { while (l != NULL) { node *doomed = l; l = l->next; delete doomed; } } // The deep copy. Copy the source list l, one node at a time template node* LinkedList::copy(node *l) { node* first = NULL; // ptr to beginning of copied LinkedList node* last = NULL; // ptr to last item insert in the copy if (l != NULL) { assert((first=last=new node(l->data,NULL)) != NULL); for (node* source=l->next;source!=NULL; source=source->next,last=last->next) { last->next = new node(source->data,NULL); assert(last->next); } } return first; } // Output a linked list, using a list iterator template // ostream& operator<<(ostream &os, const LinkedList &l) ostream& operator<<(ostream &os, LinkedList l) { listItr lt(l); for (lt.start(); lt.more(); lt.next()) os << lt.value() << " "; return os; } // One for you to do template LinkedList LinkedList::operator +(const LinkedList &l) { LinkedList concatList; // insert your solution return concatList; } // Return a pointer *directly* into a node's data in the linked list template eltType* LinkedList::operator[](int idx) { node *p=head; for (int i=0;p && inext,i++); return((p ? &(p->data) : NULL)); } // Count nodes in a linked list, starting at l template int LinkedList::countNodes(node *p) {return ((p) ? 1+countNodes(p->next) : 0);} // Return number of nodes in *this' list template int LinkedList::countNodesInList() {return(countNodes(head));} // What does it do? Replace this with an explanation template void LinkedList::mystery() { if (!head) // LinkedList empty return; node *p,*trailp; for (p=head,trailp=NULL;p->next;trailp=p,p=p->next); if (p==head) head=NULL; else trailp->next=NULL; delete p; } // Construct a list iterator. It consists of: // a reference to a linked list object // a pointer to the actual list, initially pointing to its head template listItr::listItr(const LinkedList &l): itr(l),curr(l.head) {} // Set curr to point at itr's head template void listItr::start(void) {curr = itr.head;} // Is curr at the end of the list? template bool listItr::more(void) {return curr != NULL;} // Move curr to next node template void listItr::next(void) {assert( curr != NULL ); curr = curr->next; } // Return data in curr's node. Regardless of assert(), this // function shouldn't be called until making sure more() returns true template eltType listItr::value(void) {assert( curr != NULL ); return curr->data; } // Initializers. Necessary to be able to be able to place template class // function implementations in a .cpp file template class node; template class LinkedList; template class listItr; template class ostream& operator<<(ostream &os, const LinkedList &l);