1
void sortlist()
{
    struct node *a;
    struct node *temp=head;

    struct node *temp1=head->next;

    while(temp!=NULL)
    {
        while(temp1->next!=NULL)
        {
            if(temp->data > temp1->data)
            {
                a->data=temp->data;
                temp->data=temp1->data;
                temp1->data=a->data;
            }
            else
            {
                temp1=temp1->next;
            }
        }
        temp=temp->next;
    }
}

//I am new to data structures.i am encountering some problem here while trying to sort elements of linked list.list does not get sorted.any help is greatly appreciated.

3 Answers 3

4

a is an uninitialised pointer so the line

a->data=temp->data;

invokes undefined behaviour. Crashing is the most likely result here as you'll try to write to memory at an undefined address that may not be writeable by your code or may be in use by another part of your program.

You could fix this by giving a the same type as temp->data instead.

void sortlist()
{
    int a; // change type to match node->data if required
    ...
            if(temp->data > temp1->data)
            {
                a=temp->data;
                temp->data=temp1->data;
                temp1->data=a
            }
    ...
}

EDIT: There is also a potential NULL dereference crash in the line while(temp1->next!=NULL). The code below shows a potential implementation of sortlist which avoids this.

void sortlist()
{
    struct node *ptr=head;
    while(ptr!=NULL) {
        struct node *next;
        if (ptr == NULL)
            return;
        next = ptr->next;
        while(next!=NULL) {
            if(ptr->data > next->data) {
                int a=ptr->data;
                ptr->data=next->data;
                next->data=a;
            }
            next = next->next;
        }
        ptr=ptr->next;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Or just make a local struct so you don't even have to deal with types.
@simonc Thanks buddy,The crash of program got solved.but still elements does not get sorted properly
@sujai Can you give an example of a list you have tried to sort? What was the result of calling sortlist? Was there no change in the list or did it sort the items incorrectly?
@sujai If the problem was that sortlist crashes for single-element lists, I've posted an updated answer that should fix this.
ther was an error on the above code , i.e , you have given next = ptr->next; code got struck when i ran this code. instead used next=next->next and it works fine ; but anyway thanks for your support buddy .
0

I made changes in the code and add the comments for the changes

void sortlist()
{

    // struct node *a; /* this is not required. */

    /* NULL checks are required,if head is NULL,head->next will crash.*/
    if(head == NULL || head->next == NULL) return;

    struct node *temp=head;

    struct node *temp1=head->next;

    while(temp!=NULL)
    {
        while(temp1->next!=NULL)
       {

           if(temp->data > temp1->data)
           {
                /* assuming you data is integer*/
                int d=temp->data;
                temp->data=temp1->data;
                temp1->data=d;
           }
           //else /* else is not required, better to remove it.*/
           //{
                temp1=temp1->next;
           //}
       }
    temp=temp->next;
    }
}

1 Comment

@ RadhaKrishna thanks,but the elements does not get sorted properly
0

//at last i found answer to my own problem and this is the solution,thanks for your help buddies

void sortlist()
{
    struct node *temp=head;
    int s;
    struct node *temp1=temp->next;

    while(temp!=NULL)
    {
        temp1=temp->next;                       
        while(temp1!=NULL)
        {
            if(temp->data > temp1->data)
            {
                s=temp->data;
                temp->data=temp1->data;
                temp1->data=s;
            }
            temp1=temp1->next;
        }
        temp=temp->next;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.