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. 32 5657
"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).
"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).
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.
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.
"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.
"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.
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.
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.
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
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
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.
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.
"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?
"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?
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
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
"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?
"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?
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
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
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?
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?
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
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
* 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?
* 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?
"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;
}
"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;
}
> 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
> 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
by: Dream Catcher |
last post by:
1. I don't know once the node is located, how to return that node.
Should I return pointer to that node or should I return the struct of that
node.
2. Also how to do the fn call in main for that...
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
by: Steve Lambert |
last post by:
Hi,
I've knocked up a number of small routines to create and manipulate a linked
list of any structure. If anyone could take a look at this code and give me
their opinion and details of any...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: joshd |
last post by:
Hello,
Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: theeverdead |
last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked...
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
|
by: QiongZ |
last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |