// File: LinkList.h // Definitions for Linked MyVectorList 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 MyVectorList { public: // Constructor...Initialize MyVectorList to NULL // Pre: MyVectorList(); // No destructor or copy constructor need be explicitly implemented // There are no pointers in this implementation, so defaults are fine // Pre: None // Post: MyVectorList is empty void init(); // Pre: Argument contains element of the proper type // Post: MyVectorList contains at least one element (not necessarily ordered) void insertAtHead(const listEltType &); // Pre: MyVectorList is ordered // Post: MyVectorList is ordered, containing one additional element void insertInOrder(const listEltType &); // Pre: MyVectorList contains at least one element // Post: MyVectorList contains one less element void removeHead(); // Pre: MyVectorList is ordered, item is found in list // Post: MyVectorList 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(); vector theList; // the data }; // Implementation of vector-implemented Linked List Class template MyVectorList::MyVectorList() {} // No constructor necessary. Vector starts empty // Start from scratch template void MyVectorList::init() {theList.clear();} template void MyVectorList::insertAtHead(const listEltType &elt) {typename vector::iterator it=theList.begin(); theList.insert(it,elt); // easier: theList.insert(theList.begin(),elt); } template void MyVectorList::insertInOrder(const listEltType &elt) {typename vector::iterator it; for (it=theList.begin();it!=theList.end() && *it void MyVectorList::removeHead() {theList.erase(theList.begin()); } template void MyVectorList::deleteInOrder(const listEltType &elt) {typename vector::iterator it; for (it=theList.begin();*it!=elt;it++);//assumes it will be found..crash if not theList.erase(it); } template bool MyVectorList::search(const listEltType &elt) {typename vector::iterator it; for (it=theList.begin();it!=theList.end() && *it!=elt;it++); return(it!=theList.end()); } template listEltType &MyVectorList::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 MyVectorList::countNodes() {return(theList.size());} /* This has been commented out because it does not pass the vector as a const reference template ostream &operator<<(ostream &out,MyVectorList aList) {for (typename vector::iterator it=aList.theList.begin(); it!=aList.theList.end();it++) out << *it << " "; return(out); }*/ template ostream &operator<<(ostream &out,const MyVectorList &aList) {for (typename vector::const_iterator it=aList.theList.begin(); it!=aList.theList.end();it++) out << *it << " "; return(out); } #endif