class LinkedList
1: // Created by Frank M. Carrano and Tim Henry.
2: // Copyright (c) 2013 __Pearson Education__. All rights reserved.
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 "PrecondViolatedExcep.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: // Adds a given node at a given position within the subchain pointed to by subChainPtr.
32: // Returns a pointer to the augmented subchain.
33: Node<ItemType>* insertNode(int newPosition, Node<ItemType>* newNodePtr, Node<ItemType>* subChainPtr);
35: public:
36: LinkedList();
37: LinkedList(const LinkedList<ItemType>& aList);
38: virtual ~LinkedList();
40: bool isEmpty() const;
41: int getLength() const;
42: bool insert(int newPosition, const ItemType& newEntry);
43: bool remove(int position);
44: void clear();
45:
46: /** @throw PrecondViolatedExcep if position < 1 or
47: position > getLength(). */
48: ItemType getEntry(int position) const throw(PrecondViolatedExcep);
50: /** @throw PrecondViolatedExcep if position < 1 or
51: position > getLength(). */
52: void setEntry(int position, const ItemType& newEntry)
53: throw(PrecondViolatedExcep);
54: }; // end LinkedList
56: #include "LinkedList.cpp"
57: #endif