Source of Listing12-1.h


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: // Listing 12-1

  6: /** Interface for the ADT sorted list
  7:  @file  SortedListInterface.h */

  9: #ifndef SORTED_LIST_INTERFACE_
 10: #define SORTED_LIST_INTERFACE_

 12: template<class ItemType>
 13: class SortedListInterface
 14: {
 15: public:
 16:    /** Inserts an entry into this sorted list in its proper order
 17:        so that the list remains sorted.
 18:     @pre  None.
 19:     @post  newEntry is in the list, and the list is sorted.
 20:     @param newEntry  The entry to insert into the sorted list.
 21:     @return  True if insertion is successful, or false if not. */
 22:    virtual bool insertSorted(const ItemType& newEntry) = 0;
 23:    
 24:    /** Removes the first or only occurrence of the given entry from this
 25:        sorted list.
 26:     @pre  None.
 27:     @post  If the removal is successful, the first occurrence of the
 28:        given entry is no longer in the sorted list, and the returned
 29:        value is true. Otherwise, the sorted list is unchanged and the
 30:        returned value is false.
 31:     @param anEntry  The entry to remove.
 32:     @return  True if removal is successful, or false if not. */
 33:    virtual bool removeSorted(const ItemType& anEntry) = 0;
 34:    
 35:    /** Gets the position of the first or only occurrence of the given
 36:        entry in this sorted list. In case the entry is not in the list,
 37:        determines where it should be if it were added to the list.
 38:     @pre  None.
 39:     @post  The position where the given entry is or belongs is returned.
 40:        The sorted list is unchanged.
 41:     @param anEntry  The entry to locate.
 42:     @return  Either the position of the given entry, if it occurs in the
 43:        sorted list, or the position where the entry would occur, but as a
 44:        negative integer. */
 45:    virtual int getPosition(const ItemType& anEntry) const = 0;
 46:    
 47:    // The following methods are the same as those given in ListInterface
 48:    // in Listing 8-1 of Chapter 8 and are completely specified there.
 49:    
 50:    /** Sees whether this list is empty. */
 51:    virtual bool isEmpty() const = 0;
 52:    
 53:    /** Gets the current number of entries in this list. */
 54:    virtual int getLength() const = 0;
 55:    
 56:    /** Removes the entry at a given position from this list. */
 57:    virtual bool remove(int position) = 0;
 58:    
 59:    /** Removes all entries from this list. */
 60:    virtual void clear() = 0;
 61:    
 62:    /** Gets the entry at the given position in this list. */
 63:    virtual ItemType getEntry(int position) const = 0;
 64:    
 65:    /** Destroys this sorted list and frees its assigned memory. */
 66:    virtual ~SortedListInterface() { }
 67: }; // end SortedListInterface
 68: #endif