Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ code to implement the classes. It will include a header file, an imp

ID: 3870446 • Letter: W

Question

Write a C++ code to implement the classes. It will include a header file, an implementation file, and the application file. Each line should have an explanation.

                Here is the header file:
#ifndef _H_LIST_

#define _H_LIST_

#include <iostream>

using namespace std;

class Node

{

private:

        int value ;

        Node *next;

        Node *prev;

public:

        Node();              // Default constructor, sets value to 0 and pointers(next and prev) to NULL

        Node(int);           // Constructor, sets value = <Parameter> and pointers(next and prev) to NULL

        int GetValue();                         // Returns the value

        void SetValue(int);                     // Sets value = <Parameter>

        Node *GetNext();                        // Returns the next pointer

        Node *GetPrev();                        // Returns the prev pointer;

        void SetNext(Node *);           // Sets next pointer = <Parameter>

        void SetPrev(Node *);           // Sets prev pointer = <Parameter>

};

class LinkedList

{

private:

        Node *head;

        Node *tail;

                                             // Add more Private Data and/or functions as needed.

public:

        LinkedList();          // Default constructor, sets pointers(head and tail) to NULL

        ~LinkedList();        // Destructor: Deletes all nodes in the list and sets pointers(head and tail) to NULL

        bool Insert(int);         // Insert a new node with value=<parameter> and returns true, if a node already exists with the same value, NO node is inserted and returns false.

        bool Delete(int);       // Deletes the new node with value=<parameter> and returns true, if a node does not exist with the same value, NO node is deleted and returns false.

         bool Search(int);    // Searches for a node with value=<parameter> and returns true if the node is found. if a node does not exist with the same value, NO node is deleted and returns false.

        void PrintAsc();       // prints the entire list i.e. all node values in ascending order and pointers in one line, like the following examples

NULL

NULL<-200->NULL

NULL<-10<=>100<=>200->NULL

NULL<-10<=>50<=>75<=>100<=>120<=>150<=>180<=$

        void PrintDesc();       // prints the entire list i.e. all node values in descending order and pointers in one line, like the following exa$

NULL

NULL<-200->NULL

                                                NULL<-200<=>100<=>10->NULL

NULL<-200<=>180<=>150<=>120<=>100<=>75<=>50<$

};

#endif

                Here is the main file:

#include <iostream>

#include "list.h"

using namespace std;

void RunTestCase(string Description, LinkedList &MyList, char Function, int val)

{

        static int cnt = 0;

        bool result;

        cout << endl << "========= Test Case "<< cnt++ << "-" << Description << ":" << val << endl;

        if (Function == 'I')

                cout << (MyList.Insert(val)?"Inserted":"NOT Inserted") << endl;

        else if (Function == 'D')

                cout << (MyList.Delete(val)?"Deleted":"NOT Deleted") << endl;

        else if (Function == 'S')

                cout << (MyList.Search(val)?"Found":"NOT Found") << endl;

        MyList.PrintAsc();

        MyList.PrintDesc();

}

int main()

{

        LinkedList MyList;

        RunTestCase("Insert in an empty list",MyList,'I',50);

        RunTestCase("Insert new tail",MyList,'I',80);

        RunTestCase("Insert new head",MyList,'I',30);

        RunTestCase("Insert innternal node", MyList,'I',40);

        RunTestCase("Insert internal node", MyList,'I',70);

        RunTestCase("Insert new head", MyList,'I',20);

        RunTestCase("Insert new tail", MyList,'I',100);

        RunTestCase("Insert dupluicate head", MyList,'I',20);

        RunTestCase("Insert dupluicate tail", MyList,'I',100);

        RunTestCase("Insert dupluicate node", MyList,'I',30);

        RunTestCase("Insert dupluicate node", MyList,'I',80);

        RunTestCase("Search head", MyList,'S',20);

        RunTestCase("Search tail", MyList,'S',100);

        RunTestCase("Search internal node", MyList,'S',50);

        RunTestCase("Search internal node", MyList,'S',80);

        RunTestCase("Search non existing node", MyList,'S',-10);

        RunTestCase("Search non existing node", MyList,'S',10);

        RunTestCase("Search non existing node", MyList,'S',45);

        RunTestCase("Search non existing node", MyList,'S',290);

        RunTestCase("Delete head", MyList,'D',20);

        RunTestCase("Delete tail", MyList,'D',100);

        RunTestCase("Delete non existing node", MyList,'D',20);

        RunTestCase("Delete non existing node", MyList,'D',100);

        RunTestCase("Delete non existing node", MyList,'D',-10);

        RunTestCase("Delete non existing node", MyList,'D',10);

        RunTestCase("Delete non existing node", MyList,'D',45);

        RunTestCase("Delete non existing node", MyList,'D',290);

        RunTestCase("Insert new head", MyList,'I',15);

        RunTestCase("Insert new tail", MyList,'I',105);

        RunTestCase("Insert innternal node", MyList,'I',20);

        RunTestCase("Insert internal node", MyList,'I',100);

        RunTestCase("Insert dupluicate head", MyList,'I',15);

        RunTestCase("Insert dupluicate tail", MyList,'I',105);

        RunTestCase("Insert dupluicate node", MyList,'I',30);

        RunTestCase("Delete internal node", MyList,'D',20);

        RunTestCase("Delete internal node", MyList,'D',50);

        RunTestCase("Delete internal node", MyList,'D',100);

        RunTestCase("Delete internal node", MyList,'D',40);

        RunTestCase("Delete tail", MyList,'D',80);

        RunTestCase("Delete internal node", MyList,'D',30);

        RunTestCase("Delete head", MyList,'D',15);

        RunTestCase("Delete head", MyList,'D',70);

        RunTestCase("Delete head", MyList,'D',105);

return 0;

}

