Hello
I have to work with Linkedlist, and sort them from smallest to the largest.
So I have made this sorting function. It works but I dont understand why it is not displaying me the items I have inserted after sorting it. I have also looked at the other related posts, but its not helping me.

Here is my code:

#include<iostream>
#include<conio.h>
#include <algorithm>

using namespace std;
struct link {
    int info;
    link* next;
};
class linklist 
{
private:
link* first;
int count;

public:
    linklist(){

        first =NULL;
        count =0;
    }
    void additem(int d) 
{ 
    link* newlink = new link ; 
    newlink -> info = d ; 
    newlink -> next = NULL ; 
    if ( first == NULL ) 
        first = newlink ; 
    else { 
        link* current = first ; 
        while ( current -> next != NULL ) 
            current = current -> next ; 
        current -> next = newlink ; 
    }
}

    void display() {

    link* current = first ; 
    cout<<"The list of elements in the list are : ";
    while ( current !=  NULL) 
    { 
        cout<< endl<<current -> info  ; 
        current = current -> next ;  
    }
    cout<<endl;
} 

    void sortSmall(){
    link* ptr1;
    ptr1 = first;

    link* ptr2;
    ptr2 = ptr1 -> next;
    int temp;

    link* small = new link();

    while ( ptr2 != NULL) {
        {
            if (ptr1 -> info < ptr2 -> info)
            small -> info = ptr1 -> info;
        else
        temp = ptr1 -> info;
        ptr1 -> info = ptr2 -> info;
        ptr2 -> info = temp;
        }

    ptr1 -> next;
    ptr2 = ptr1 -> next;

    }
    void display();
    }

    ~linklist(){
        link *current;
        link *temp;

        current=first;
        temp=first;

        while (current != NULL){
            temp = temp -> next;
        delete current;
        current = temp;
        }
    }
    };


    int main(){

    linklist object;
    object.additem (12);
    object.additem (10);
    object.additem (8);
    object.additem (7);
    object.display();

    object.sortSmall();
    object.display();


    getch();
    }

>It works but I dont understand why it is not displaying me the items I have inserted after sorting it.
Um, if the output isn't as expected then it clearly doesn't work. Looking at your sorting algorithm, aside from it being obviously wrong to a trained eye, temp is an uninitialized variable that's going to be used in half the calls on average. There's also an infinite loop, which I suspect is why you're not seeing the sorted items display. ;)

To fix the infinite loop, the final update should be:

ptr1 = ptr1 -> next;
ptr2 = ptr1 -> next;

instead of:

ptr1 -> next;
ptr2 = ptr1 -> next;

But your algorithm is still not going to sort correctly. Might I suggest reading this tutorial to get a better handle on the concept of sorting? The simplest linked list sort starts here.

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.