Question: using namespace std; #include #includellist.h llist::llist ( ) { cout < < . . . in llist constructor... < < endl; Count =

using namespace std;
#include
#include"llist.h"
llist::llist()
{ cout <<"... in llist constructor..." << endl;
Count =0;
Front = NULL;
Rear = NULL;
}
llist::~llist()
{ cout <<".... in llist destructor..." << endl;
int x;
while(!isEmpty())
{
deleteRear(x); // It is called to delete all the nodes
}
}
//PURPOSE: This function checks if the list is empty or not
//PARAMETER: None
bool llist::isEmpty()
// be sure to check all 3 data members
{
if (Front == NULL && Rear == NULL && Count ==0)
return true;
else
return false;
}
//PURPOSE: This function displays all the elements in the list
//PARAMETER: None
void llist::displayAll()
{// be sure to display horizontally in [] with
// blanks between elements e.g.[1234]
// You MUST use while (P != NULL) loop or you will not get the credit!
Node *p = Front;
if (isEmpty())// SPECIAL CASE
cout<<"[Empty]"<< endl;
else
{//REGULAR CASE
cout<<"[";
while(p != NULL)
{
cout<< p->Elem <<"";
p = p->Next;
}
cout<<"]"<< endl;
}
}
//PURPOSE: This function adds a node at the end of the end of the list
//PARAMETER: NewNum - this is the number which we want add it to the list .
void llist::addRear(el_t NewNum)
{// comment the 2 cases
//SPECIAL CASE
if(isEmpty())
{
Node *p = new Node;
Front = p;
Rear = p;
Rear->Next = NULL;
Rear->Elem = NewNum;
Count++;
}
else
{
//REGULAR CASE
Rear->Next = new Node;
Rear = Rear->Next;
Rear->Elem = NewNum;
Rear->Next = NULL;
Count++;
}
}
//PURPOSE: This function is going to add an element in the front of the loop!
//PARAMETER: NewNum - This is the elememt that is to be added to the front of the list.
void llist::addFront(el_t NewNum)
{// comment the 2 cases
//SPECIAL CASE
if(isEmpty())
{
Node *p = new Node;
Front = p;
Rear = p;
Rear->Elem = NewNum;
Rear->Next = NULL;
Count++;
}
else
{
//REGULAR CASE
Node *p = new Node;
p->Next = Front;
Front = p;
Front->Elem = NewNum;
Count++;
}
}
//PURPOSE: This function deletes the front element of the list.
//PARAMETER: OldNum -old num is the variable which will store the
//value of teh variable before it gets deleted.
void llist::deleteFront(el_t& OldNum)
{// comment the 3 cases
//ERROR CASE
if (isEmpty())
throw Underflow();
else
{
//SPECIAL CASE
Node *p = Front;
if (Count ==1)
{
OldNum = Front->Elem;
delete Front;
Front = NULL;
Rear = NULL;
Count--;
}
else
{
// REGULAR CASE
OldNum = Front->Elem;
Node *Second;
Second = Front->Next;
delete Front;
Front = Second;
Count--;
}
}
}
//PURPOSE: This Function is used to delete the rear number from the list
//PARAMETER: OldNum - this stores the number in it beforeit gets deleted.
void llist::deleteRear(el_t& OldNum)
{// comment the 3 cases
//ERROR CASE
if(isEmpty())
throw Underflow();
else
{
//SPECIAL CASE
if(Count ==1)
{
OldNum = Front->Elem;
delete Rear;
Front = NULL;
Rear = NULL;
Count--;
}
//REGULAR CASE
else
{
OldNum = Rear -> Elem;
Node* p = Front;
while(p -> Next != Rear){
p = p->Next;
}
delete Rear;
Rear = p;
Rear -> Next == NULL;
Count--;
OldNum = Rear -> Elem;
Node* traversal = Front;
while(traversal -> Next != Rear){
traversal = traversal -> Next;
}
delete Rear;
Rear = traversal;
Rear -> Next = NULL;
Count --;
}
}
}
// Utility Function to move a local pointer to the Jth node
void llist::moveTo(int J, Node*& temp)
{// Note that case1 client does not test this. But it is neede
// for case 2 so make it work perfectly.
for ( int K =1; K < J-1; K++)
// moves temp J-1 times to go to the Jth node (>=1 and <= Count is assumed)
// for ( int K =...) temp = temp->Next;
temp = temp->Next;
}
/*--- harder ones for case 2 and 3 follow --*/
//PURPOSE: This function is used to to delete an elememt at the Ith position
//PARAMETER: I , OldNum - I is the position we are passing and
//oldNum is the number which passed by reference before it gets
//deleted from the list
void llist::deleteIth(int I, el_t& OldNum)
{// must use moveTo to move local pointers. Be sure to comment to which node you are moving them. Do not forget to set OldNum.
//ERROR CASE
if(I > Count || I <1)
throw OutOfRange();
else
{//SPECIAL CASE
if(I ==1)
deleteFront(OldNum);
else if(I == Count)
deleteRear(OldNum);
else
{
//REGULAR CASE
Node *p = Front;
Node *q = Front;
moveTo(I-1, p);
moveTo(I+1, q);
OldNum = p -> Next -> Elem;
delete p -> Next;
p -> Next = q;
Count--;
}
}
}
//PURPOSE: This function is used to insert an element at the Ith position of the list
//PARAMETER: I, newNum - I is the position where the element is
//supposed to be inserted whereas newNum is the number which is to
//be inserted in the list.
void llist::insertIth(int I, el_t new

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!