Memory is not a constraint. In a single iteration(NOTE: you
can't go back), how will you find out the 10th last
node/item in a linked list.
Answers were Sorted based on User's Feedback
Answer / vivek
ListNodePtr* tenthListNodePtr = NULL; //Holds 10th last node
ListNodePtr* tempListNodePtr = firstNode;
int counter = 1;
//Advance the tempListNodePtr to 10th node from first //node.
while( (counter < 10) && (tempListNodePtr) )
{
tempListNodePtr = tempListNodePtr->nextNode;
++counter;
}
tenthListNodePtr = firstNode;
//Advance both the pointers. also check for cycle.
// Since two ptrs differ by 10, when last node is reached
//the result will be there.
while( (tempListNodePtr) && (tempListNodePtr != firstNode) )
{
tenthListNodePtr = tenthListNodePtr->nextNode;
tempListNodePtr = tempListNodePtr->nextNode;
}
| Is This Answer Correct ? | 14 Yes | 3 No |
Answer / ranjani
The approach is to have 2 ptrs ,ptr1 and ptr2.Where ptr1
would start at the first node of the linked list and ptr2
would be at the kth position from ptr1.In this case k=10.
From there on until ptr2->next!=null keep moving ptr1 and
ptr2 by one each.When ptr2 reached the last element of the
linked list.Ptr1 would be pointing to the 10th (kth) last
element.
Example:
1->2->3->4->5->6->7->8->9->10->11->12. In this case 10th
last element would be 3.
Start with ptr1 at node 1 and ptr2 at node 10.
Now till ptr2->next!=null ptr1=ptr1->next and ptr2=ptr2->next.
This way when ptr2 reaches 12 ptr1 would be at 3,which is
the 10th last element.
| Is This Answer Correct ? | 8 Yes | 0 No |
Answer / atul bodke
suppose list is already created having some elements
poninted by start ......
node* start;
node*p[10];
node* ptr;
int n=0;
ptr=start;
while(ptr->next==null)
{
p[(n++)%10]=ptr;
}
if(n<10) printf("there r unsufficient elements");
else { ptr=p[n%10]}
| Is This Answer Correct ? | 6 Yes | 1 No |
Answer / mms zubeir
Ashutosh,
Can you explain how your answer sync with the question?
| Is This Answer Correct ? | 6 Yes | 2 No |
Answer / dg
ahutosh ,the kid is right pos% 10 will do fine,,are u
nuts,work on basic maths
| Is This Answer Correct ? | 6 Yes | 2 No |
Answer / ravindra
simple,
in case the list has say 90 elements, then for every 10
elements, he is writing to the array. though the array size
is 10, this is done by saying pos%10.
thus when the list is iterated he just substracts 10 from
the pos and then does %10 since he has to factor in, that
array starts from 0 and not from 1 :)
| Is This Answer Correct ? | 5 Yes | 2 No |
Answer / ashutosh
NO, it won't work.
because, after the wile loop, pos is pointing to the last
element NOT 10th last. So, you have to go back 10 elements.
| Is This Answer Correct ? | 5 Yes | 2 No |
Answer / atul bodke
correction in prev answer ...
i saw it after posting ... :)
correction in while loop ...
while(ptr!=null)
{
p[(n++)%10]=ptr;
ptr=ptr->next;
}
................................
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / rohith
it can be just done tis way!!
temp=first;
while(temp->link->link->link->link->link->link->link->link-
>link!=NULL)
{
temp=temp->link;
}
printf("%d",temp->data);
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / mms zubeir
I got it, thank you.
but one suggestion, instead of using
printf("Tenth last element is %d",nodes[(pos-10)%10]->data);
you can simply give,
printf("Tenth last element is %d",nodes[pos%10]->data);
| Is This Answer Correct ? | 3 Yes | 2 No |
Explain the different access specifiers for the class member in c++.
What is binary search in c++?
when can we use virtual destructor?
7 Answers HCL, HP, Virage Logic,
Explain the difference between 'operator new' and the 'new' operator?
Can you declare an array without a size in c++?
Where do I find the current c or c++ standard documents?
What is the difference between the functions rand(), random(), srand() and randomize()?
What is difference between malloc()/free() and new/delete?
what is the size of this class class size { public: char data1; double d; int data2; char data3; double data4; short data5; }; please explain the padding for these double variables.
how can i access a direct (absolute, not the offset) memory address? here is what i tried: wrote a program that ask's for an address from the user, creates a FAR pointer to that adress and shows it. then the user can increment/decrement the value in that address by pressing p(inc+) and m(dec-). NOW, i compiled that program and opened it twice (in 2 different windows) and gave twice the same address to it. now look what happen - if i change the value in one "window" of the program, it DOES NOT change in the other! even if they point to the same address in the memory! here is the code snippet: //------------------------------------------------------ #include <stdio.h> //INCLUDE EVERY KNOWN HEADER FILE #include <conio.h> //FOR ANY CASE... #include <iostream.h> #include <dos.h> #include <process.h> main() { int far *ptr; //FAR POINTER!!! long address; char key=0; //A KEY FROM THE KEYBOARD int temp=0; clrscr(); cout<<"Enter Address:"; cin>>hex>>address; //GETS THE ADDRESS clrscr(); (long)ptr=address; temp=*ptr; //PUTS THE ADDRESS IN THE PTR cout<<"["<<hex<<(unsigned long)ptr<<"]="<<*ptr<<" = "<< (char)(*ptr); //SHOWS: [address]=value=ASCII symbol. while (key!=27) //WHILE YOU DONT PRESS ESC. { while(!kbhit()) //WHILE KEY IS NOT PRESSED { if (temp!=*ptr) { temp=*ptr; clrscr(); cout<<"["<<hex<< (unsigned long)ptr<<"]="<<*ptr<<" = "<<(char)(*ptr); }; //IF THE VALUE HAS CHANGED, CLEAR THE SCREEN AND SHOW //AGAIN if (key=='p') {key=0; (*ptr)++; } //INCREMENT VALUE if (key=='m') {key=0; (*ptr)--; } //DEC. VALUE }; key=getch(); //IF A KEY IS PRESSED, READ IT FROM THE //KEYBOARD }; return 0; //IF ESC WAS THE KEY, EXIT THE PROGRAM } //---------------------------------------------------------
Is string an object in c++?
What is #include iostream in c++?