The linked list assignment goes as follows. First Grab the Files From Blackboard
ID: 3582089 • Letter: T
Question
The linked list assignment goes as follows. First Grab the Files From Blackboard related to this assignment (also attached to the lecture, if you were in class we probably completed some of this).
If you don't follow these specifications exactly you will get a 0 (i.e. you could do this without using the node class correctly etc. That is definitely not the point of this assignment.).
1) You will complete the INLINE implementation of node.h (there will be no node.cpp implementation file. All the node implementations should be pretty trivial. The constructor is already completed for you if you need help inlining).
2) You will then create 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).
3) You will 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.
The attached output will give you a clue as to how to achieve this. Good luck!
//My code
//testNode.h
// FILE: node.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include // Provides size_t and NULL
class node
{
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR (already implemented for you)
node(const value_type& init_data = value_type(), node* init_link = NULL)
{
data_field = init_data; link_field = init_link;
}
//IMPLEMENT THE FOLLOWING MEMBER FUNCTIONS
// 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 this;
}
node* link()
{
return this;
}
private:
value_type data_field;
node* link_field;
};
#endif
//testMain
#include
#include "testNode.h"
using namespace std;
void printLinkedList(node* headPtr);
int main()
{
int userInput = 0;
//node *headPtr = new node(headPtr->data());
cout << "input a positive number to insert into the linked list or a negative number to quit" << endl;
cin >> userInput;
while (userInput >= 0)
{
//two node pointers to get you started
//node *headPtr = new node(userInput);
node *temp = new node(userInput);
node *currentPtr = temp;
printLinkedList;
cout << "your current list is: head->" << currentPtr->data() << "->" << endl;
cin >> userInput;
}
int x = 0;
cin >> x;
}
void printLinkedList(node* headPtr)
{
node* person = headPtr;
while (person->link() != NULL)
{
//cout << person;
cout << person->data();
person = person->link();
}
//person = person->previous;
//delete person->link();
//person->link() =Null;
//last line of your pirnt function
cout << "NULL" << endl;
}
Example of the required output
I am having trouble doing this. Please help this assignment is due tommorow
Explanation / Answer
//header file testNode.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include <cstddef>// Provides size_t and NULL
class node
{
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR (already implemented for you)
node(const value_type& init_data = value_type(), node* init_link = NULL)
{
data_field = init_data; link_field = init_link;
}
//IMPLEMENT THE FOLLOWING MEMBER FUNCTIONS
// 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;
}
//declare method to get next link,Added by chegg EA
node *next_link(node *ptr)
{
if (ptr != NULL)
ptr = ptr->link_field;
else
return NULL;
return ptr;
}
//check link is NULL or not , Added by chegg EA
bool check_link(node *ptr)
{
if (ptr != NULL)
{
if (ptr->link_field != NULL)
return true;
else
{
return false;
}
}
else
return false;
}
// 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 this;
}
node* link()
{
return this;
}
private:
value_type data_field;
node* link_field;
};
#endif
---------------------------------------------------------------------
//main
#include "testNode.h"
#include<iostream>
using namespace std;
void printLinkedList(node* headPtr);
node *headPtr = NULL;
int main()
{
int userInput = 0;
//node *headPtr = new node(headPtr->data());
cout << "input a positive number to insert into the linked list or a negative number to quit" << endl;
cin >> userInput;
while (userInput >= 0)
{
//two node pointers to get you started
//node *headPtr = new node(userInput);
node *temp = new node(userInput);
temp->set_link(NULL);
node *currentPtr ;
if (headPtr == NULL)
{
headPtr = temp;
currentPtr = headPtr;
}
else
{
currentPtr = headPtr;
//go through each node till we get current->netx not NULL
while (currentPtr->check_link(currentPtr) != NULL)
{
currentPtr = currentPtr->next_link(currentPtr);
}
currentPtr->set_link(temp);
}
//printLinkedList(headPtr);
//cout << "your current list is: head->" << currentPtr->data() << "->" << endl;
cout << "your current list is: head";
printLinkedList(headPtr);
cin >> userInput;
}
int x = 0;
cin >> x;
}
void printLinkedList(node* headPtr)
{
node* person = headPtr;
/*while (person->link() != NULL)
{
//cout << person;
cout << person->data();
person = person->link();
}*/
//added by checg EA
while (person->link() != NULL)
{
//cout << person;
cout << "->"<<person->data();
person = person->next_link(person);
}
//person = person->previous;
//delete person->link();
//person->link() =Null;
//last line of your pirnt function
cout << "->NULL " << endl;
}
-------------------------------------------------------------------------------------
output
input a positive number to insert into the linked list or a negative number to q
uit
2
your current list is: head->2->NULL
44
your current list is: head->2->44->NULL
33
your current list is: head->2->44->33->NULL
55
your current list is: head->2->44->33->55->NULL
66
your current list is: head->2->44->33->55->66->NULL
-1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.