void SortedNumberList::add(double number)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL || head->value >= number)
   {
      head = new ListNode(number, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;

      
      while (nodePtr != NULL && nodePtr->value < number)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
     
      previousNodePtr->next = new ListNode(number, nodePtr);
   }
}

int main()
{
   SortedNumberList list;

   // Add elements in order
   list.add(2.5);
   list.add(7.9);
   list.add(12.6);
   // Add a value that should go in the middle of the list
   list.add(10.5);
   // Display the list.
   list.displayList();
   cout << endl;
   return 0;
}

When it goes through the first time I can see that a new node is made since head is NULL, but what I don't get is the 2nd and 3rd time around. The if statement says that if head is NULL or head->value >= number a new node would be created, but wouldn't it skip that part for the 2nd time around since head is now pointing to a node and head->value is less than number (2.5>=7.9)?

The output is supposed to be
2.5 7.9 10.5 12.6

Please send enough code .............I mean to say send class as well
For better help.
In this code every thing is correct or may i cant able to find out so please send full source or enough to get help

Please send enough code .............I mean to say send class as well
For better help.
In this code every thing is correct or may i cant able to find out so please send full source or enough to get help

#include <iostream>
using namespace std;
class NumberList
{
protected:
    // Declare a class for the list node.
    struct ListNode
    {
       double value;
       ListNode *next;
       ListNode(double value1, ListNode *next1 = NULL)
       {
          value = value1;
          next = next1;
       }      
    };
    ListNode *head;                   // List head pointer
public:
    NumberList() { head = NULL;  }    // Constructor
    ~NumberList();                    // Destructor
    void add(double number);
    void remove(double number);
    void displayList();
};


#include "NumberList.h"
using namespace std;

void NumberList::add(double number)
{
   if (head == NULL)
      head = new ListNode(number);
   else
     {
       ListNode *nodePtr = head;
       while (nodePtr->next != NULL)
          nodePtr = nodePtr->next;

       nodePtr->next = new ListNode(number);
     }
}

void NumberList::displayList()
{
   ListNode *nodePtr = head;   
   while (nodePtr)
   {
      cout << nodePtr->value << "    ";
      nodePtr = nodePtr->next;
   }
}

NumberList::~NumberList()
{
  ListNode *nodePtr = head;
  while (nodePtr != NULL)
  {
      ListNode *garbage = nodePtr;
      nodePtr = nodePtr->next;
      delete garbage;
  }
}

#include "NumberList.h"
class SortedNumberList : public NumberList
{
public:
    void add(double number);   
};


void SortedNumberList::add(double number)
{
   ListNode *nodePtr, *previousNodePtr;

   if (head == NULL || head->value >= number)
   {
      head = new ListNode(number, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;

      
      while (nodePtr != NULL && nodePtr->value < number)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
     
      previousNodePtr->next = new ListNode(number, nodePtr);
   }
}

int main()
{
   SortedNumberList list;

   // Add elements in order
   list.add(2.5);
   list.add(7.9);
   list.add(12.6);
   // Add a value that should go in the middle of the list
   list.add(10.5);
   // Display the list.
   list.displayList();
   cout << endl;
   return 0;
}
void SortedNumberList::add(double number)
{
   ListNode *nodePtr, *previousNodePtr;
 
   if (head == NULL || head->value >= number)
   {
      head = new ListNode(number, head);
   }
   else
   {
      previousNodePtr = head;
      nodePtr = head->next;
 
 
      while (nodePtr != NULL && nodePtr->value < number)
      {
         previousNodePtr = nodePtr;
         nodePtr = nodePtr->next;
      }
 
      previousNodePtr->next = new ListNode(number, nodePtr);
   }
}

You have to re think on

if (head == NULL || head->value >= number)

this line of code problem is that head is always pointing to first node 2.5 in your case .............I hope now you understand whats the problem ...........
feel free to ask any more query.............Best of luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.