because you are doing odd=odd->next; and even=even->next;
you will lose head pointers for the two linked list formed once you come
out of the loop.
void segregate(node* head)
{
node *temp,*odd,*even;
toggle=1;
if(head==NULL)
{
return;
}
odd=head;
if(head->next==NULL)
{
return;
}
even=head->next;
if(even->next==NULL)
{
return;
}
else
{
temp=even->next;
}
node *otemp,*etemp;
otemp=odd;
etemp=even;
while(temp!=NULL)
{
if( toggle == 1)
{
otemp->next=temp;
otemp=otemp->next;
temp=temp->next;
otemp->next=NULL;
toggle=0;
}
else
{
etemp->next=temp;
etemp=etemp->next;
temp=temp->next;
etemp->next=NULL;
toggle=1;
}
}
}
On Sat, Dec 24, 2011 at 10:56 PM, Karthikeyan V.B <[email protected]>wrote:
> Segregate even and odd psoitioned nodes in a linked list
>
> Eg:
> 2->3->1->9->7->5
> The output should be two separate lists
> 2->1->7
> 3->9->5
>
> *My code is:*
> void segregate(node* head)
> {
> int i=1;
> node* sec=head->next;
> node *odd=head,*even=head->next;
> while(even)
> {
> if(i%2)
> {
> odd->next=even->next;
> even->next=NULL;
> odd=odd->next;
> if(!odd->next) break;
> }
> else
> {
> even->next=odd->next;
> odd->next=NULL;
> even=even->next;
> if(!even->next) break;
> }
> i++;
> }
> }
>
> Pls correct me if i'm wrong or suggest me a better approach
>
> Regards,
> KARTHIKEYAN.V.B
> PSGTECH
> CBE
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.