I need this ASAP complete the code where it says \" // Needs to be completed…. \
ID: 3820282 • Letter: I
Question
I need this ASAP
complete the code where it says " // Needs to be completed…. "
Codes for doublelinkedlist hearder and implementation
#ifndef DOUBLELINKEDLIST_H
#define DOUBLELINKEDLIST_H
#include <iostream>
using namespace std;
struct Node
{
double value;
Node *N, *P;
Node(double y)
{
value = y;
N = P = NULL;
}
};
class doubleLinkedList
{
Node *front;
Node *back;
public:
doubleLinkedList()
{
front = NULL; back = NULL;
}
~doubleLinkedList() { destroyList(); }
void appendNodeFront(double x);
void appendNodeBack(double x);
void insertNode(...);
void deleteNode(...);
void searchNode(...);
void dispNodesForward();
void dispNodesReverse();
void destroyList();
};
void doubleLinkedList::appendNodeFront(double x)
{
Node *n = new Node(x);
// Needs to be completed….
}
void doubleLinkedList::appendNodeBack(double x)
{
Node *n = new Node(x);
// Needs to be completed….
}
void doubleLinkedList::dispNodesForward()
{
Node *temp = front;
cout << " Nodes in forward order:" << endl;
while (temp != NULL)
{
cout << temp->value << " ";
temp = temp->N;
}
}
void doubleLinkedList::dispNodesReverse()
{
Node *temp = back;
cout << " Nodes in reverse order :" << endl;
while (temp != NULL)
{
cout << temp->value << " ";
temp = temp->P;
}
}
void doubleLinkedList::destroyList()
{
Node *T = back;
while (T != NULL)
{
Node *T2 = T;
T = T->P;
delete T2;
}
front = NULL;
back = NULL;
}
void insertNode(..)
{
// Needs to be completed….
}
void deleteNode(..)
{
// Needs to be completed….
}
Void searchNode(..)
{
// Needs to be completed….
}
#endif
Codes for Main Program to test these functions
#include <iostream>
#include "doubleLinkedList.h"
using namespace std;
int main()
{
doubleLinkedList *list = new doubleLinkedList();
//append nodes to front of the list
for (int i = 1; i < 4; i++)
list->appendNodeFront(i*1.1);
list->dispNodesForward();
list->dispNodesReverse();
//append nodes to back of the list
for (int i = 1; i < 4; i++)
list->appendNodeBack(11.0 - (1.1 * i));
cout << endl << endl;
list->dispNodesForward();
list->dispNodesReverse();
cout << endl << endl;
delete list;
return 0;
}
Explanation / Answer
#ifndef DOUBLELINKEDLIST_H
#define DOUBLELINKEDLIST_H
#include <iostream>
using namespace std;
struct Node
{
double value;
Node *N, *P;
Node(double y)
{
value = y;
N = P = NULL;
}
};
class doubleLinkedList
{
Node *front;
Node *back;
int size;
public:
doubleLinkedList()
{
front = NULL; back = NULL;
size = 0;
}
~doubleLinkedList() { destroyList(); }
void appendNodeFront(double x);
void appendNodeBack(double x);
void insertNode(double x, int index);
void deleteNode(double x);
void deleteNodeAt(int position);
Node* searchNode(double x);
void dispNodesForward();
void dispNodesReverse();
void destroyList();
};
void doubleLinkedList::appendNodeFront(double x)
{
Node *n = new Node(x);
size++;
if (front == NULL) {
front = back = n;
return;
}
front->P = n;
n->N = front;
front = n;
}
void doubleLinkedList::appendNodeBack(double x)
{
Node *n = new Node(x);
size++;
if (back == NULL) {
front = back = n;
return;
}
back->N = n;
n->P = back;
back = n;
}
void doubleLinkedList::dispNodesForward()
{
Node *temp = front;
cout << "Nodes in forward order:" << endl;
while (temp != NULL)
{
cout << temp->value << " ";
temp = temp->N;
}
cout << " ";
}
void doubleLinkedList::dispNodesReverse()
{
Node *temp = back;
cout << "Nodes in reverse order :" << endl;
while (temp != NULL)
{
cout << temp->value << " ";
temp = temp->P;
}
cout << " ";
}
void doubleLinkedList::destroyList()
{
Node *T = back;
while (T != NULL)
{
Node *T2 = T;
T = T->P;
delete T2;
}
front = NULL;
back = NULL;
}
/* Node numbering starts from 0 */
void doubleLinkedList::insertNode(double x, int index)
{
Node *Temp = front;
if (index <= 0 || front == NULL)
{
appendNodeFront(x);
return;
}
if (index >= size)
{
appendNodeBack(x);
return;
}
int i=1;
while (Temp != NULL && i<index) {
Temp = Temp->N;
i++;
}
Node *newNode = new Node(x);
newNode-> N = Temp->N;
if (newNode->N != NULL)
newNode->N->P = newNode;
Temp->N = newNode;
newNode->P = Temp;
size++;
}
void doubleLinkedList::deleteNode(double x)
{
Node * temp = searchNode(x);
if (temp == NULL)
return;
size--;
/* If there is only one node, then delete it and update front and back*/
if (temp == front && front == back)
{
front = back = NULL;
delete temp;
return;
}
/* If the node to be deleted is first, then also update front pointer */
if (temp == front)
{
front = front->N;
if (front != NULL)
front->P = NULL;
delete temp;
return;
}
/* If the node to be deleted is last, then also update back pointer */
if (temp == back)
{
back = back->P;
if (back != NULL)
back->N = NULL;
delete temp;
return;
}
/* If the node is other than frist and last */
if (temp->P != NULL)
temp->P->N = temp->N;
if (temp->N != NULL)
temp->N->P = temp->P;
delete temp;
}
void doubleLinkedList::deleteNodeAt(int position)
{
Node *temp = front;
if (position < 0 || position >= size || temp == NULL)
return;
/* If there is only one node, then delete it and update front and back*/
if (front == back)
{
front = back = NULL;
delete temp;
size--;
return;
}
/* If the node to be deleted is first, then also update front pointer */
if (position == 0)
{
front = front->N;
if (front != NULL)
front->P = NULL;
delete temp;
size--;
return;
}
/* If the node to be deleted is last, then also update back pointer */
if (position == size-1)
{
Node *oldNode = back;
back = back->P;
if (back != NULL)
back->N = NULL;
delete oldNode;
size--;
return;
}
int i=0;
while (temp != NULL && i<position) {
temp = temp->N;
i++;
}
/* If there are >= 3 nodes, and node to be delete is not first or last */
temp->P->N = temp->N;
temp->N->P = temp->P;
size--;
delete temp;
}
Node* doubleLinkedList::searchNode(double x)
{
Node *Temp = front;
int i = 0;
while (Temp != NULL)
{
if (Temp->value == x)
{
cout << "Node found at position " << i <<endl;
return Temp;
}
i++;
Temp = Temp->N;
}
cout << "No node with value "<< x << " has been found in the list " << endl;
return NULL;
}
#endif
-----------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include "doubleLinkedList.h"
using namespace std;
int main()
{
doubleLinkedList *list = new doubleLinkedList();
//append nodes to front of the list
for (int i = 1; i < 4; i++)
list->appendNodeFront(i*1.1);
list->dispNodesForward();
list->dispNodesReverse();
//append nodes to back of the list
for (int i = 1; i < 4; i++)
list->appendNodeBack(11.0 - (1.1 * i));
cout << endl << endl;
list->dispNodesForward();
list->dispNodesReverse();
cout << endl << endl;
// Inserting nodes at the given position
list->insertNode(11.87, 0);
list->dispNodesForward();
list->insertNode(12.98, 7);
list->dispNodesForward();
list->insertNode(19.51, 4);
list->dispNodesForward();
cout << endl << endl;
// Search for a node
list->searchNode(12.98);
list->searchNode(1.5);
cout << endl << endl;
// deleting nodes with values
list->deleteNode(11.87);
list->dispNodesForward();
list->deleteNode(12.98);
list->dispNodesForward();
list->deleteNode(19.51);
list->dispNodesForward();
list->deleteNode(7.51);
cout << endl << endl;
// deleting nodes w.r.t. postion
list->deleteNodeAt(3);
list->dispNodesForward();
list->deleteNodeAt(0);
list->dispNodesForward();
list->deleteNodeAt(3);
list->dispNodesForward();
cout << endl << endl;
delete list;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.