NEED HELP WRITTING THE METHOD\" LinkedList<ItemType>::LinkedList(const LinkedLis
ID: 3765363 • Letter: N
Question
NEED HELP WRITTING THE METHOD" LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList)"
/** Implementation file for the class LinkedList.
@file LinkedList.cpp */
//#include "LinkedList.h" // Header file
#include <cassert>
template<class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(nullptr), itemCount(0)
{
} // end default constructor
template<class ItemType>
LinkedList<ItemType>::LinkedList(const LinkedList<ItemType>& aList)
{
// WRITES THE CODE TO THIS METHOD - the copy constructor
} // end copy constructor
template<class ItemType>
LinkedList<ItemType>::~LinkedList()
{
clear();
delete headPtr;
headPtr = nullptr;
} // end destructor
template<class ItemType>
bool LinkedList<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
int LinkedList<ItemType>::getLength() const
{
return itemCount;
} // end getLength
template<class ItemType>
bool LinkedList<ItemType>::insert(int newPosition, const ItemType& newEntry)
{
bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
if (ableToInsert)
{
// Create a new node containing the new entry
Node<ItemType>* newNodePtr = new Node<ItemType>(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
Node<ItemType>* prevPtr = getNodeAt(newPosition - 1);
// Insert new node after node to which prevPtr points
newNodePtr->setNext(prevPtr->getNext());
prevPtr->setNext(newNodePtr);
} // end if
itemCount++; // Increase count of entries
} // end if
return ableToInsert;
} // end insert
template<class ItemType>
bool LinkedList<ItemType>::remove(int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
Node<ItemType>* curPtr = nullptr;
if (position == 1)
{
// Remove the first node in the chain
curPtr = headPtr; // Save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<ItemType>* prevPtr = getNodeAt(position - 1);
// Point to node to delete
curPtr = prevPtr->getNext();
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
// Return node to system
curPtr->setNext(nullptr);
delete curPtr;
curPtr = nullptr;
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
template<class ItemType>
void LinkedList<ItemType>::clear()
{
while (!isEmpty())
remove(1);
} // end clear
template<class ItemType>
ItemType LinkedList<ItemType>::getEntry(int position) const throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
Node<ItemType>* nodePtr = getNodeAt(position);
return nodePtr->getItem();
}
else
{
string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
} // end if
} // end getEntry
template<class ItemType>
void LinkedList<ItemType>::setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep)
{
//STUDENT WRITES THE CODE TO THIS METHOD - setEntry
throw(PrecondViolatedExcep)
{
//Enforece precondition
bool ableToset = (position >= 1) ** (position <= itemCount);
if(ableToset)
{
Insert(position, newEntry);
}
else{
string message = "setEntry() called with ";
message = message + "invalid position";
throw(PrecondViolatedExcep(message));
}
}
} // end setEntry
template<class ItemType>
Node<ItemType>* LinkedList<ItemType>::getNodeAt(int position) const
{
// Debugging check of precondition
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<ItemType>* curPtr = headPtr;
for (int skip = 1; skip < position; skip++)
curPtr = curPtr->getNext();
return curPtr;
} // end getNodeAt
// End of implementation file.
//LinkedList.h
/** ADT list: Link-based implementation.
Listing 9-2.
@file LinkedList.h */
#ifndef _LINKED_LIST
#define _LINKED_LIST
#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
//Node.h
/** @file Node.h
Listing 4-1 */
#ifndef _NODE
#define _NODE
#include<iostream>
#include<cstddef>
using namespace::std;
template<class ItemType>
class Node
{
private:
ItemType item; // A data item
Node<ItemType>* next; // Pointer to next node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node<ItemType>* nextNodePtr);
void setItem(const ItemType& anItem);
void setNext(Node<ItemType>* nextNodePtr);
ItemType getItem() const ;
Node<ItemType>* getNext() const ;
}; // end Node
#include "Node.cpp"
#endif
//PrecondViolatedExcep.h
/** Listing 7-5.
@file PrecondViolatedExcep.h */
#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP
#include <stdexcept>
#include <string>
using namespace std;
class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif
Explanation / Answer
#include "LinkedList.h" // Header file
#include <cassert>
template<class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(NULL), itemCount(0)
{
} // end default constructor
template<class ItemType>
LinkedList<ItemType>::~LinkedList()
{
clear();
} // end destructor
template<class ItemType>
bool LinkedList<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
int LinkedList<ItemType>::getLength() const
{
return itemCount;
} // end getLength
template<class ItemType>
bool LinkedList<ItemType>::insert(int newPosition, const ItemType& newEntry)
{
bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
if (ableToInsert)
{
// Create a new node containing the new entry
Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry);
headPtr = insertNode(newPosition, newNodePtr, headPtr);
}// end if
return ableToInsert;
} // end insert
template<class ItemType>
bool LinkedList<ItemType>::remove(int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
Node<ItemType>* curPtr = NULL;
if (position == 1)
{
// Remove the first node in the chain
curPtr = headPtr; // Save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<ItemType>* prevPtr = getNodeAt(position - 1);
// Point to node to delete
curPtr = prevPtr->getNext();
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
// Return node to system
curPtr->setNext(NULL);
delete curPtr;
curPtr = NULL;
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
template<class ItemType>
void LinkedList<ItemType>::clear()
{
while (!isEmpty())
remove(1);
} // end clear
template<class ItemType>
ItemType LinkedList<ItemType>::getEntry(int position) const throw(PrecondViolatedExcep)
{
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
Node<ItemType>* nodePtr = getNodeAt(position);
return nodePtr->getItem();
}
else
{
string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
throw(PrecondViolatedExcep(message));
} // end if
} // end getEntry
template<class ItemType>
Node<ItemType>* LinkedList<ItemType>::getNodeAt(int position) const
{
// Debugging check of precondition
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<ItemType>* curPtr = headPtr;
for (int skip = 1; skip < position; skip++)
curPtr = curPtr->getNext();
return curPtr;
} // end getNodeAt
// RECURSIVE
template<class ItemType>
Node<ItemType>* LinkedList<ItemType>::insertNode(int position, Node<ItemType>* newNodePtr,
Node<ItemType>* subChainPtr)
{
if (position == 1)
{
// Insert new node at beginning of subchain
newNodePtr->setNext(subChainPtr);
subChainPtr = newNodePtr;
itemCount++; // Increase count of entries
}
else
{
Node<ItemType>* afterPtr = insertNode(position - 1, newNodePtr, subChainPtr->getNext());
subChainPtr->setNext(afterPtr);
} // end if
return subChainPtr;
} // end insertNode
// End of implementation file.
#ifndef _LINKED_LIST
#define _LINKED_LIST
#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);
LinkedList<ItemType> & operator = ( const LinkedList<ItemType> & aList );
}; // end LinkedList
#include "LinkedList.cpp"
#endif
******************************************************************************
class Message
{
private:
vector<string> messageLines;
int numLines;
public:
void setNumLines(int nLines) {numLines = nLine;);//setting number of lines in the message
bool setMessage(int lineNum, string message);//fills up the vector
ostream& writeMessage(ostream &os)// overloding << operator, displays the vector.
}
bool Message :: setMessage(int lineNum, string message)
{
if (lineNum > 0)
{
messageLines.push_back(message);
return true;
}
}
ostream& Message:: writeMessage(ostream &os)
{
for (int i = 0 ; i < messageLines.size() ; i++)
{
os << messageLines[i] << endl;
}
return os;
}
From my main, after I declared a LinkedList<Message> variable and a Message messageVar to fill up the vector, I inserted the Message variable into a linked list like this:
variable.insert(messageVar);
Here is an example of what my main looks like:
int main()
{
LinkedList<message> variable;
for (int i = 0 ; i < 5 ; i++)
{
Message messageVariable;
messageVariable.setMessage(1, "random strings")
variable.insert(messageVariable) // filling up the list with 5 random strings
}
}
However, when I try to call the linked list's display function and try to display the string values, i realized that the cout << currPtr->getItem() in the display function will not work since it I am displaying the Message type rather than a string. I know I should use the overloaded << operator from the class I wrote but I have no idea where I should use it.
Here is the display function just in case:
template<class ItemType>
void LinkedList<ItemType>::display() const
{
Node<ItemType>* currPtr = headPtr;//The Node<ITemType> is in another header file
while (currPtr->getNext() != headPtr)
{
cout << currPtr->getItem() << endl; // cout can't display Message type
currPtr = currPtr->getNext();
}
cout << endl << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.