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

there are 5 nodes of data values given for the program 15 , 45 , 10, 35 , 100 an

ID: 3640298 • Letter: T

Question

there are 5 nodes of data values given for the program 15 , 45 , 10, 35 , 100 and we are suppose to enter a node of data value 20 between 10 and 35, i guess the problem is with the output line in the end ......

#include<iostream>
using namespace std;
struct list
{
double value;
list *next;
list(double value1, list *next1=NULL)
{
value=value1;
next=next1;

}
};
int main()
{
list *head=NULL;
int N;
double number;

cout<<"please enter the length of the linked list :"<<endl;
cin>>N;
cout<<"please store data in "<<N<<" nodes "<<endl;

for(int i=0;i<N;i++)
{
cin>>number;
head = new list(number,head);
}
cout<<endl<<"The contents of the list are: "<<endl;
list *ptr=head;
while(ptr !=NULL)
{
cout <<ptr->value<< " ";
ptr=ptr->next;
}
//-------------------------------------------------------------------------
list *nodePtr, *previousNodePtr;
double number1;

cout<<endl;
cout<<endl<<"Enter the data for the new node: "<<endl;
cin>>number1;
if (head == NULL || head->value == 10)
{
head = new list(number1, head);
}
else
{
previousNodePtr = head;
nodePtr = head->next;

// Find the insertion point.
while (nodePtr != NULL && nodePtr->value ==35 )
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
// Insert the new node just before nodePtr.
previousNodePtr->next = new list(number1, nodePtr);
cout<<endl<<"The contents of the list after insertion are: "<<endl;

while(nodePtr !=NULL)
{
cout <<nodePtr->value<< " ";
nodePtr=nodePtr->next;
}
}
system("pause");
}

Explanation / Answer

You forget to set set nodePtr to head to display the full list.

#include<iostream>
using namespace std;

struct list
{
   double value;
   list *next;
   list(double value1, list *next1 = NULL)
   {
      value = value1;
      next = next1;
   }
};


int main()
{
   list *head = NULL;
   int N;
   double number;

   cout << "please enter the length of the linked list :" << endl;
   cin >> N;
   cout << "please store data in " << N << " nodes " << endl;

   for (int i = 0; i < N; i++)
   {
      cin >> number;
      head = new list(number, head);
   }
   cout << endl << "The contents of the list are: " << endl;
   list *ptr = head;
   while (ptr != NULL)
   {
      cout << ptr->value << " ";
      ptr = ptr->next;
   }
   //-------------------------------------------------------------------------
   list *nodePtr, *previousNodePtr;
   double number1;

   cout << endl;
   cout << endl << "Enter the data for the new node: " << endl;
   cin >> number1;
   if (head == NULL || head->value == 10)
   {
      head = new list(number1, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;

      // Find the insertion point.
      while (nodePtr != NULL && nodePtr->value == 35)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
      // Insert the new node just before nodePtr.
      previousNodePtr->next = new list(number1, nodePtr);
   }

   //Bring these part out of the if else statement
   cout << endl << "The contents of the list after insertion are: " << endl;
   nodePtr = head; //**you must first set nodePtr to head to display the full list
   while(nodePtr != NULL)
   {
      cout << nodePtr->value << " ";
      nodePtr = nodePtr->next;
   }


   //Clean up the list before end program
   list *nextNodePtr;
   nodePtr = head;
   while (nodePtr != NULL)
   {
      nextNodePtr = nodePtr->next;
      delete nodePtr;
      nodePtr = nextNodePtr;
   }
   head = NULL;


   cin.sync();
   cin.get();
   return 0;
}