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

Using this setup, create a doubly linked list that allows for inserting, removin

ID: 3912339 • Letter: U

Question

Using this setup, create a doubly linked list that allows for inserting, removing, and outputting data at a certain index #include // Provides sizet and NULL class dnode public: - /I TYPEDEF typedef double value_type; /CONSTRUCTOR dnode( const value_type& init_data value_type(, dnode* init-fore = NULL, dnode* initback = NULL - data field init data; link-fore = init-fore; link_back-init_back; // Member functions to set the data and link fields: void set data(const value type& new_data) data field new_data; h void set fore(dnodex new_fore) void set back (dnodex new_back) { link-fore= new-fore; } f link back new_back; // Const member function to retrieve the current data: value_type data() constt return data field; h // Two slightly different member functions to retrieve each current link: const dnodex fore( const t return link fore; h dnodex fore) const dnode* back const return link back; ) dnodex back) return link fore; t return link back; h private: value_type data field; dnode xlink_fore dnode *link back; b:

Explanation / Answer

#include <iostream>

#include <cstdlib>

using namespace std;

// Provides size_t and NULL

class dnode
{
public:  
// TYPEDEF
typedef double value_type;

// CONSTRUCTOR
dnode(
const value_type& init_data = value_type(),
dnode *init_fore = NULL,
dnode *init_back = NULL
)
{
data_field = init_data;
link_fore = init_fore;
link_back = init_back;
}
// Member functions to set the data and link fields:
void set_data(const value_type& new_data) { data_field = new_data; }
void set_fore(dnode* new_fore) { link_fore = new_fore;}
void set_back(dnode* new_back) { link_back = new_back;}

// Const member function to retrieve the current data:
value_type data() const { return data_field; }
// Two slightly different member functions to retrieve
const dnode* fore( ) const { return link_fore; }
dnode* fore( ) { return link_fore; }
const dnode* back( ) const { return link_back; }
dnode* back( ) { return link_back; }

private:
value_type data_field;
dnode *link_fore;
dnode *link_back;
};

void displayNode(dnode *start, int index)
{
dnode * temp1 = start;
//traverse the list till the required position and display the node's data
for(int i = 0; i < index-1; i++)
temp1 = temp1-> fore();
if(temp1 != NULL)
cout<<"Data at position "<<index<<" is: "<<temp1->data()<<" ";   
else
cout<<"Given position is out of bound ";
}

dnode* removeNode(dnode *start, int index)
{
dnode * temp1 = start;
for(int i = 0; i < index-1; i++)
temp1 = temp1-> fore();
  
if (start == NULL || temp1 == NULL)
{
cout<<"Cann't delete as index is out of bound ";
return start;
}
/* If node to be deleted is head node */
if (start == temp1)
start = temp1->fore();

/* Change next only if node to be deleted is NOT
the last node */
if (temp1->fore() != NULL)
temp1->fore()->set_back(temp1->back());

/* Change prev only if node to be deleted is NOT
the first node */
if (temp1->back() != NULL)
temp1->back()->set_fore(temp1->fore());   

return start;
}

dnode* insert(dnode *start , dnode *newNode, int index)
{
//If the list is empty
if(start == NULL)
{
if(index == 1)
start = newNode;
else
cout<<"Cann't insert at said position which not a first position as list is empty ";
}
/*If the node to be insered is at first position, make the start node's back as new node and
new node's front as start and the new node as start node of list */
else if(index ==1)
{
newNode->set_fore(start);
start->set_back(newNode);   
start = newNode;
}
/*If it is not first position */
else
{
dnode *temp1 = start;
//traverse through the list till the position one before in the list
for(int i = 0; i < index-2; i++)
temp1 = temp1-> fore();
dnode *temp2;   
  
newNode->set_fore(temp1 ->fore()); //set current node's front as new node's front
temp1->set_fore(newNode); //set new node as current node's front
temp2 = newNode->fore();
newNode->set_back(temp1); //set current node as new node's back
/* If the new node is not inserted as last node, then set new node
as back of new node's front node */
if(temp2 != NULL)
temp2->set_back(newNode);
}   
return start;
}

dnode *start=NULL;

int main()
{
//New nodes inserted to dll
dnode *newNode = new dnode(10.5);
start = insert(start,newNode,1);
dnode *newNode1 = new dnode(11.5);
start = insert(start,newNode1,1);
dnode *newNode2 = new dnode(15);
start = insert(start,newNode2,3);
dnode *newNode3 = new dnode(17);
start = insert(start,newNode3,3);
dnode *newNode4 = new dnode(20);
start = insert(start,newNode4,2);
cout<<"AFter Insertions ";
  
//function call to display the data at given position in list
displayNode(start,5);

//function call to remove the node at given position in list
start = removeNode(start,5);
  
cout<<"After removal ";
displayNode(start,5);
  
return 0;
}

Output:

AFter Insertions                                                                                                               

Data at position 3 is: 10.5                                                                                                    

After removal                                                                                                                  

Data at position 3 is: 17