Velocity Reviews > C++ > Linked List

source
Guest
Posts: n/a

 04-04-2004
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.

Siemel Naran
Guest
Posts: n/a

 04-04-2004
"source" <(E-Mail Removed)> wrote in message

> 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).

osmium
Guest
Posts: n/a

 04-04-2004
source writes:

> 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.

Siemel Naran
Guest
Posts: n/a

 04-04-2004
"osmium" <(E-Mail Removed)> wrote in message news:c4pnvn\$2kbbqu\$1@ID-

> > 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.

> 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.

David Harmon
Guest
Posts: n/a

 04-04-2004
On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, http://www.velocityreviews.com/forums/(E-Mail 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.

Pete
Guest
Posts: n/a

 04-04-2004
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.
>
> 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

Kevin Goodsell
Guest
Posts: n/a

 04-04-2004
osmium wrote:

> source writes:
>
>
>>function that would: return the 5th element from the end in a singly
>>linked list of integers, in one pass,

>
>
> 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
--
My email address is valid, but changes periodically.

Siemel Naran
Guest
Posts: n/a

 04-05-2004
"Pete" <(E-Mail Removed)> wrote in message news:yKZbc.14870

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

What happens on a list of 0 to 4 elements?

Mark A. Gibbs
Guest
Posts: n/a

 04-05-2004
Siemel Naran wrote:

> "Pete" <(E-Mail Removed)> wrote in message news:yKZbc.14870
>
>
>>node* node = yourfirstnode;
>>node* node5 = node->next->next->next->next;

>
>
> 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

Siemel Naran
Guest
Posts: n/a

 04-05-2004
"Pete" <(E-Mail Removed)> wrote in message news:yKZbc.14870

> 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:perator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator:perator++ on node, and N-5 on node5.
So don't they both have the same running time?