pgm to find middle element of linklist(in efficent manner)

Answers were Sorted based on User's Feedback



pgm to find middle element of linklist(in efficent manner)..

Answer / abdur rab

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

int mid_element ( struct node* _node )
{
struct node* cur_ptr;
struct node* cur_next_ptr;

if ( NULL == _node ) return ( -1 );
else {
cur_ptr = _node;
cur_next_ptr = _node;
while ( ( NULL != cur_ptr -> next )
&& ( NULL != cur_next_ptr -
> next )
&& ( NULL != cur_next_ptr -
> next -> next ) )
{
cur_ptr = cur_ptr -> next;
cur_next_ptr = cur_next_ptr ->
next -> next;
}
}

return ( cur_ptr -> data );
}

Is This Answer Correct ?    4 Yes 0 No

pgm to find middle element of linklist(in efficent manner)..

Answer / sharan

NODE display_middle(NODE first)
{
int count = 0;
NODE temp,mid;

for ( temp = mid = first, count=0; temp ; temp = temp ->
link,count++)

{
if ( count % 2 )
{
mid = mid -> link;
}
}
return mid;
}

Is This Answer Correct ?    6 Yes 2 No

pgm to find middle element of linklist(in efficent manner)..

Answer / ashwini

struct node
{
int data;
struct node *ptr;
};


struct node mid_element(struct node* head)//since we pass addr
{
int count=0,n_count,i=0;
struct node* temp,*mid;
temp=mid=head;
while(temp -> ptr != NULL)
{
count++;
temp = temp->otr;
}
count++;

if(count % 2)
{
n_count = (count/2)+1;
for(i=0 ; i<n_count ; i++)
mid = mid -> ptr;
}

return mid;
}

Is This Answer Correct ?    2 Yes 0 No

pgm to find middle element of linklist(in efficent manner)..

Answer / vishnu

typedef struct LL_tag
{
int data ;
struct LL_tag *next ;
} LL ;

/*Pass a valid singly linked list*/

LL* Mid (LL *head)
{
LL *one, *two ;

one = two = head ;

while (two)
{
two = two->next ;
if (two)
{
two = two->next ;
one = one->next ;
}
else
{
two = NULL ;
}
}
return one ;
}

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More C Interview Questions

Explain what math functions are available for integers? For floating point?

0 Answers  


Why is a semicolon (;) put at the end of every program statement?

0 Answers  


what is develop in c language

2 Answers  


what is the differnce between AF_INET and PF_INET?

5 Answers   Systems Plus, Wipro,


What is the output of the following program main();{printf ("chennai""superkings"}; a. Chennai b. superkings c. error d. Chennai superkings

6 Answers  






A collection of functions,calls,subroutines or other data a) library b) header files c) set of files d) textfiles

0 Answers  


Sir i want e-notes of C languge of BAlaguruswami book i.e scanned or pdf file of balaguruswamy book on c language.PLEASE SEND ME on my mail id ajit_kolhe@rediff.com

8 Answers  


Can one function call another?

0 Answers  


IS STRUCTURES CAN BE USED WITHIN AN ARRAY?

7 Answers   Caritor,


what is a pointer

4 Answers   Bank Of America, TCS,


What are types of preprocessor in c?

0 Answers  


What is the difference between null pointer and wild pointer?

0 Answers  


Categories