Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Implement a function that returns the 5th element from the
end in a singly linked list of integers in one pass.

Answers were Sorted based on User's Feedback



Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / amol b

int return_fifth_from_end()
{
int a[5],curr_ct=0;
struct node *p;
p=head;
while(p->next!=NULL)
{
a[curr_ct%5]=p->val;
p=p->next;
curr_ct++;
}
if(curr_ct>=5)
return a[(curr_ct-5)%5];
else
return -1;
}

Is This Answer Correct ?    8 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / abhijit annaldas

Sorry, it was my mistake.. previous answer was not correct.
Here is the corrected one...

node* getNthFromLast(node* head, int n)
{
int c=0;
node *nth=head;
node *pt=head;
while(pt!=NULL)
{
pt=pt->next;
c++;
//if c=n then nth node is already set to head.
if(c>n)
nth=nth->next;
}
if(c<n) //LL contains less than n nodes
return (*node)0;
else
return *nth;

}

Use it as..
fifth_node = getNthFromLast(head, 5);

Is This Answer Correct ?    3 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / tarun dhiraj

Consider:
struct Node
{
int data;
struct Node *next;
}*start;

void FIFTHFRMLAST()
{
struct Node *ptr;
ptr=start;

printf("\n");

/*Traverse elements of linked list till the 5th element from
the end of linked list*/
while(ptr->next->next->next->next->next!=NULL)
{
ptr=ptr->next;
}
printf("->%d",ptr->data);

Is This Answer Correct ?    3 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / abhijit annaldas

node* getNthFromLast(node* head, int n)
{
int c;
node *nth;
while(pt!=NULL)
{
pt=pt->next;
c++;
if(c>=n)
*nth=pt;
}
if(c<n) //LL contains less than n nodes
return (*node)0;
else
return *nth;
}

Use it as..
fifth_node = getNthFromLast(head, 5);

Is This Answer Correct ?    2 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / manesh nambiar

int return_fifth_from_end()
{
int i,j;
struct node *p,*q;
*p=HEAD_NODE;
for(i=0;i<4;i++)//will make p point to the 5th element
{
p=p->next;
if(p==NULL)
{
printf("List has less than 5 elements");
}
}
q=HEAD_NODE;
while(p!=NULL)
{
p=p->next;
q=q->next;
}
return(q->Value);
}

Is This Answer Correct ?    8 Yes 7 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / lly

I think both #1 & #2 are wrong. Because the code travels
the list in two passes not one pass.

Is This Answer Correct ?    1 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / jishu

Answer 6 is correct and well thought but there is a tiny
mistake.

the while loop should check upto the last node which it
doesn't and hence prints the wrong value ie. value of the
node before the fifth node from the end.

while(p!=NULL) would give the correct node.

Also while returning the value, decrementing curr_ct by 5 is
not really necessary.

return a[curr_ct%5]; would give the same value in all cases.

Is This Answer Correct ?    1 Yes 0 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / vignesh1988i

let us assume that we have created a linked lists ..... the
no. of nodes is say 15.
count=15;
printf("enter the node do you wann to look out :\n");
scanf("%d",&i);
if(i>count || i>=0)
printf("no node exists like this \n");
else
{
i=count-i;
view_node(i);
printf("%d",HEAD1->data);
}
getch();
}
void view_node(int i)
{
HEAD1=HEAD;
for(int k=1;k<i;k++)
HEAD1=->HEAD1->ptr;
}

Is This Answer Correct ?    2 Yes 2 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / gbohrn

int return_fifth_from_end()
{
int i,j;
struct node *p,*q;
*p=HEAD_NODE;
for(i=0;i<4;i++)//will make p point to the 5th element
{
p=p->next;
if(p==NULL)
{
printf("List has less than 5 elements");
}
}
q=HEAD_NODE;
while(p->next!=NULL)
{
p=p->next;
q=q->next;
}
return(q->Value);
}

Is This Answer Correct ?    5 Yes 6 No

Implement a function that returns the 5th element from the end in a singly linked list of integers..

Answer / a.eklare7

int fifth_element_last(node *head)
{
node *p;
int len=0;
p=head;
while(p->next!=NULL)
{
p=p->next;
len++;
}
p=head;
for(int i=1;i<=len-4;i++)
p=p->next;
return(p->data);
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

What are the concepts introduced in OOPs?

3 Answers  


Write a program in c to print * * * * * *******

1 Answers  


WHOT IS CHAR?

4 Answers   TCS,


What are data structures in c and how to use them?

0 Answers  


if the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one of the item

2 Answers  


How is actual parameter different from the formal parameter?

0 Answers  


Find duplicates in a file containing 6 digit number (like uid) in O (n) time.

0 Answers   GrapeCity,


Why is C called a middle-level language?

1 Answers  


what is difference between array,strutter,union and pointers

3 Answers   CTS, Lovely Professional University, Mannar Company,


How does C++ help with the tradeoff of safety vs. usability?

1 Answers  


write a c/c++ programthat connects to a MYSQL server and checks if the INNoDB plug in is installed on it.If so your program should print the total number of disk writes by MYSQL.

0 Answers   BirlaSoft,


Explain how do you list files in a directory?

0 Answers  


Categories