Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

3. Sort a linked list

3. Sort a linked list

//sorting in descending order
 struct node
{
 int value; node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
node* first,second,temp;
 first= Head; while(first!=null)
 second=first->NEXT; while(second!=null)
{
 if(first->value < second->value)
temp = new node();
 temp->value=first->value;
 first->value=second->value; 
second->value=temp->value;
 delete temp;
}
                            second=second->NEXT;
}
first=first->NEXT;
}
}


No comments:

Post a Comment