By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,978 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,978 IT Pros & Developers. It's quick & easy.

Linked List

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
32 Replies


P: n/a
"source" <su*********@hotmail.com> 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).

Jul 22 '05 #2

P: n/a
"source" <su*********@hotmail.com> 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).

Jul 22 '05 #3

P: n/a
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.
Jul 22 '05 #4

P: n/a
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.
Jul 22 '05 #5

P: n/a
"osmium" <r1********@comcast.net> 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.
Jul 22 '05 #6

P: n/a
"osmium" <r1********@comcast.net> 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.
Jul 22 '05 #7

P: n/a
On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, su*********@hotmail.com
(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.

Jul 22 '05 #8

P: n/a
On 4 Apr 2004 12:05:51 -0700 in comp.lang.c++, su*********@hotmail.com
(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.

Jul 22 '05 #9

P: n/a
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
Jul 22 '05 #10

P: n/a
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
Jul 22 '05 #11

P: n/a
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.
To contact me please use the address from a recent posting.
Jul 22 '05 #12

P: n/a
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.
To contact me please use the address from a recent posting.
Jul 22 '05 #13

P: n/a
"Pete" <x@x.x> 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?

Jul 22 '05 #14

P: n/a
"Pete" <x@x.x> 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?

Jul 22 '05 #15

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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

Jul 22 '05 #16

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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

Jul 22 '05 #17

P: n/a
"Pete" <x@x.x> 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::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?
Jul 22 '05 #18

P: n/a
"Pete" <x@x.x> 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::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ on node, and N-5 on node5.
So don't they both have the same running time?
Jul 22 '05 #19

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ 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
Jul 22 '05 #20

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ 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
Jul 22 '05 #21

P: n/a
On Mon, 05 Apr 2004 00:42:42 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote,
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::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ 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?

Jul 22 '05 #22

P: n/a
On Mon, 05 Apr 2004 00:42:42 GMT in comp.lang.c++, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote,
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::operator++.
Then you perform N-5 more calls to operator++. But in the 1 pass method,
you perform N calls to const_iterator::operator++ 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?

Jul 22 '05 #23

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ 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
Jul 22 '05 #24

P: n/a
Siemel Naran wrote:
"Pete" <x@x.x> 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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ 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
Jul 22 '05 #25

P: n/a
* su*********@hotmail.com (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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #26

P: n/a
* su*********@hotmail.com (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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #27

P: n/a
"Pete" <x@x.x> wrote in message news:yF2cc.15284
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;
}
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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?


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[i];
}
last[i] = &node5->val;
node5 = node5->next;
}
Jul 22 '05 #28

P: n/a
"Pete" <x@x.x> wrote in message news:yF2cc.15284
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;
}
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::operator++. Then you perform N-5 more calls to
operator++. But in the 1 pass method, you perform N calls to
const_iterator::operator++ on node, and N-5 on node5. So don't they
both have the same running time?


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[i];
}
last[i] = &node5->val;
node5 = node5->next;
}
Jul 22 '05 #29

P: n/a
> 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
Jul 22 '05 #30

P: n/a
> 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
Jul 22 '05 #31

P: n/a
osmium wrote:

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.


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #32

P: n/a
osmium wrote:

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.


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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #33

This discussion thread is closed

Replies have been disabled for this discussion.