The prgram will only display one number. In this case 9.

I switched, 9 with 6.71; and only 6.71 displayed. So what this means, only the 2nd to last node's value is displayed.

Note that the display function is able to display the original order of the array passed to the nodes.

Alogrithm is based on insrtion.

I placed a cout statement with a counter iterator to keep track of calls, within the inner while loop, and the following displayed (implying 10 total iterations):

Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 2
Call in inner while loop: 3
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 2
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 0

The cout statement within the if statement, didn't display at all :(

#include <iostream>
using namespace std;

class LinkedList 
{ 
private: 
    struct ListNode 
    { 
        double value;           
        struct ListNode *next;  
    }; 

    ListNode *head;         

public: 
    LinkedList() 
    {   head = NULL;    } 

    ~LinkedList()
    { 
        ListNode * nodePtr; 
        ListNode *nextNode;     

        nodePtr = head; 

        while (nodePtr != NULL) 
        {
            nextNode = nodePtr->next; 

            delete nodePtr; 

            nodePtr = nextNode;
        }
    }

    void append(double num)
    { 
        ListNode *newNode;
        ListNode *nodePtr; 

        newNode = new ListNode; 
        newNode->value = num; 
        newNode->next = NULL;

        if(!head) 
            head = newNode; 

        else 
        { 
            nodePtr = head; 

            while (nodePtr->next) 
                nodePtr = nodePtr->next; 

            nodePtr->next = newNode;
        } 
    } 

    void sortList()
    { 
        ///
        ListNode *i; 
        ListNode *j;
        ListNode *sub;

        i = head;//->next;

        while (i->next != NULL) 
        {
            sub = i;
            j = i->next;

            int count = 0;
            while( j->next != NULL)
            {
                cout << "\nCall in inner while loop: "  << count << endl;//Displays 10 times.
                if ( sub->next > j->next)
                {
                    cout << "\nThis never displayed\n";
                    sub = j;                                        
                }

                j = j->next;
                count++;
            }

            ListNode *temp1, *temp2, *temp3;

            temp1 = i->next;
            i->next = sub->next;
            temp2 = sub->next;
            temp3 = sub->next->next;
            sub->next = temp1;  
            temp2->next = temp1->next;
            temp1->next = temp3;    


            i = i->next;
        }

        delete head;

        head = i;

        ///
    } 

    void display() const
    { 
        ListNode *nodePtr;

        nodePtr = head; 

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


int main()
{
    LinkedList call;

    const int nSIZE = 6;
    double numbers[] = { 4.2, 3.2, 4.1, 6.7, 6.71, 9, 1};

    // send array to linked list
    for (int i = 0; i < nSIZE; i++)
        call.append(numbers[i]);
    call.display();

    cout << "\nSorting...\n";
    call.sortList();
    call.display();

    system("pause");
    return 0;
}   

Since I can't edit my post, I have to post the following.

Correction: nSIZE in line 127 is supposed to = 7, not a 6. So basically the only "sorted number" displayed, is the one in the last index, of the original array created.

This is the output of counter in the nested while loop, with the correction:
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 2
Call in inner while loop: 3
Call in inner while loop: 4
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 2
Call in inner while loop: 3
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 2
Call in inner while loop: 0
Call in inner while loop: 1
Call in inner while loop: 0

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.