Explanation / Answer

//contents of lists.h header file

#ifndef _H_LIST_
#define _H_LIST_

#include <iostream>
using namespace std;

class Node
{
private:
        int value ;
        Node *next;
        Node *prev;
public:
        Node();              // Default constructor, sets value to 0 and pointers(next and prev) to NULL
        Node(int);           // Constructor, sets value = <Parameter> and pointers(next and prev) to NULL
        int GetValue();                         // Returns the value
        void SetValue(int);                     // Sets value = <Parameter>
        Node *GetNext();                        // Returns the next pointer
        Node *GetPrev();                        // Returns the prev pointer;
        void SetNext(Node *);           // Sets next pointer = <Parameter>
        void SetPrev(Node *);           // Sets prev pointer = <Parameter>
};

class LinkedList
{
private:
        Node *head;
        Node *tail;
                                 // Add more Private Data and/or functions as needed.
public:
        LinkedList();           // Default constructor, sets pointers(head and tail) to NULL
        ~LinkedList();        // Destructor: Deletes all nodes in the list and sets pointers(head and tail) to NULL
        bool Insert(int);         // Insert a new node with value=<parameter> and returns true, if a node already exists
                                //with the same value, NO node is inserted and returns false.
        bool Delete(int);       // Deletes the new node with value=<parameter> and returns true,
                                //if a node does not exist with the same value, NO node is deleted and returns false.

         bool Search(int);    // Searches for a node with value=<parameter> and returns true
                            //if the node is found. if a node does not exist with the same value,
                            //NO node is deleted and returns false.
         void PrintAsc();       // prints the entire list i.e. all node values in ascending order and pointers in one line, like the following examples

// NULL

// NULL<-200->NULL

// NULL<-10<=>100<=>200->NULL

//NULL<-10<=>50<=>75<=>100<=>120<=>150<=>180<=$

        void PrintDesc();       // prints the entire list i.e. all node values in descending order and pointers in one line, like the following exa$

// NULL

// NULL<-200->NULL

//                                                 NULL<-200<=>100<=>10->NULL

// NULL<-200<=>180<=>150<=>120<=>100<=>75<=>50<$

};

#endif

// end of list,h header file

// contents of list.cc

#include "list.h"

Node::Node()
{
   this->value = 0;
   this->next = NULL;
   this->prev = NULL;
}

Node::Node(int value)
{
   this->value = value;
   this->next = NULL;
   this->prev = NULL;
}

int Node:: GetValue()
{
   return this->value;
}

void Node::SetValue(int value)
{
   this->value = value;
}

Node* Node::GetNext()
{
   return this->next;
}

Node* Node::GetPrev()
{
   return this->prev;
}

void Node::SetNext(Node* next)
{
   this->next = next;
}

void Node::SetPrev(Node* prev)
{
   this->prev = prev;
}


LinkedList::LinkedList()
{
   head = NULL;
   tail = NULL;
}

LinkedList::~LinkedList()
{
   while(this->head)
   {
       Node* temp = this->head;
       this->head = this->head->GetNext();
       delete temp;
   }
   this->tail = NULL;
}

bool LinkedList::Insert(int value)
{
   if (this->head == NULL)
   {
       Node* newNode = new Node;
       this->head = newNode;
       this->tail = newNode;
       newNode->SetNext(NULL);
       newNode->SetPrev(NULL);
       newNode->SetValue(value);
       return true;
   }
   Node* temp = this->head;
   if(this->Search(value))
   {
       cout << "Value Exist already ";
       return false;
   }
   while(temp && temp->GetValue() < value)
   {
       temp = temp->GetNext();
   }

   Node* newNode = new Node;
   newNode->SetValue(value);
   if(!temp)
   {
       newNode->SetNext(NULL);
       newNode->SetPrev(this->tail);
       this->tail->SetNext(newNode);
       this->tail = newNode;
       return true;
   }
   if(temp->GetValue()>=value)
   {
       newNode->SetNext(temp);
       newNode->SetPrev(temp->GetPrev());
       if(temp->GetPrev())
           (temp->GetPrev())->SetNext(newNode);
       else
           this->head = newNode;
       temp->SetPrev(newNode);
       return true;
   }
}

