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
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
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 =
int GetValue(); // Returns the value
void SetValue(int); // Sets value =
Node *GetNext(); // Returns the next pointer
Node *GetPrev(); // Returns the prev pointer;
void SetNext(Node *); // Sets next pointer =
void SetPrev(Node *); // Sets prev pointer =
};
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=
bool Delete(int); // Deletes the new node with value=
bool Search(int); // Searches for a node with value=
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
#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;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
