Thursday, 28 May 2015

Sorting of nodes in Linked List(Insertion sort)

#include<stdio.h>
#include<stdlib.h>
struct nod
{
   int data;
   struct nod *llink,*rlink;
};
typedef struct nod *node;
main()
{
   int i,size,count=0,temp1;
   node head,temp,p,q;
   printf("\n enter size\n");
   scanf("%d",&size);
   for(i=0;i<size;i++)
   {
       temp=(node)malloc(sizeof(node));
       printf("\n enter element\n");
       scanf("%d",&temp->data);
       if(!i)
        {
           temp->llink=NULL;
           temp->rlink=NULL;
           head=temp;
        }
        else
        {
           head->rlink=temp;
           temp->llink=head;
           temp->rlink=NULL;
           head=temp;
        }
   }
   while(temp->llink!=NULL)
      temp=temp->llink;
    head=temp;
     printf("\n details entered are\n");
    while(temp!=NULL)
    {
      printf("%d\n",temp->data);
      temp=temp->rlink;
     }
     p=head;
     q=head;
     while(--size)
     {
         count++;
         temp=p->rlink;
          q=p;
          for(i=count;i>0;i--)
          {
             
           if(temp->data<q->data)
           {
               temp1=temp->data;
                temp->data=q->data;
               q->data=temp1;
           }
           q=q->llink;
           temp=temp->llink;
         }
         p=p->rlink;
     }
     printf("\n after sorting\n");
     p=head;
     while(p!=NULL)
     {
       printf("%d\n",p->data);
       p=p->rlink;
     }
      
}



No comments:

Post a Comment