need help completing the INLINE implementation of node.h. The constructor is alr
ID: 3720009 • Letter: N
Question
need help completing the INLINE implementation of node.h. The constructor is already completed. i attached the files https://drive.google.com/drive/u/0/folders/1SIRbDeqLWhxpyYzXRXDQxrFsycz8shxF
2) need help creating a main file, that includes node.h that will allow a user to input as many positive numbers as they want and add it to a Linked List created in main (that utilizes your node file), we will stop insert when the user enters a negative number. Think of what type of loop you will need for this!
3) tthen create a print function that takes in the head pointer of a linked list and prints out the whole linked list from beginning to end to the console.
It will print the list out in the format
"your current list is: head->10->20->30->NULL"
(if the user has added nodes of 10, 20, and 30 respectively).
The last thing this function couts should be an ENDL.
output should look like this when input lets say
2 44 33 55 66 -3
input a positive number to insert into the linked list or a negative number to quit
your current list is: head->2->NULL
your current list is: head->2->44->NULL y
our current list is: head->2->44->33->NULL
your current list is: head->2->44->33->55->NULL
your current list is: head->2->44->33->55->66->NULL
Explanation / Answer
/////////////////////////////////////node.h///////////////////
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <cstdlib> // Provides size_t and NULL
class node
{
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR
node(const value_type& init_data = value_type(),node* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}
// Member functions to set the data and link fields.
void set_data(const value_type& new_data)
{
data_field = new_data;
}
void set_link(node* new_link)
{
link_field = new_link;
}
// Constant member function to retrieve the current data:
value_type data() const
{
return data_field;
}
// Two slightly different member functions to retreive
// the current link. (they have the same implementation)
//they take care of whether it's called from a const function
//or a non-const function
const node* link() const
{
return link_field;
}
node* link()
{
return link_field;
}
private:
value_type data_field;
node* link_field;
};
#endif
////////////////////////////////////////main.cpp ////////////////////////////
#include <iostream>
#include "node.h"
using namespace std;
void printLinkedList(node* headPtr);
int main()
{
//two node pointers
node *headPtr=NULL; //maintains start of link list
node *currentPtr=NULL; //maintain last node of link list
int userInput=0;
cout<<"input a positive number to insert into the linked list or a negative number to quit"<<endl;
while(1)
{
cin >> userInput; //get user input
if (userInput < 0) //exit in case of negative input value
break;
//create a new node to add in list
node *listItem = new node(userInput);
//insert node in link list
if (headPtr == NULL) //if first node of link list intialise linklist to first node
{
headPtr = listItem;
currentPtr = headPtr;
}
else //if not first node add new node at the end of link list
{
currentPtr->set_link(listItem);
currentPtr = listItem;
}
printLinkedList(headPtr);
}
return 0;
}
void printLinkedList(node* headPtr)
{
cout << "head->" ;
while (headPtr)
{
cout << headPtr->data() << "->";
headPtr = headPtr->link();
}
//last line of print function
cout<<"NULL"<<endl;
}
//////////////////////////OUTPUT///////////////////////////
hax-vikashar-1$ ./a.out
input a positive number to insert into the linked list or a negative number to quit
2
head->2->NULL
44
head->2->44->NULL
33
head->2->44->33->NULL
55
head->2->44->33->55->NULL
66
head->2->44->33->55->66->NULL
-3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.