//LinkedList.h #include \"ListInterface.h\" #include \"Node.h\" #include \"Preco
ID: 3761017 • Letter: #
Question
//LinkedList.h
#include "ListInterface.h"
#include "Node.h"
#include "PrecondViolatedExcep.h"
template<class ItemType>
class LinkedList : public ListInterface<ItemType>
{
private:
Node<ItemType>* headPtr; // Pointer to first node in the chain;
// (contains the first entry in the list)
int itemCount; // Current count of list items
// Locates a specified node in this linked list.
// @pre position is the number of the desired node;
// position >= 1 and position <= itemCount.
// @post The node is found and a pointer to it is returned.
// @param position The number of the node to locate.
// @return A pointer to the node at the given position.
Node<ItemType>* getNodeAt(int position) const;
public:
LinkedList();
LinkedList(const LinkedList<ItemType>& aList);
virtual ~LinkedList();
bool isEmpty() const;
int getLength() const;
bool insert(int newPosition, const ItemType& newEntry);
bool remove(int position);
void clear();
/** @throw PrecondViolatedExcep if position < 1 or
position > getLength(). */
ItemType getEntry(int position) const throw(PrecondViolatedExcep);
/** @throw PrecondViolatedExcep if position < 1 or
position > getLength(). */
void setEntry(int position, const ItemType& newEntry)
throw(PrecondViolatedExcep);
}; // end LinkedList
#include "LinkedList.cpp"
#endif
template<class ItemType>
void LinkedList<ItemType>::setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep)
{
//STUDENT WRITES THE CODE TO THIS METHOD - setEntry
} // end setEntry
emplate<class ItemType>
LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList)
{
//STUDENT WRITES THE CODE TO THIS METHOD - the copy constructor
}
no need for driverprogram implementation just write these two methods
Explanation / Answer
template<class ItemType>
void LinkedList<ItemType>::setEntry(int position, const ItemType& newEntry)
throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToSet = (position >= 1) && (position <= itemCount);
if (ableToSet)
{
insert(position, newEntry);
}
else
{
std::string message = "setEntry() called with ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
}
}
template<class ItemType>
LinkedList<ItemType>::~LinkedList()
{
clear();
delete headPtr;
headPtr = nullptr;
}
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.