// // // dlist.cc // Routines to implement a doubly linked list of integer items. // // The list is dynamic and can have any number of integer items // The list can be maintained sorted, or sorted later. #include #include "dlist.h" // The following class defines a "dlink" -- an item in a doubly // linked list of elements with type integer // class Dlink { public: Dlink(int newValue); // Construct a new link containing newValue ~Dlink(); // Destroy the link. int getValue (); // Returns the value in this. Dlink *Next() const; // Return the successor to this. Returns null if there is no successor. Dlink *Prev() const; // Return the predecessor to this. Returns null if there is no // predecessor. void Prepend(Dlink *p); // Add p before this. Does nothing if p is null. void Append(Dlink *p); // Add p after this. Does nothing if p is null. void Remove(); // Remove this from list. // ADD: Declare any extra member functions you need here. private: int value; Dlink *next; Dlink *prev; }; // // Dlink IMPLEMENTATION // //---------------------------------------------------------------------- // Dlink ::Dlink(int value) // Construct a new link containing newValue // //---------------------------------------------------------------------- Dlink ::Dlink(int newValue) { value = newValue; // set value to the newValue next = 0; // the link is not yet in a list, prev = 0; // so set its next and prev to 0. } //---------------------------------------------------------------------- // Dlink::~Dlink(int value) // Destroy the link. // //---------------------------------------------------------------------- Dlink::~Dlink(int newValue) { // nothing to do here } //---------------------------------------------------------------------- // int Dlink::getValue() // Returns the value in this. // //---------------------------------------------------------------------- int Dlink::getValue () { return value; } //---------------------------------------------------------------------- // Dlink *Dlink::Next() // Return the successor to this. Returns null if there is no successor. //---------------------------------------------------------------------- Dlink *Dlink::Next() const { return next; } //---------------------------------------------------------------------- // Dlink *Dlink::Prev() // Return the predecessor to this. Returns null if there is no // predecessor. //---------------------------------------------------------------------- Dlink *Dlink::Prev() const { return prev; } //---------------------------------------------------------------------- // Dlink::Prepend(Dlink *p) // Add p before this. Does nothing if p is null. //---------------------------------------------------------------------- void Dlink::Prepend(Dlink *p) { if (p == 0) { // first item in list return; } p->next = this; p->prev = this->prev; if ( p->prev != 0 ) { p->prev->next = p; } this->prev = p; } // // ADD: other member functions for Dlink // // Dlist IMPLEMENTATION // // // ADD: constructors, deconstructors, and other declared member // functions for Dlist // //---------------------------------------------------------------------- // int Dlist::First () // Returns the first value in the list. //---------------------------------------------------------------------- int Dlist::First() const { return first->getValue(); } //---------------------------------------------------------------------- // Dlist::Prepend(int value) // Adds value to beginning of list. //---------------------------------------------------------------------- void Dlist::Prepend(int value) { Dlink *item = new Dlink(value); if ( first != 0 ) { first->Prepend(item); } first = item; // update first and last pointers if (last == 0) { last = first; // first item to go on list } } //---------------------------------------------------------------------- // Dlist::Print() // Prints the contents of the list to standard output. //---------------------------------------------------------------------- void Dlist::Print() const { Dlink *tmp; int i = 1; cout << " value: "; for (tmp = first; tmp != 0; tmp = tmp->Next()) { cout << tmp->getValue() << " "; i++; } cout << "\n"; } //---------------------------------------------------------------------- // Dlist::SelfTest() // Tests... //---------------------------------------------------------------------- void Dlist::SelfTest(int start) { int count = start; int end = start + 4; for ( ; count <= end; count = count + 2) { Prepend(count); Append(count+1); } InsertAfter(count++,Last()); InsertAfter(count,First()); Print(); cout << "Should be 10 13 8 6 7 9 11 12\n\n"; cout << "Testing Sort \n"; Sort(); Print(); cout << "Should be 6 7 8 9 10 11 12 13\n\n"; cout << "Remove first item \n"; Remove(First()); Print(); cout << "Should be 7 8 9 10 11 12 13\n\n"; cout << "Remove last item \n"; Remove(Last()); Print(); cout << "Should be 7 8 9 10 11 12\n\n"; cout << "Remove second item \n"; Remove(first->Next()->getValue()); Print(); cout << "Should be 7 9 10 11 12\n\n"; Clear(); cout << "Removed all items \n"; Print(); cout << "Should be empty\n\n"; }