Download CS302 – Data Structures and more Exercises Logic in PDF only on Docsity!
CS302 - Data Structures
using C++
Kostas Alexis
Topic: List Implementations
Array-based Implementation of ADT List
+isEmpty(): boolean
+getLength(): integer
+insert(newPosition: integer, newEntry: ItemType): boolean
+remove(position: integer): boolean
+clear(): void
+getEntry(position: integer): ItemType
+replace(position: integer, newEntry: ItemType): ItemType
Array-based Implementation of ADT List
0 1 1 12 2 2 3 3 3 19 4 4 100 5 5 75 6 6 10 …?? … maxItems maxItems ? itemCount ? ADT list positions Array indices items
The Header File
/** ADT list: Array-based implementation @file ArrayList.h */ #ifndef ARRAY_LIST_ #define ARRAY_LIST_ #include “ListInterface.h” #include “PrecondViolatedExcept.h” template < class ItemType> class ArrayList : public ListInterface { private : static const int DEFAULT_CAPACITY = 100; // Default list capacity ItemType items[DEFAULT_CAPACITY + 1]; // Array of list items (ignore // items[0]) int itemCount; // Current count of list items int maxCount; // Maximum capacity of the list public : ArrayList(); // Copy constructor and destructor are supplied by compiler bool isEmpty() const ; int getLength() const ; bool remove( int position); void clear(); ItemType getEntry( int position) const throw (PrecondViolatedExcept); ItemType replace( int position, const ItemType& newEntry) throw (PrecondViolatedExcept); }; // end ArrayList #include “ArrayList.cpp” #endif
The Implementation File
template < class ItemType> ItemType ArrayList::getEntry( int position) const throw (PrecondViolatedExcept) { // enforce precondition bool ableToGet = (position >= 1) && (position <= itemCount); if (ableToGet) return items[position]; else { std::string message = “getEntry() called with an empty list or “; message = message + “invalid position.”; throw (PrecondViolatedExcept(message)); } // end if } // end getEntry Method getEntry
The Implementation File
template < class ItemType> bool ArrayList::insert( int newPosition, const ItemType& newEntry) { bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1) && (itemCount < maxItems); if (ableToInsert) { // Make room for new entry by shifting all entries // positions from itemCount down to newPosition // (no shift if newPosition == itemCount + 1) for ( int pos = itemCount; pos >= newPosition; pos--) item[pos + 1] = items[pos]; // Insert new Entry items[newPosition] = newEntry; itemCount++; // Increase count of entries } // end if return ableToInsert; } // end getEntry Method insert
The Implementation File
template < class ItemType> ItemType ArrayList::getEntry( int newPosition) const throw(PrecondViolatedExcept) { // Enforce precondition bool ableToGet = (position >= 1) && (position <= itemCount); if (ableToGet) { return items[position]; else { std::string message = “getEntry() called with an empty list or”; message = message + “invalid position.”; throw (PrecondViolatedExcept(message)); } // end if } // end getEntry Method getEntry
The Implementation File
template < class ItemType> ItemType ArrayList::replace( int position, const ItemType& newEntry) throw (PrecondViolatedExcept) { // Enforce precondition bool ableToSet = (position >= 1) && (position <= itemCount); if (ableToSet) { ItemType oldEntry = items[position]; items[position] = newEntry; return oldEntry; } else { std::string message = “replace() called with an empty list or”; message = message + “invalid position.”; throw (PrecondViolatedExcept(message)); } // end if } // end replace Method replace
The Implementation File
Shifting items to remove
The Implementation File
template < class ItemType> void ArrayList::clear() { itemCount = 0; } // end clear Method clear
Link-based Implementation of ADT List
- A link-based implementation of the ADT list
12 ptr
item next
3 ptr
item next
25 ptr
item next
item next
ptr
headPtr itemCount 4
The Header File
/** ADT list: Linked-based implementation @file LinkedList.h / #ifndef LINKED_LIST_ #define LINKED_LIST_ #include “ListInterface.h” #include “Node.h” #include “PrecondViolatedExcept.h” template < class ItemType> class LinkedList : public ListInterface { private : Node headPtr; // Pointer to first node in chain // (contains the first entry in the list) ItemType items[DEFAULT_CAPACITY + 1]; // Array of list items (ignore // items[0]) int itemCount; // Current count of list items // Locates a specified node in a linked list // @pre position is the number of the desired node; // position >= 1 and position <= itemCount // @post The node is found and a pointer to it is returned // @param position The number of the node to locate //@return A pointer to the node at the given position Node* getNodeAt( int position) const ; public : LinkedList(); LinkedList( const LinkedList& aList); virtual ~LinkedList(); bool isEmpty() const ; int getLength() const ; bool remove( int position); void clear(); ItemType getEntry( int position) const throw (PrecondViolatedExcept); ItemType replace( int position, const ItemType& newEntry) throw (PrecondViolatedExcept); }; // end LinkedList #include “LinkedList.cpp” #endif
The Implementation File
template < class ItemType> ItemType LinkedList::getEntry( int position) const throw (PrecondViolatedExcept) { // enforce precondition bool ableToGet = (position >= 1) && (position <= itemCount); if (ableToGet) Node* nodePtr = getNodeAt(position) return nodePtr->getItem(); else { std::string message = “getEntry() called with an empty list or “; message = message + “invalid position.”; throw (PrecondViolatedExcept(message)); } // end if } // end getEntry Method getEntry
The Implementation File
template < class ItemType> ItemType LinkedList::getNodeAt( int position) const { // debugging check of precondition assert( (position >= 1) && (position <= itemCount) ); // Count from the beginning of the chain Node* curPtr = headPtr; for (int skip = 1; skip < position; skip++) curPtr = curPtr->getNext(); return curPtr; } // end getNodeAt Method getEntry