Source of Listing9-2.h


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

  4: /** ADT list: Link-based implementation.
  5:     Listing 9-2.
  6:     @file LinkedList.h */

  8: #ifndef LINKED_LIST_
  9: #define LINKED_LIST_

 11: #include "ListInterface.h"
 12: #include "Node.h"
 13: #include "PrecondViolatedExcept.h"

 15: template<class ItemType>
 16: class LinkedList : public ListInterface<ItemType>
 17: {
 18: private:
 19:    Node<ItemType>* headPtr; // Pointer to first node in the chain;
 20:                             // (contains the first entry in the list)
 21:    int itemCount;           // Current count of list items 
 22:    
 23:    // Locates a specified node in this linked list.
 24:    // @pre  position is the number of the desired node;
 25:    //       position >= 1 and position <= itemCount.
 26:    // @post  The node is found and a pointer to it is returned.
 27:    // @param position  The number of the node to locate.
 28:    // @return  A pointer to the node at the given position.
 29:    Node<ItemType>* getNodeAt(int position) const;

 31: public:
 32:    LinkedList();
 33:    LinkedList(const LinkedList<ItemType>& aList);
 34:    virtual ~LinkedList();

 36:    bool isEmpty() const;
 37:    int getLength() const;
 38:    bool insert(int newPosition, const ItemType& newEntry);
 39:    bool remove(int position);
 40:    void clear();
 41:    
 42:    /** @throw PrecondViolatedExcept if position < 1 or
 43:                                        position > getLength(). */
 44:    ItemType getEntry(int position) const throw(PrecondViolatedExcept);

 46:    /** @throw PrecondViolatedExcept if position < 1 or
 47:                                        position > getLength(). */
 48:    void replace(int position, const ItemType& newEntry)
 49:                               throw(PrecondViolatedExcept);
 50: }; // end LinkedList

 52: #include "LinkedList.cpp"
 53: #endif