Pages

Saturday, September 17, 2016

Code to sort link list in C++

Hello
       Friends you can use the given function to sort a link list
#include<iostream>
#include<conio.h>
struct node
{
int data;
node *next;
node *prev;

} *temp, *head=NULL,*refnode;

void sort()

  {int size=0;
int tem;
                bool swap;
                 refnode=head;

while(refnode!=NULL)   // it find out size of the link list
  {size++;
  refnode=refnode->next;
  }
 
do      // it sort the given link list
{refnode=head; 
swap = false; //it become true when the lik list sort out
for (int count = 1; count < (size); count++)
{
if ((refnode->data)>(refnode->next->data))
{
tem = refnode->data;
refnode->data = refnode->next->data;
refnode->next->data = tem;
swap = true;

}
refnode=refnode->next;
}
} while (swap)}

No comments:

Post a Comment