Puzzle 3 Answer

28/07/2008 14:12

 

Declared two structure pointers, both should point to Linked List.

Travel the Linked List till the lost node using first pointer

Increment the first pointer by 2 and increment the second pointer by 1.

When the first pointer reaches the end node, the second pointer pointing the middle element of the list.

Now print the data, which is pointed by the second pointer.

void find_mid_element(struct Link_List *p)

{

struct Link_List *first,*second;

first=second=p;

while(first->Next!=NULL)

{

if(first->Next->Next!=NULL)

first=first->Next->Next;

else

first=first->Next;

second=second->Next;

}

printf("\nThe Middle Element = %d\n",second->Data);

}

sahaya.webnode.comĀ© 2008 All rights reserved.

Create a free websiteWebnode