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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Process by which one bit pattern in to another by bit wise operation is?

615


What are examples of structures?

597


Explain what is gets() function?

635


a linearly ordered set of data elements that have the same structure and whose order is preserved in storage by using sequential allocation a) circular b) ordinary c) array d) linear list

635


Write a C program to help a HiFi’s Restaurant automate its breakfast billing system. Your assignment should implement the following items: a. Show the customer the different breakfast items offered by the HiFi’s Restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill to the customer. d. Produce a report to present your complete program and show more sample output. Assume that the HiFi’s Restaurant offers the following breakfast menu: Plain Egg $2.50 Bacon and Egg $3.45 Muffin $2.20 French Toast $2.95 Fruit Basket $3.45 Cereal $0.70 Coffee $1.50 Tea $1.80

2559






When should structures be passed by values or by references?

586


Do pointers need to be initialized?

564


What is a loop?

556


What is the use of function in c?

715


What are void pointers in c?

575


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

2008


What is a #include preprocessor?

620


What is the purpose of the statement: strcat (S2, S1)?

642


What are local variables c?

552


What are the 4 types of organizational structures?

626