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.

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a void pointer point to a function?

1029


What is c variable?

1049


What is linear search?

1141


my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

1590


What is local and global variable in c?

1210


What functions are used for dynamic memory allocation in c language?

1153


What is the difference between a string and an array?

1210


how many errors in c explain deply

2093


Is c a great language, or what?

1123


how to write a c program to print list of fruits in alpabetical order?

2306


Is it better to bitshift a value than to multiply by 2?

1094


Is that possible to store 32768 in an int data type variable?

1086


Is null always equal to 0(zero)?

1038


find the output? void r(int a[],int c, int n) { if(c>n) { a[c]=a[c]+c; r(a,++c,n); r(a,++c,n); } } int main() { int i,a[5]={0}; r(a,0,5); for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }

2316


What are the types of pointers in c?

1029