@kamakshi this code will work for every value except when your list has only one node.for that you will have to add check : p=head; q=head; if(p->link == NULL) printf("The middle element is %d",p->data); else { if(q->link->link!=NULL) {
p=p->link; q=q->link->link; } printf("The middle element is %d",p->data);
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
p=head;
ReplyDeleteq=head;
if(q->next->next!=NULL)
{
p=p->next;
q=q->next->next;
}
printf("The middle element is %d",p->data);
There is a problem in the above code.It does not work if there is a single node,or there are odd no of nodes.
ReplyDeleteit should be like this:
int middle(struct node *head)
{
p=head;
q=head;
if(head->next==NULL)
return (p->data);
else
while(q->next->next!=NULL)
{
p=p->next;
if(q->next!=NULL)
q=q->next->next;
else
return (p->data);
}
return(p->data);
}
@kamakshi this code will work for every value except when your list has only one node.for that you will have to add check :
ReplyDeletep=head;
q=head;
if(p->link == NULL)
printf("The middle element is %d",p->data);
else
{
if(q->link->link!=NULL)
{
p=p->link;
q=q->link->link;
}
printf("The middle element is %d",p->data);