// slist.cpp: implementation of the slist class. // ////////////////////////////////////////////////////////////////////// #include "slist.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// slist::slist() : list() { // list() does everything. } slist::~slist() // nothing to do, ~list does everthing. { } void slist::insert(String name) // Insert name, but keep them all in alphabetical order. // // This function works in O(n) time. The actual time depends on how far // currentPos is from name. { String currentName = getCurrent(); if (currentName == emptyString) //list is empty so just insert it here list::insert(name); else if (currentName >= name) insertLeft(name); else insertRight(name); } void slist::insertLeft(String name) { while ((getCurrent() > name) && !(isAtHead())) (*this)--; if (isAtHead() && (getCurrent() > name)) {//this is a special case list::insertAtHead(name); } else list::insert(name); } void slist::insertRight(String name) { while (!(isAtTail())) if (getCurrent() > name){ (*this)--; break; } else (*this)++; list::insert(name); } void slist::insertAtHead(String name) { cerr << "Sorry, you cannot do that to a sorted list, use insert instead." << endl; return; }