Question: C++ Linked List I need help implementing the required function in the node.cpp and linkedlist.cpp file so that to make the doubly linked list works.

C++ Linked List

I need help implementing the required function in the node.cpp and linkedlist.cpp file so that to make the doubly linked list works. (All the instructions have been commented in the code below)

//node.h

class node { private: node* next; int data; public: node(); node(int input); node(int input[], int length); ~node(); void append(int value); void append(int values[], int length); node* insert(int location, int value); node* remove(int location); void print(); void print_middle(); int get_value(int location); void set_data(int location, int value); }; 

--------------------------------------------------------------------------------------------------------------------

//node.cpp

#include "node.h" // Take in value and create a node node::node(int input) { } // Takes in an array of values and creates the appropriate nodes node::node(int values[], int length) { } // Default destructor node::~node() { // Hint: You don't want to just delete the current node. You need to keep track of what is next } // Add a value to the end node void node::append(int input) { } // Add an array of values to the end as separate nodes void node::append(int inputs[], int length) { } // Insert a new node after the given location node* node::insert(int location, int value) { // Must return head pointer location } // Remove a node and link the next node to the previous node node* node::remove(int location) { // Must return head pointer location } // Print all nodes void node::print() { } //Print the middle node void node::print_middle() { // HINT: Use a runner to traverse through the linked list at two different rates, 1 node per step // and two nodes per step. When the faster one reaches the end, the slow one should be // pointing to the middle } // Get the value of a given node int node::get_value(int location) { } // Overwrite the value of a given node void node::set_data(int location, int value) { } 

--------------------------------------------------------------------------------------------------------------------

//linked_list.h

#include "node.h" class linked_list { private: node* head; public: linked_list(); linked_list(int value); linked_list(int values[], int length); ~linked_list(); bool append(int value); bool append(int values[], int length); void insert(int location, int value); void remove(int location); void print(); void print_middle(); int get_value(int location); void set_data(int location, int value); }; 

--------------------------------------------------------------------------------------------------------------------

//linked_list.cpp

#include "linked_list.h" #include // Default constructor for creating a linked list with nothing in it linked_list::linked_list() { head = nullptr; } // Default constructor for creating a linked list with a given value linked_list::linked_list(int value) { head= new node(value); } // Default constructor for creating a linked list from a given integer array linked_list::linked_list(int values[], int length) { head= new node(values,length); } // Default destructor. Should run through each of the nodes and delete them linked_list::~linked_list() { delete head; } // Add a single value to a linked list. Adds to the end of the linked_list bool linked_list::append(int value) { if(head== nullptr) { head = new node(value); } else { head->append(value); } } // Add an array of values to the end of the bool linked_list::append(int values[], int length) { if(head== nullptr) { head = new node(values, length); } else { head->append(values, length); } } // Add a a single value after the given location void linked_list::insert(int location, int value) { if(head== nullptr) { return; } else { head= head->insert(location, value); } } // Remove a node at the given node and link the previous node to the next node void linked_list::remove(int location) { if(head== nullptr) { return; } else { head= head->remove(location); } } // Print the entire linked list // void linked_list::print() { head->print(); } // Print the middle node of the linked list void linked_list::print_middle() { head->print_middle(); } // Get the data at the given location int linked_list::get_value(int location) { return head->get_value(location); } // Overwrite the data at the given location void linked_list::set_data(int location, int value) { head->set_data(location, value); } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!