1: // Created by Frank M. Carrano and Timothy M. Henry.
2: // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
4: template<class ItemType>
5: bool LinkedList<ItemType>::remove(int position)
6: {
7: bool ableToRemove = (position >= 1) && (position <= itemCount);
8: if (ableToRemove)
9: {
10: Node<ItemType>* curPtr = nullptr;
11: if (position == 1)
12: {
13: // Remove the first node in the chain
14: curPtr = headPtr; // Save pointer to node
15: headPtr = headPtr –>getNext();
16: }
17: else
18: {
19: // Find node that is before the one to remove
20: Node<ItemType>* prevPtr = getNodeAt(position − 1);
21:
22: // Point to node to remove
23: curPtr = prevPtr –>getNext();
24:
25: // Disconnect indicated node from chain by connecting the
26: // prior node with the one after
27: prevPtr –>setNext(curPtr –>getNext());
28: } // end if
29:
30: // Return node to system
31: curPtr –>setNext(nullptr);
32: delete curPtr;
33: curPtr = nullptr;
34: itemCount––; // Decrease count of entries
35: } // end if
36:
37: return ableToRemove;
38: } // end remove