// Implementation of the Dynamic List Class // Filename: list.cxx // Darin Gillis 349-70-0453 // Last Updated: 2-5-99 // INVARIANT FOR LIST ADT: // 1. The number of items in the list is held in the variable used // 2. The items in the list are held in an Item array ( which is // partially filled ). The array is dynamic pointed to by *data // 3. The size of the dynamic array is held in variable capacity #include #include "list2.h" #define SENTINEL -1 //--------------------------------------- // CONSTRUCTORS and DESTRUCTOR List::List(long initial_capacity) { data = new Item[initial_capacity]; capacity = initial_capacity; used = 0; curpos = SENTINEL; } //---------------------------------------- // The Copy Constructor List::List(const List& source) { long ctr; data = new Item[source.capacity]; capacity = source.capacity; used = source.used; curpos = source.curpos; for (ctr=0;ctr 0) curpos = 0; else curpos = SENTINEL; } //---------------------------------------- void List::advance() { // if no current position // if (curpos==SENTINEL) // return; assert(is_item()); if ((curpos+1)== used) { curpos=SENTINEL; return; } curpos++; } //---------------------------------------- void List::insert(const Item& entry) { int ctr; // if the list is empty or curpos is not set, then // insert the entry at the start of the list if (curpos==SENTINEL) curpos=0; // if list is full, resize by 10% + 1 if (used == capacity) resize( capacity*(1.1) + 1 ); // shift all data right one spot for(ctr=used;ctr>=curpos;ctr--) data[ctr]=data[ctr-1]; data[curpos]=entry; used++; } //---------------------------------------- void List::attach(const Item& entry) { int ctr; // if list is full, resize by 10% + 1 if (used == capacity) resize( capacity*(1.1) + 1 ); // if the list is empty or curpos is not set, then // insert the entry at the start of the list if (curpos==SENTINEL) { curpos=used; data[curpos]=entry; used++; return; } // shift all data right one spot for(ctr=used;ctr>curpos;ctr--) data[ctr]=data[ctr-1]; data[curpos+1]=entry; curpos++; used++; } //---------------------------------------- void List::remove_current() { int ctr; // if (curpos==SENTINEL) // return; assert(is_item()); // shift all data left one spot for(ctr=(curpos+1);ctr= 0) && (curpos < used)); } //---------------------------------------- List::Item List::current() const { assert(is_item()); return (data[curpos]); }