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

Differentiate call by value and call by reference?

0 Answers   Cyient,


How to run c Program without using IDE of c. means if program made in notepad.then how to compile by command prompt.

1 Answers   HP, TCS,


who developed c and why he developed c?

5 Answers  


Write a program for finding factorial of a number.

0 Answers   Tech Mahindra,


Explain how can I make sure that my program is the only one accessing a file?

0 Answers  






Study the code: void show() main() { show(); } void show (char *s) { printf("%sn",s); } What will happen if it is compiled & run on an ANSI C Compiler? A)It will compile & nothing will be printed when it is executed B)it will compile but not link C)the compiler will generate an error D)the compiler will generate a warning

4 Answers   Accenture,


Why doesnt that code work?

0 Answers  


what is a pointer

4 Answers   Bank Of America, TCS,


what is the maximum no. of bytes calloc can allocate

4 Answers   Mphasis,


Why is not a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it is been freed?

0 Answers  


Tell me when would you use a pointer to a function?

0 Answers  


Take an MxN matrice from user and then sum upper diagonal in a variable and lower diagonal in a separate variables. Print the result

0 Answers  


Categories