#ifndef LINKED_LIST_ #define LINKED_LIST_ template class LinkedList { private: std::shared_ptr> headPtr; // Pointer to first node in the chain; // (contains the first entry in the list) int itemCount; // Current count of list items std::shared_ptr> getNodeAt(int position) const; public: LinkedList(); LinkedList(const LinkedList& aList); virtual ~LinkedList(); bool isEmpty() const; int getLength() const; bool insert(int newPosition, const ItemType& newEntry); bool remove(int position); void clear(); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ ItemType getEntry(int position) const throw(PrecondViolatedExcept); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ void replace(int position, const ItemType& newEntry) throw(PrecondViolatedExcept); // Operator Overloading void operator = (const LinkedList& arg); friend std::ostream& operator << (std::ostream& os, const LinkedList& arg) { os << \"There are \" << arg.getLength() << \" values in the list:\" << std::endl; for (int i{ 1 }; i <= arg.getLength(); i++) { os << arg.getEntry(i) << std::endl; } os << std::endl << std::endl << std::endl << std::endl; return os; } }; // Returns a pointer to the Node at the given position. template std::shared_ptr> LinkedList::getNodeAt (int position) const { std::shared_ptr> currPtr{ headPtr }; for (int i{ 1 }; i < position; i++) currPtr = currPtr->getNext(); return currPtr; } // Default constructor. template LinkedList::LinkedList() { headPtr = nullptr; itemCount = 0; } // Copy constructor. template LinkedList::LinkedList(const LinkedList& aList) { itemCount = aList.getLength(); auto currPtr{ aList.headPtr }; auto newNodePtr{ headPtr = std::make_shared>(currPtr->getItem()) }; auto prevNodePtr{ newNodePtr }; currPtr = currPtr->getNext(); for (int i{ 2 }; i <= itemCount; i++) { newNodePtr = std::make_shared>(currPtr->getItem()); prevNodePtr->setNext(newNodePtr); prevNodePtr = newNodePtr; currPtr = currPtr->getNext(); } } // Destructor. template LinkedList::~LinkedList() { headPtr = nullptr; itemCount = 0; } // Accessor/Info Functions. template bool LinkedList::isEmpty() const { return (itemCount > 0)?0:1; } template int LinkedList::getLength() const { return itemCount; } // Places a Node at a given position (element at the same position is now pos+1). template bool LinkedList::insert(const int newPosition, const ItemType& newEntry) { bool ableToInsert{ (newPosition >= 1) && (newPosition <= itemCount + 1) }; if (ableToInsert) { // Create a new node containing the new entry auto newNodePtr{ std::make_shared>(newEntry) }; // Attach new node to chain if (newPosition == 1) { // Insert new node at beginning of chain newNodePtr->setNext(headPtr); headPtr = newNodePtr; } else { // Find node that will be before new node auto prevPtr{ getNodeAt(newPosition - 1) }; // Insert new node after node to which prevPtr points newNodePtr->setNext(prevPtr->getNext()); prevPtr->setNext(newNodePtr); } // end if itemCount++; // .