Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help, just need help finishing the following list.cpp class, need the last 3 function implementations on list.cpp, use list.h class as reference to complete

Please help, just need help finishing the following list.cpp class, need the last 3 function implementations on list.cpp, use list.h class as reference to complete the program, Please take a look through my code and then help me out at void llist::insertIth(int I, el_t newNum) { } on list.cpp to complete the code, thanks in advance.

// 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) { }

//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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions