// File: LinkList.h // Definitions for Linked List ADT // The linked list contains a dummy header node that is initialized to // hold a value that will always come first #ifndef LINK_LIST_H #define LINK_LIST_H #include #include using namespace std; template class List { public: // Constructor...Initialize List to NULL // Pre: List(); // No destructor or copy constructor need be explicitly implemented // There are no pointers in this implementation, so defaults are fine // Pre: None // Post: List is empty void init(); // Pre: Argument contains element of the proper type // Post: List contains at least one element (not necessarily ordered) void insertAtHead(const listEltType &); // Pre: List is ordered // Post: List is ordered, containing one additional element void insertInOrder(const listEltType &); // Pre: List contains at least one element // Post: List contains one less element void removeHead(); // Pre: List is ordered, item is found in list // Post: List is ordered, containing one additional element void deleteInOrder(const listEltType &); // Pre: None // Post: Boolean value returned bool search(const listEltType&); // Retrieve a listEltType reference using an int key listEltType &retrieve(const int &); // Pre: None // Post: Value returned is number of elements in list int countNodes(); friend ostream &operator<<(ostream &,List); vector theList; }; // Implementation of vector-implemented List Class using namespace std; template List::List() {} // No constructor necessary. Vector starts empty // Start from scratch template void List::init() {theList.clear();} template void List::insertAtHead(const listEltType &elt) {vector::iterator it=theList.begin(); theList.insert(it,elt); // easier: theList.insert(theList.begin(),elt); } template void List::insertInOrder(const listEltType &elt) {vector::iterator it; for (it=theList.begin();it!=theList.end() && *it void List::removeHead() {theList.erase(theList.begin()); } template void List::deleteInOrder(const listEltType &elt) {vector::iterator it; for (it=theList.begin();*it!=elt;it++);//assumes it will be found...crash if not theList.erase(it); } template bool List::search(const listEltType &elt) {vector::iterator it; for (it=theList.begin();it!=theList.end() && *it!=elt;it++); return(it!=theList.end()); } template listEltType &List::retrieve(const int &key) {// This function can be easily adapted to search in objects or structs for // a member that is an int } template int List::countNodes() {return(theList.size());} template ostream &operator<<(ostream &out,List aList) {for (vector::iterator it=aList.theList.begin(); it!=aList.theList.end();it++) out << *it; return(out); } ostream &operator<<(ostream &out,List aList) {for (vector::iterator it=aList.theList.begin();it!=aList.theList.end();it++) out << *it; return(out); } #endif