Saturday 14 September 2013

sort array with 0 1 and 2

#include<stdio.h>
#include<conio.h>

typedef struct node node;

struct node
{
       int info;
       node *next;
};

node *head=NULL;

void sort_zero_one_two(node *head)
{
       
}
main()
{
      FILE *f=fopen("hinput.txt","r");
      char c[5];
     
      node *ptr,*temp,*given;
      int count,i=0,flag=1;
     
      while(fscanf(f,"%s",c)!=EOF)
      {
                 ptr=(node*)malloc(sizeof(node));
                 ptr->info=atoi(c);
               
                 if(head==NULL)                
                 {
                                head=ptr;
                                ptr->next=NULL;              
                                temp=head;
                                given=head;
                                i+=1;
                 }
                 else
                 {
                                temp->next=ptr;
                                ptr->next=NULL;
                                temp=temp->next;
                               
                                if(i!=count && flag)
                                {
                                            given=given->next;
                                }
                                else
                                    flag=0;
                                    i+=1;
                 }
      }

     node *ptr0=NULL,*ptr1=NULL,*ptr2=NULL;
     node *head1=NULL,*head2=NULL;
     temp=head;
     while(temp!=NULL)
     {
                if(temp->info==0)
                {
                                 if(ptr0==NULL)
                                 {
                                               head=temp;
                                               ptr0=temp;
                                 }
                                 else
                                 {              ptr0->next=temp;
                                                ptr0=ptr0->next;
                                 }
                                 temp=temp->next;
                                 ptr0->next=NULL;
                               
                }
                else if(temp->info==1)
                {
                                 if(ptr1==NULL)
                                 {
                                               head1=temp;
                                               ptr1=temp;
                                 }
                                 else
                                 {              ptr1->next=temp;
                                                ptr1=ptr1->next;
                                 }
                                 temp=temp->next;
                                 ptr1->next=NULL;
                               
                }
                else
                {
                                 if(ptr2==NULL)
                                 {
                                               head2=temp;
                                               ptr2=temp;
                                 }
                                 else
                                 {              ptr2->next=temp;
                                                ptr2=ptr2->next;
                                 }
                                 temp=temp->next;
                                 ptr2->next=NULL;
                               
                }
     }
     ptr0->next=head1;
     ptr1->next=head2;
      temp=head;
      while(temp!=NULL)
      {
                       printf("\n%d\n",temp->info);
                       temp=temp->next;
      }    
   
      getch();
}

No comments:

Post a Comment

Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.