refer stanford page
1,2,3,4,5,6
will become
5,3,1
6,4,2
void MoveNode(struct node** destRef, struct node** sourceRef) {
struct node* newNode = *sourceRef; // the front source node
assert(newNode != NULL);
*sourceRef = newNode->next; // Advance the source pointer
newNode->next = *destRef; // Link the old dest off the new node
*destRef = newNode; // Move dest to point to the new node
}
void AlternatingSplit(struct node* source,
struct node** aRef, struct node** bRef) {
struct node* a = NULL; // Split the nodes to these 'a' and 'b' lists
struct node* b = NULL;
struct node* current = source;
while (current != NULL) {
MoveNode(&a, ¤t); // Move a node to 'a'
if (current != NULL) {
MoveNode(&b, ¤t); // Move a node to 'b'
}
}
*aRef = a;
*bRef = b;
}
Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652
On Sat, Dec 24, 2011 at 11:37 PM, atul anand <[email protected]>wrote:
> 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.
>
--
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.