Can any one tell me what I am doing wrong. I am sorting a single linked list.

void ll::sort()
{
    for(int i=0;i<size-1;i++)
        {node *cptr = fptr;
            for(int j= 0;j<size-1;j++)
    {
        if((cptr->no) > (cptr->nxt->no))
        {
            if(cptr == fptr)
            { node *c2ptr = cptr->nxt;

                cptr->nxt=c2ptr->nxt;
                c2ptr->nxt=cptr;
                fptr = c2ptr;

            }
            else
            {
                node *c2ptr = cptr->nxt;
                node *tptr = fptr;
                while(tptr->nxt!=cptr)
                {   tptr=tptr->nxt;}

                cptr->nxt=c2ptr->nxt;
                c2ptr->nxt=cptr;
                tptr->nxt=c2ptr;


            }
        }
    cptr=cptr->nxt;
    }

}
}

I would swap the data, not the link pointers. That makes sorting a lot easier. But your teacher may or may not allow that simple method.

You are doing everything wrong according to me.

Sorting is very simple once you understand how its done. Take a pencil and a paper and make a sketch.
you need a *temp to hold on to the checks in the if statement.
example

node::sort(){
node *temp=0;
node *start=head;
while(start !=0){
if(start.data > start->next.data){
   temp =start;
   start = start->next;
   start->next=temp;
   }else{
   start = start->next;
}
}
}

Writing from phone but i guess you got the idea.
Its not a working code. Its just to give you the idea.

Sorry, but that does not sort the linked list. You need to test it out with working code before trying to tell someone else how to do it.

This is not a working code. Sure but this example will sort for sure.
Once ii get home, i will post a working code about this style here.

Just like i promised... This is the sort.

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void bsort(vector<int> &v){  
    int temp,index = v.size();

    while(index > 0){
        if(v[index] > v[index-1]){
            temp= v[index];
            v[index]= v[index-1];
            v[index-1]=temp;
            bsort(v);

            }
            index--;

        }  
    }

int main()
{
vector <int> vec ={10 ,9, 8, 7, 6, 10, 12, 16, 20};
   bsort(vec);
for_each(vec.begin(), vec.end(),[](int c){ cout << c <<endl;});

return 0;
}
// out put
20
16
12
10
10
9
8
7
6

And the output.
You can change the sorting order if you understand the sorting trick.
Help yourself my friend.

Wrong example. That is sorting an array, not a linked list.

I have figured it out was missing one line only.

{
    for(int i=0;i<size-2;i++)
        {node *cptr = fptr;
            while(cptr!=bptr)
    {
        if((cptr->no) > (cptr->nxt->no))
        {
            if(cptr == fptr)
            { node *c2ptr = cptr->nxt;

                cptr->nxt=c2ptr->nxt;
                c2ptr->nxt=cptr;
                fptr = c2ptr;
                cptr=c2ptr;
            }
            else
            {
                node *c2ptr = cptr->nxt;
                node *tptr = fptr;
                while(tptr->nxt!=cptr)
                {   tptr=tptr->nxt;}

                cptr->nxt=c2ptr->nxt;
                c2ptr->nxt=cptr;
                tptr->nxt=c2ptr;
                cptr=c2ptr;

            }
        }
    cptr=cptr->nxt;
    }

}
}

What i provided was a simple style or sorting a data.
If that algorithm can be used to sort linklist without a problem. I as on about sorting style. This is also applicable with link list.

I assume you are not doubting that are you ?
I know you are not but trying to be witty.
Good one. ))

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.