Linked List

S

source

function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.
 
S

Siemel Naran

source said:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

Show us your best effort so far. Also try to write a function that finds
the 5th element in a container matching a certain condition (for example,
the element is >10).
 
O

osmium

source said:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

For test cases, test for within range, each valid boundary and one beyond
each valid boundary. Valid because numbering might start with zero or one.
 
S

Siemel Naran

If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the
beginning.

You can do it in one pass. The first way to implement it could actually be
slower than the 2 pass method, and the second way would probably be faster.

For test cases, test for within range, each valid boundary and one beyond
each valid boundary. Valid because numbering might start with zero or
one.

I'd say the empty list, list of 1 element, list of 5 elements, list of >5
elements.
 
D

David Harmon

On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, (e-mail address removed)
(source) wrote,
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

This sentence no verb.

What I consider the usual approach to that kind of thing is what I call
a "following pointer". That is, you have two iterators, one points to
where you are looking in the list and the other five items behind it.
You advance both of them at the same time. When you get to what you
were looking for (in this case, the end) the "following" iterator points
to what you wanted.

No doubt you can write the C++ code based on that.
 
P

Pete

source said:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

And please dont think this is my homework.
Can anybody simple tell me how this can be done. I am preparing for an
interview and wanted to know how to go about implementing this.
Or atleast tell me where I can find few examples on traversing Lists.
Even if I can get any good resources which explains lists properly my
job will be done.

P-code, and without error checking (assumes list is at least 5 long):

node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

- Pete
 
K

Kevin Goodsell

osmium said:
source writes:




If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Keep two pointers. Begin traversing the list with the first. 5 elements
in, begin traversing with the second. Once the first reaches the end,
the second will be 5 elements from the end.

-Kevin
 
M

Mark A. Gibbs

Siemel said:
What happens on a list of 0 to 4 elements?

As he noted:

"P-code, and without error checking (assumes list is at least 5 long):"

mark
 
S

Siemel Naran

Pete said:
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::eek:perator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::eek:perator++ on node, and N-5 on node5.
So don't they both have the same running time?
 
P

Pete

Siemel said:
Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::eek:perator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::eek:perator++ on node, and N-5 on node5. So don't they
both have the same running time?

No, it really wouldn't be faster much if at all, but it's much more
"elegant" IMHO.

- Pete
 
D

David Harmon

On Mon, 05 Apr 2004 00:42:42 GMT in comp.lang.c++, "Siemel Naran"
Is it really faster than the 2 pass method? In the 2 pass method, first
visit all N elements for a total of N calls to const_iterator::eek:perator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::eek:perator++ on node, and N-5 on node5.
So don't they both have the same running time?

Nobody said it was faster. The problem spec said "in one pass".
The question is, with both pointers running through the list like that
does it count as one pass, or two passes running not quite in parallel?
 
P

Pete

Siemel said:
Is it really faster than the 2 pass method? In the 2 pass method,
first visit all N elements for a total of N calls to
const_iterator::eek:perator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::eek:perator++ on node, and N-5 on node5. So don't they
both have the same running time?

BTW, this exercise is a good example of a good reason to use a doubly linked
list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't
an issue, usually.

- Pete
 
A

Alf P. Steinbach

* (e-mail address removed) (source) schriebt:
function that would: return the 5th element from the end in a singly
linked list of integers, in one pass, and then provide a set of test
cases
against that function.

What is your definition of "one pass", and does this need to be
non-destructive?
 
S

Siemel Naran

Pete said:
Siemel Naran wrote:
node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

for(;;)
{
if(node5->next == 0)
{
return node->val;
}
node = node->next;
node5 = node5->next;
}

Seems the 2 pass method you maintain an index for looping from [0, N-5) but
in the 1 pass method you don't. So based on that the 1 pass method might be
faster, though the improvement is probably negligible.

BTW, this exercise is a good example of a good reason to use a doubly linked
list, because it's easy to go either forwards or back depending on the
situation. The extra memory used for the extra pointer per node really isn't
an issue, usually.

If going back is not a common operation, the single list could be better.
Anyway, the point of these 'clever' interview questions is not always to
mimic reality, but to see how you think through problems, especially the
logic questions.

Anyway, I think if you just store the value of the pointer in one pass, you
could get better


node* node = yourfirstnode;
node* node5 = node->next->next->next->next;

T * last[5] = { &node->val, &node->next->val, ... };

for(int i=0; ; i = (i==5 ? 0 : 5))
{
if(node5->next == 0)
{
return last;
}
last = &node5->val;
node5 = node5->next;
}
 
N

Niels Dybdahl

Is it really faster than the 2 pass method?

If the time for retrieval of one element is high, then a one pass approach
is faster than a two pass.
However the method of using two pointers are actually a two pass algorithm
in that case (each element is loaded/passed twice).
A one pass algorithm, that would save time, would cache the elements once
loaded and maintain a local 5 element list.

Niels Dybdahl
 
K

Karl Heinz Buchegger

osmium said:
If I understand the question, it can't be done; at least on a bare bones
linked list. You will have to count the elements so you can subtract five
from that. And then traverse the list again, which would be easiest from
the head end. I call that two passes, not one that you asked for. My
default meaning for end is the far end; I call the other end the beginning.

Or simply:
Use a queue to store exactly 5 pointers to nodes as you traverse the list.
When you have reached the end of the list, the queue will contain pointers
to the last 5 list elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

linked list 14
linked list 5
sorting linked list 1
Binary Tree 2 Linked List 6
linked list 3
Linked List Library 18
linked list program in c++ 7
Indexed list 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top