Write a C++ code to implement the classes. It will include a header file, an imp
ID: 3871973 • 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
Sorry yaar. Running out of time. Its partially working. Just check it out.
#include "list.h"
Node::Node() // Default constructor, sets value to 0 and pointers(next and prev) to NULL
{
value = 0;
next = prev = NULL;
}
Node::Node(int val) // Constructor, sets value = <Parameter> and pointers(next and prev) to NULL
{
value = val;
next = prev = NULL;
}
int Node::GetValue() // Returns the value
{
return value;
}
void Node::SetValue(int val) // Sets value = <Parameter>
{
value = val;
}
Node* Node::GetNext() // Returns the next pointer
{
return next;
}
Node* Node::GetPrev() // Returns the prev pointer;
{
return prev;
}
void Node::SetNext(Node *nxt) // Sets next pointer = <Parameter>
{
next = nxt;
}
void Node::SetPrev(Node *prv) // Sets prev pointer = <Parameter>
{
prev = prv;
}
LinkedList::LinkedList() // Default constructor, sets pointers(head and tail) to NULL
{
head = tail = NULL;
}
LinkedList::~LinkedList() // Destructor: Deletes all nodes in the list and sets pointers(head and tail) to NULL
{
head = tail = NULL;
}
bool LinkedList::Insert(int val) // 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.
{
Node* temp = head;
Node* newNode = new Node(val);
if(head == NULL) //If there are no nodes, make this the head & tail.
{
head = tail = newNode;
return true;
}
else if(head->GetValue() > val) //If the node to be inserted is the smallest, make this the first node.
{
newNode->SetNext(head);
head->SetPrev(newNode);
head = newNode;
return true;
}
else //If not.
{
while(temp->GetNext() != NULL && temp->GetValue() <= val) //Traverse till either the end is reached, or till the position is found.
temp = temp->GetNext();
if(temp->GetNext() == NULL && temp->GetValue() != val) //If the node is to be inserted is, at the end.
{
tail->SetNext(newNode); //Connect this at the end.
newNode->SetNext(tail); //Update the new node previous value.
tail = newNode; //Update the tail.
return true;
}
if(temp->GetValue() == val) //If the value to be inserted is a duplicate, don't insert.
return false;
newNode->SetPrev(temp->GetPrev()); //Update the new node previous pointer.
newNode->SetNext(temp);
newNode->GetPrev()->SetNext(newNode);
temp->SetPrev(newNode);
return true;
}
}
bool LinkedList::Delete(int val) // 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.
{
return false;
}
bool LinkedList::Search(int val) // 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.
{
Node* temp = head;
while(temp != NULL)
{
if(temp->GetValue() == val)
return true;
}
return false;
}
void LinkedList::PrintAsc() // prints the entire list i.e. all node values in ascending order and pointers in one line, like the following examples
{
Node* temp = head;
cout << "NULL<=>";
while(head != NULL)
{
cout << temp->GetValue() << "<=>";
temp = temp->GetNext();
}
cout << "NULL" << endl;
}
void LinkedList::PrintDesc() // prints the entire list i.e. all node values in descending order and pointers in one line, like the following exa$
{
Node* temp = tail;
cout << "NULL<=>";
while(tail != NULL)
{
cout << temp->GetValue() << "<=>";
temp = temp->GetPrev();
}
cout << "NULL" << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.