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

find the minimum of three values inputted by the user

3 Answers  


What is static identifier?

0 Answers   TCS,


What does node * mean?

0 Answers  


What is the difference between printf and scanf in c?

0 Answers  


Explain high-order and low-order bytes.

0 Answers  






main() { int ptr[] = {1,2,23,6,5,6}; printf("%d",&ptr[3]-&ptr[0]); }

8 Answers   Vector,


What is omp_num_threads?

0 Answers  


Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;

0 Answers  


The process of repeatedly running a set of computer instructions until some condition is specifed a) condition b) sequential condition c) global d) iteration

0 Answers  


write a c program to remove all the duplicate characters in a string and replace with single character? ex:-input- AAABBBCCC output- ABC

2 Answers   HCL,


What is static memory allocation? Explain

0 Answers  


whether itis a structured language?

1 Answers   Microsoft,


Categories