pgm to find middle element of linklist(in efficent manner)
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
Who invented b language?
what is the maximum limit of row and column of a matrix in c programming. in linux .
Which one would you prefer - a macro or a function?
how to find a 5th bit is set in c program
What is formal argument?
write a program to print data of 5 five students with structures?
What language is windows 1.0 written?
define c
How can I check whether a file exists? I want to warn the user if a requested input file is missing.
what is an ERP?
write a program to generate address labels using structures?
Give me basis knowledge of c , c++...