Saturday, 9 July 2011

Sorting the Linked List.

struct node
{
int data;
struct node* link;
};

Sort( struct node *Head)
{
  struct node* first,second,temp;
  first= Head;
  while(first!=null)
  {
    second=first->link;
    while(second!=null)
    {
      if(first->data < second->data) // sorting in descending order.
      {
        temp = new node();
        temp->data =first->data;
        first->data =second->data;
        second->data =temp->data;
        delete temp;
      }
      second=second->link;
    }
   first=first->link;
  }
}

No comments:

Post a Comment