bool LinkedList::Delete(int value)
{
   Node* temp = this->head;
   while(temp && temp->GetValue()<value)
   {
       temp = temp->GetNext();
   }
   if(temp)
   {
       if (temp->GetValue() == value)
       {
           if(temp->GetPrev())
               (temp->GetPrev())->SetNext(temp->GetNext());
           else
               this->head = temp->GetNext();
           if(temp->GetNext())
               (temp->GetNext())->SetPrev(temp->GetPrev());
           delete temp;
           return true;
       }
   }
   return false;
}

bool LinkedList::Search(int value)
{
   Node* temp = this->head;
   while(temp && temp->GetValue()<value)
   {
       temp = temp->GetNext();
   }
   if(temp)
   {
       if (temp->GetValue() == value)
       {
           return true;
       }
   }
   return false;
}

void LinkedList::PrintAsc()
{
   Node* temp = this->head;
   cout << "NULL <-";
   while(temp)
   {
       cout << temp->GetValue() <<"<=>";
       temp = temp->GetNext();
   }
   cout << "NULL ";
}

void LinkedList::PrintDesc()
{
   Node* temp = this->tail;
   cout << "NULL <-";
   while(temp)
   {
       cout << temp->GetValue() <<"<=>";
       temp = temp->GetPrev();
   }
   cout << "NULL ";
}

//end of list.cc

// content of main

#include <iostream>
#include "list.h"

using namespace std;

void RunTestCase(string Description, LinkedList &MyList, char Function, int val)

{

        static int cnt = 0;
        bool result;
        cout << endl << "========= Test Case "<< cnt++ << "-" << Description << ":" << val << endl;
        if (Function == 'I')
                cout << (MyList.Insert(val)?"Inserted":"NOT Inserted") << endl;
        else if (Function == 'D')
                cout << (MyList.Delete(val)?"Deleted":"NOT Deleted") << endl;
        else if (Function == 'S')
                cout << (MyList.Search(val)?"Found":"NOT Found") << endl;
        MyList.PrintAsc();
        MyList.PrintDesc();
}

int main()
{
    LinkedList MyList;

    RunTestCase("Insert in an empty list",MyList,'I',50);
    RunTestCase("Insert new tail",MyList,'I',80);
    RunTestCase("Insert new head",MyList,'I',30);
    RunTestCase("Insert innternal node", MyList,'I',40);
    RunTestCase("Insert internal node", MyList,'I',70);
    RunTestCase("Insert new head", MyList,'I',20);
    RunTestCase("Insert new tail", MyList,'I',100);
    RunTestCase("Insert dupluicate head", MyList,'I',20);
    RunTestCase("Insert dupluicate tail", MyList,'I',100);
    RunTestCase("Insert dupluicate node", MyList,'I',30);
    RunTestCase("Insert dupluicate node", MyList,'I',80);

    RunTestCase("Search head", MyList,'S',20);
    RunTestCase("Search tail", MyList,'S',100);
    RunTestCase("Search internal node", MyList,'S',50);
    RunTestCase("Search internal node", MyList,'S',80);
    RunTestCase("Search non existing node", MyList,'S',-10);
    RunTestCase("Search non existing node", MyList,'S',10);
    RunTestCase("Search non existing node", MyList,'S',45);
    RunTestCase("Search non existing node", MyList,'S',290);

    RunTestCase("Delete head", MyList,'D',20);
    RunTestCase("Delete tail", MyList,'D',100);
    RunTestCase("Delete non existing node", MyList,'D',20);
    RunTestCase("Delete non existing node", MyList,'D',100);
    RunTestCase("Delete non existing node", MyList,'D',-10);
    RunTestCase("Delete non existing node", MyList,'D',10);
    RunTestCase("Delete non existing node", MyList,'D',45);
    RunTestCase("Delete non existing node", MyList,'D',290);

    RunTestCase("Insert new head", MyList,'I',15);
    RunTestCase("Insert new tail", MyList,'I',105);
    RunTestCase("Insert innternal node", MyList,'I',20);
    RunTestCase("Insert internal node", MyList,'I',100);
    RunTestCase("Insert dupluicate head", MyList,'I',15);
    RunTestCase("Insert dupluicate tail", MyList,'I',105);
    RunTestCase("Insert dupluicate node", MyList,'I',30);

    RunTestCase("Delete internal node", MyList,'D',20);
    RunTestCase("Delete internal node", MyList,'D',50);
    RunTestCase("Delete internal node", MyList,'D',100);
    RunTestCase("Delete internal node", MyList,'D',40);
    RunTestCase("Delete tail", MyList,'D',80);
    RunTestCase("Delete internal node", MyList,'D',30);
    RunTestCase("Delete head", MyList,'D',15);
    RunTestCase("Delete head", MyList,'D',70);
    RunTestCase("Delete head", MyList,'D',105);
    return 0;
}

// end of main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote