The document provides code for implementing a linked list data structure in C++. It includes class definitions for a ListInterface, Node, and LinkedList class. The Node class represents individual nodes in the linked list. The LinkedList class implements the linked list and inherits from ListInterface. It uses Node objects to store and connect list elements. Helper functions and exceptions are also defined to support linked list operations like insertion, removal and accessing elements.
3. } // end getNext
//PrecondViolatedExcept.h
#ifndef PRECOND_VIOLATED_EXCEPT_
#define PRECOND_VIOLATED_EXCEPT_
#include
#include
class PrecondViolatedExcept : public std::logic_error
{
public:
PrecondViolatedExcept(const std::string& message = "");
}; // end PrecondViolatedExcept
#endif
//PrecondViolatedExcept.cpp
#include "PrecondViolatedExcept.h"
PrecondViolatedExcept::PrecondViolatedExcept(const std::string& message)
: std::logic_error("Precondition Violated Exception: " + message)
{
} // end constructor
//LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "ListInterface.h"
#include "Node.h"
#include
#include "PrecondViolatedExcept.h"
template
class LinkedList : public ListInterface
{
private:
Node* 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.
Node* getNodeAt(int position) const;
public:
LinkedList();
4. 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();
ItemType getEntry(int position) const;
void replace(int position, const ItemType& newEntry);
}; // end LinkedList
#endif
//LinkedList.cpp
#include "LinkedList.h"
#include
#include
#include
#include
//Default constructor
template
LinkedList::LinkedList() : headPtr(nullptr), itemCount(0)
{
}
//Copy constructor
template
LinkedList::LinkedList(const LinkedList& aList)
{
Node* aListheadPtr = aList.headPtr;
itemCount=aList.itemCount;
//Check given list is empty
if (aListheadPtr == nullptr)
//If aList is empty then set headPtr to null.
headPtr = nullptr;
else
{
//copying first node
headPtr = new Node();
5. headPtr->setItem(aListheadPtr->getItem());
//*this headPtr
Node* myListHeadPtr = headPtr;
//Get next node in the aList
aListheadPtr = aListheadPtr->getNext();
//Copy all the items
while (aListheadPtr != nullptr)
{
// Get from aList
ItemType nxtItm = aListheadPtr->getItem();
// Create new node for nxtItm
Node* nNodePointer = new Node(nxtItm);
// place new node
myListHeadPtr->setNext(nNodePointer);
// Increment last pointer
myListHeadPtr = myListHeadPtr->getNext();
// Move to next element in aList
aListheadPtr = aListheadPtr->getNext();
}
//Set end
myListHeadPtr->setNext(nullptr);
}
}
//Destructor that destroy the object
template
LinkedList::~LinkedList()
{
//Clear all elements
clear();
//Reset itemCount
itemCount=0;
6. }
//Function to check *this is empty.
//It return 0 if list is empty, otherwise 1
template
bool LinkedList::isEmpty() const
{
//Check itemCount is 0.
if(itemCount==0)
//Return true
return true;
//Return false
return false;
}
//Function return the length of the list *this
template
int LinkedList::getLength() const
{
//Return the itemCount
return itemCount;
}
//Function to insert the given newEntry at the said position
template
bool LinkedList::insert(int newPosition, const ItemType& newEntry)
{
//Check position is available
bool isPossInsert = false;
if((newPosition <= itemCount + 1) && (newPosition >= 1))
//Set possible to insert to true.
isPossInsert = true;
//If possible to insert then
if (isPossInsert)
{
//Create a nNodePointer for newEntry
Node* nNodePointer = new Node(newEntry);
// Check if newPosition is 1
7. if (newPosition == 1)
{
// Insert newEntry at start
nNodePointer->setNext(headPtr);
//set the headPtr
headPtr = nNodePointer;
}
//Otherwise insert the node in the middle
else
{
//Find the previous node
Node* myPrePtrr = getNodeAt(newPosition - 1);
//Insert nNodePointer after myPrePtrr
nNodePointer->setNext(myPrePtrr->getNext());
myPrePtrr->setNext(nNodePointer);
}
//increment the item count
itemCount++;
}
//Return the isPossInsert
return isPossInsert;
}
//Function to remove the node at the position
template
bool LinkedList::remove(int position)
{
//Check possible to remove node at "position"
bool isPossRemove =false;
if((position <= itemCount) && (position >= 1))
isPossRemove =true;
//If possible to remove
if (isPossRemove)
{
//Temp node
Node* myCurrPtrr = nullptr;
8. //Check first node needed to be deleted
if (position == 1)
{
// Remove the 1st node
myCurrPtrr = headPtr;
//Reset the headPtrS
headPtr = headPtr->getNext();
}
//Otherwise, node is to deleted in somewhere else
else
{
int ppId=position - 1
// Find prev node
Node* myPrePtrr = getNodeAt(ppId);
// Get the node to be deleted
myCurrPtrr = myPrePtrr->getNext();
//Reset the node connection
myPrePtrr->setNext(myCurrPtrr->getNext());
}
// set the curr node
myCurrPtrr->setNext(nullptr);
//Delete node
delete myCurrPtrr;
myCurrPtrr = nullptr;
//Decrement the item count.
itemCount--;
}
//return isPossRemove
return isPossRemove;
}
//Function to empty the *this
template
void LinkedList::clear()
9. {
//Until elements in the list
while (!isEmpty())
//Remove the element
remove(1);
}
//Function to item at the position
template
ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept)
{
// Check possibility to retrieve item
bool isPossRetrieve = (position >= 1) && (position <= itemCount);
//if possible to get item then
if (isPossRetrieve)
{
//Get node at the "position"
Node* nndePointr = getNodeAt(position);
//Return the item
return nndePointr->getItem();
}
else
{
//Throw a PrecondViolatedExcept exception
throw(PrecondViolatedExcept("List is empty or invalid position"));
}
}
//Function to replace item at position with newEntry
template
void LinkedList::replace(int position, const ItemType& newEntry)
throw(PrecondViolatedExcept)
{
// Check possibility to replace
bool isPossReplace = (position >= 1) && (position <= itemCount);
//If possible to replace
if (isPossReplace)
{
10. //Get node at the "position"
Node* nndePointr = getNodeAt(position);
//Set item to newEntry
nndePointr->setItem(newEntry);
}
else
{
//Throw a PrecondViolatedExcept exception
throw(PrecondViolatedExcept("Position is not valid"));
}
}
//Function to get node at specified position
template
Node* LinkedList::getNodeAt(int position) const
{
//Assert the position
assert( (position >= 1) && (position <= itemCount) );
// get headPtr
Node* myCurrPtrr = headPtr;
//Move to the specified position
for (int kk = 1; kk < position; kk++)
//Move to next node
myCurrPtrr = myCurrPtrr->getNext();
//Return the node
return myCurrPtrr;
}
//main.cpp
#include
#include
#include "ListInterface.h"
#include "Node.h"
#include "PrecondViolatedExcept.h"
#include "LinkedList.h"
using namespace std;
int main()
{
11. LinkedList ll;
int a=10;
int b=20;int c=15;
ll.insert(0,a);
ll.insert(2,b);
ll.insert(1,c);
cout<<"List length:"<
Solution
Answer:
Note: LinkedList.cpp is written and driver program main.cpp is also given.
//ListInterface.h
#ifndef _LIST_INTERFACE
#define _LIST_INTERFACE
template
class ListInterface
{
public:
virtual bool isEmpty() const = 0;
virtual int getLength() const = 0;
virtual bool insert(int newPosition, const ItemType& newEntry) = 0;
virtual bool remove(int position) = 0;
virtual void clear() = 0;
virtual ItemType getEntry(int position) const = 0;
virtual void replace(int position, const ItemType& newEntry) = 0;
}; // end ListInterface
#endif
//Node.h
#ifndef NODE_
#define NODE_
template
class Node
{
private:
ItemType item; // A data item
14. {
private:
Node* 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.
Node* 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();
ItemType getEntry(int position) const;
void replace(int position, const ItemType& newEntry);
}; // end LinkedList
#endif
//LinkedList.cpp
#include "LinkedList.h"
#include
#include
#include
#include
//Default constructor
template
LinkedList::LinkedList() : headPtr(nullptr), itemCount(0)
{
}
//Copy constructor
template
LinkedList::LinkedList(const LinkedList& aList)
{
Node* aListheadPtr = aList.headPtr;
15. itemCount=aList.itemCount;
//Check given list is empty
if (aListheadPtr == nullptr)
//If aList is empty then set headPtr to null.
headPtr = nullptr;
else
{
//copying first node
headPtr = new Node();
headPtr->setItem(aListheadPtr->getItem());
//*this headPtr
Node* myListHeadPtr = headPtr;
//Get next node in the aList
aListheadPtr = aListheadPtr->getNext();
//Copy all the items
while (aListheadPtr != nullptr)
{
// Get from aList
ItemType nxtItm = aListheadPtr->getItem();
// Create new node for nxtItm
Node* nNodePointer = new Node(nxtItm);
// place new node
myListHeadPtr->setNext(nNodePointer);
// Increment last pointer
myListHeadPtr = myListHeadPtr->getNext();
// Move to next element in aList
aListheadPtr = aListheadPtr->getNext();
}
//Set end
myListHeadPtr->setNext(nullptr);
}
16. }
//Destructor that destroy the object
template
LinkedList::~LinkedList()
{
//Clear all elements
clear();
//Reset itemCount
itemCount=0;
}
//Function to check *this is empty.
//It return 0 if list is empty, otherwise 1
template
bool LinkedList::isEmpty() const
{
//Check itemCount is 0.
if(itemCount==0)
//Return true
return true;
//Return false
return false;
}
//Function return the length of the list *this
template
int LinkedList::getLength() const
{
//Return the itemCount
return itemCount;
}
//Function to insert the given newEntry at the said position
template
bool LinkedList::insert(int newPosition, const ItemType& newEntry)
{
//Check position is available
bool isPossInsert = false;
if((newPosition <= itemCount + 1) && (newPosition >= 1))
17. //Set possible to insert to true.
isPossInsert = true;
//If possible to insert then
if (isPossInsert)
{
//Create a nNodePointer for newEntry
Node* nNodePointer = new Node(newEntry);
// Check if newPosition is 1
if (newPosition == 1)
{
// Insert newEntry at start
nNodePointer->setNext(headPtr);
//set the headPtr
headPtr = nNodePointer;
}
//Otherwise insert the node in the middle
else
{
//Find the previous node
Node* myPrePtrr = getNodeAt(newPosition - 1);
//Insert nNodePointer after myPrePtrr
nNodePointer->setNext(myPrePtrr->getNext());
myPrePtrr->setNext(nNodePointer);
}
//increment the item count
itemCount++;
}
//Return the isPossInsert
return isPossInsert;
}
//Function to remove the node at the position
template
bool LinkedList::remove(int position)
{
18. //Check possible to remove node at "position"
bool isPossRemove =false;
if((position <= itemCount) && (position >= 1))
isPossRemove =true;
//If possible to remove
if (isPossRemove)
{
//Temp node
Node* myCurrPtrr = nullptr;
//Check first node needed to be deleted
if (position == 1)
{
// Remove the 1st node
myCurrPtrr = headPtr;
//Reset the headPtrS
headPtr = headPtr->getNext();
}
//Otherwise, node is to deleted in somewhere else
else
{
int ppId=position - 1
// Find prev node
Node* myPrePtrr = getNodeAt(ppId);
// Get the node to be deleted
myCurrPtrr = myPrePtrr->getNext();
//Reset the node connection
myPrePtrr->setNext(myCurrPtrr->getNext());
}
// set the curr node
myCurrPtrr->setNext(nullptr);
//Delete node
delete myCurrPtrr;
myCurrPtrr = nullptr;
19. //Decrement the item count.
itemCount--;
}
//return isPossRemove
return isPossRemove;
}
//Function to empty the *this
template
void LinkedList::clear()
{
//Until elements in the list
while (!isEmpty())
//Remove the element
remove(1);
}
//Function to item at the position
template
ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept)
{
// Check possibility to retrieve item
bool isPossRetrieve = (position >= 1) && (position <= itemCount);
//if possible to get item then
if (isPossRetrieve)
{
//Get node at the "position"
Node* nndePointr = getNodeAt(position);
//Return the item
return nndePointr->getItem();
}
else
{
//Throw a PrecondViolatedExcept exception
throw(PrecondViolatedExcept("List is empty or invalid position"));
}
}
//Function to replace item at position with newEntry
20. template
void LinkedList::replace(int position, const ItemType& newEntry)
throw(PrecondViolatedExcept)
{
// Check possibility to replace
bool isPossReplace = (position >= 1) && (position <= itemCount);
//If possible to replace
if (isPossReplace)
{
//Get node at the "position"
Node* nndePointr = getNodeAt(position);
//Set item to newEntry
nndePointr->setItem(newEntry);
}
else
{
//Throw a PrecondViolatedExcept exception
throw(PrecondViolatedExcept("Position is not valid"));
}
}
//Function to get node at specified position
template
Node* LinkedList::getNodeAt(int position) const
{
//Assert the position
assert( (position >= 1) && (position <= itemCount) );
// get headPtr
Node* myCurrPtrr = headPtr;
//Move to the specified position
for (int kk = 1; kk < position; kk++)
//Move to next node
myCurrPtrr = myCurrPtrr->getNext();
//Return the node
return myCurrPtrr;
}
//main.cpp