Don't just copy from another forum please help, just need help finishing the following list.cpp class, I just need the last 2 function implementations on
Don't just copy from another forum please help, just need help finishing the following list.cpp class, I just need the last 2 function implementations on list.cpp, use list.h class as reference to complete the program, Please look through the code and then finish it by correctly inserting code on llist::llist(const llist& Original) {} on list.cpp to complete the code, thanks in advance. Please follow the guideline provided below, and below the guideline is the code that needs to be completed.
copy constructor for pass by value and return by value (see below)
operator= for allowing the client to use = for linked list objects (see below)
Allow L1 = L2 in the client
llist& llist::operator=(const llist& OtherOne)
{
// First make sure this-> and OtherOne are not the same object.
// To do this, compare the pointers to the objects .
if (&OtherOne != this) // if not the same
{
// this-> object has to be emptied first.
while (! this->isEmpty() )
this->deleteRear();
// this-> object has to be built up by allocating new cells with OtherOne elements (**)
Node* P; // local pointer for OtherOne
P = OtherOne.Front;
while (P != NULL) // a loop which repeats until you reach the end of OtherOne.
{
this->addRear(P->elem); //Ps element is added to this->
P = P->Next; // Go to the next node in OtherOne
}
}// end of if
return *this; // return the result unconditionally. Note that the result is returned by reference.
}
Allow passing of a list by value and returning of a list by value
// Note that the Original is being passed to the constructor by reference
// to create a new object this-> as a copy of the Original
llist::llist(const llist& Original)
{
// this->'s data members need to be initialized here first
Front = NULL; Rear = NULL; Count = 0;
// this-> object has to be built up by allocating new cells
// and copying the values from Original into each cell as we did with
// operator= above. To do this,
copy here the (**) lines in Operator Overloading of = but note that it is Original and not OtherOne.
// Nothing to return because this is a constructor.
}
-----------------------------------------------------------------------------
// llist.cpp
using namespace std;
#include
#include"llist.h"
llist::llist()
{ Front = Rear = NULL;
Count = 0;}
llist::~llist() { cout << "Calling the llist deconstructor";
el_t oldNum;
while(!isEmpty()) { //keep deleting nodes until it's empty.
deleteFront(oldNum);
}
}
//PURPOSE: Check if the list is empty by checking if both front and rear are NULL and that Count = 0.
//PARAMETER:
bool llist::isEmpty() { return Front == NULL && Rear == NULL && Count == 0;}
//PURPOSE: Display all the elements in all the nodes.
//PARAMETER:
void llist::displayAll() { if (isEmpty()) //Check that there is something to display.
cout << "[ empty ]" << endl;
else { //else, there really is something to display
Node *P = Front; //set a temporary pointer to the front of the list.
while (P != NULL) { //while it isn't NULL (Reach the end) display the elements in brackets.
cout << "[" << P->Elem << "] ";
P = P->Next; //move the pointer to the next node.
}
cout << endl;
}
}
//PURPOSE: add an element to the front of the list
//PARAMETER: The new element we want to add to the rear of list
void llist::addRear(el_t NewNum) { if (Front == NULL) { //if this is the first element we are adding, it needs to be made front and rear
Front = new Node; //make a new node that is front
Rear = Front; //set both front and rear to the same node
}
else { //else, we can simply add a new node at the rear of the list
Rear->Next = new Node; //make a new rear->next node to take the elem
Rear = Rear->Next; //set rear to that node
}
Rear->Elem = NewNum; //give the new node the elem
Rear->Next = NULL; //set the rear->next to NULL for reasons
Count++;}
//PURPOSE: add a new node with the element to the rear of the list.
//PARAMETER: The new element that we want to add to the rear of the list.
void llist::addFront(el_t NewNum) { if (Front == NULL) { //make sure we are empty
Front = new Node; //make a new front node
Front->Elem = NewNum; //front-> elem is new filled with our new elem
Front->Next = NULL; //front->next shouldn't be pointing to anything
Rear = Front; //front and rear are still pointing to the same thing
}
else {
Node *x; //make a new temp node x
x = new Node;
x->Next = Front; //set x->next to front so it is added to the front
Front = x; //set front to x
Front->Elem = NewNum; //add the new elem
}
Count++;
}
//PURPOSE: delete the front node and elem
//PARAMETER: The variable we will be passing the old elem to that will be deleted.
void llist::deleteFront(el_t& OldNum) { if (isEmpty()) //error case: make sure we have something to delete
throw Underflow();
else if (Count == 1) { //else, if there's only on elem
OldNum = Front->Elem; //set the returned variable to the old elem in the node.
delete Front; //delete the front elem
Front = NULL; //set front to NULL
Rear = NULL; //same with rear
Count--; //decrement count
}
else { //else, we have more than one node/elem
OldNum = Front->Elem; //set the returned variable to the old elem in the node.
Node *Second; //make a temporary node so we don't lose front.
Second = Front->Next; //make the temp node->next point to the new front
delete Front; //delete front
Front = Second; //set front to the temporary node (new front)
Count--;
}
}
//PURPOSE: delete the rear elem and pass the elem back
//PARAMETER: the variable that will receive the old elem
void llist::deleteRear(el_t& OldNum) { if (isEmpty()) //error case: check if empty
throw Underflow(); //send error
else if (Count == 1) { //if only one elem/node is present
OldNum = Rear->Elem; //save the deleted elem
delete Rear; //delete rear
Front = NULL; //set rear and front to null
Rear = NULL;
Count--; //decrement count
}
else { //else, we have more than one elem/node
OldNum = Rear->Elem; //save the old elem to be deleted.
Node *p = Front; //make a temporary pointer at front
// Make p go to the one right before rear (using while)
while (p->Next != Rear) //we need to move through the list until we reach rear
p = p->Next; //keep moving until p->next = rear
delete Rear; //delete rear
Rear = p; //make p the new rear
Rear->Next = NULL; //make p->next = null
Count--; //decrement count
}
}
//PURPOSE: delete the designated elem/node.
//PARAMETER: The node you wish deleted and the variable to receive the old elem.
void llist::deleteIth(int I, el_t& OldNum) { if (I > Count || I < 1) //make sure we have a valid number
throw OutOfRange();
else if (I == 1) //if first elem, call the func
deleteFront(OldNum);
else if (I == Count) //if the last elem, call the func
deleteRear(OldNum);
else { //else, it's in the middle
Node *P = Front; //make a temp point at p
Node *PToDelete = Front->Next; //make a temp pointer at the next node after front
for (int K = 1; K <= I-1; K++) { //go through the list one node at a time, until we hit our number-1
P = P->Next;
PToDelete = PToDelete->Next;
}
P->Next = PToDelete->Next; //then we want to make p->next to point to the elem after the one being deleted
OldNum = PToDelete->Elem; //save the old elem
delete PToDelete; //delete the node
Count--; //decrement count
}
}
//PURPOSE: inserts the designated elem/node.
//PARAMETER: the node you wish to inserted and the variable to receive the elem.
void llist::insertIth(int I, el_t newNum)
{ if (I > Count+1 || I < 1) // error case
throw OutofRange();
else if (I == 1) //special case
addFront(newNum);
else if (I == Count+1) //special case
addRear(newNum);
else { //else it's somewhere in the middle
Node *P = Front; //make a temp node at front to move through the list
for (int K = 1; K <= I-1; K++) { //move through the list one node at a time
P = P->Next;
}
Node *temp = P->Next; //make a temp node to save p->next
P->Next = new Node; //make a new node at p->next
P->Next->Next = temp; //set that new node's->next to the temp node
P->Next->Elem = newNum; //give it an elem
Count++; //increment count
}
}
//PURPOSE:
//PARAMETER:
llist::llist(const llist& Original) { }
//PURPOSE:
//PARAMETER:
llist& llist::operator=(const llist& OtherOne) { }
//---------------------------------------------------------------------
// List.h
class llist
{
private:
Node *Front; // pointer to the front node
Node *Rear; // pointer to the rear node
int Count; // counter for the number of nodes
public:
// Exception handling classes
class Underflow{};
class OutOfRange{}; // thrown when the specified Node is out of range
llist (); // constructor to create a list object
~llist(); // destructor to destroy all nodes
// PURPOSE: Check if the list is empty by checking if both front and rear are NULL and that Count = 0.
bool isEmpty();
// PURPOSE: Display all the elements in all the nodes.
void displayAll();
// PURPOSE: add a new node with the element to the rear of the list.
// PARAMETERS: The new element that we want to add to the rear of the list.
void addFront(el_t);
// PURPOSE: add an element to the front of the list
// PARAMETERS: the element that we will add to the front of the list
void addRear(el_t);
// PURPOSE: delete the front node and elem
// PARAMETERS: The variable we will be passing the old elem to that will be deleted.
void deleteFront(el_t&);
// PURPOSE: delete the rear elem and pass the elem back
// PARAMETERS: the variable that will receive the old elem
void deleteRear(el_t&);
// PURPOSE: delete the designated elem/node.
// PARAMETERS: The node you wish deleted and the variable to receive the old elem.
void deleteIth(int, el_t&);
// PURPOSE: inserts the designated elem/node
// PARAMETERS: the node you want inserted and the variable to receive the old elem.
void insertIth(int, el_t);
//Copy Constructor to allow pass by value and return by value
llist(const llist&);
//Overloading of =
llist& operator=(const llist&);
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started