473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Linked list

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.
Can we do this in CSharp?
If no, then can anybody tell me how to go about coming with the solution.

How do u develop test cases for any software you write?

Any insight will be helpful.

Nov 16 '05 #1
8 9810

"source" <so***********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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.
Can we do this in CSharp?
If no, then can anybody tell me how to go about coming with the solution.
Of course it can be done, except this sounds an awful lot like you are
trying to get someone to do your homework. I won't give you a solution but
I'll tell you waht to do

1. Write a singally linked list. This is pretty simple. I would advise
building a LinkedList around IList and using that to access your nodes if
you can. Using a manager class will allow you to centralize list
modifications and let you keep track of the node count.

2. retrieving the value at a given index is a pretty generic function. If
you understand how to write the list, you should be able to do this. Hint:
keeping track of node count is a nessecity here

3. To generate tests cases, you would basically build a set of lists and
check to make sure you are getting hte correct value back.
How do u develop test cases for any software you write?
This is easier to answer. You develop test cases by subjecting the method to
a series of values which result in a provable output. Since above you are
looking at the 5th value from the end, you would create a list that is
atleast 5 membesr long and see if the method that locates the 5th member
returns the proper member(If the set is {1,2,3,4,5,6,7} then the correct
value is 2). For more complex functionality your tests will have to monitor
class and possibly application state, but don't get ahead of yourself for
now.

The only other advice I can give you is look out for edge cases and that you
will never catch every edge case.
Any insight will be helpful.

Nov 16 '05 #2
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea**************@TK2MSFTNGP12.phx.gbl...

"source" <so***********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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.
Can we do this in CSharp?
If no, then can anybody tell me how to go about coming with the
solution.
Of course it can be done, except this sounds an awful lot like you are
trying to get someone to do your homework.


Traverse the list with one reference, and have another one that follows
along 5 elements back. When you reach the end of the list with the first
reference, the second one will be 5 from the end. You would need a counter
to make sure you have at least 5 elements, of course.
Nov 16 '05 #3
This is not my homework. I read this question on one website, and I am
preparing for an interview.
Wanted to know where I can get good source for creating linked list.
I tried looking up MSDN for examples that will implement ILIST interface,
but did not find any examples.
The only reference MSDN gives for using ILIST is for the Listbox control.

Anyway thanks for the explanation for the test cases.
But any good resource for ILIST examples will definately help.

Thanks
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea**************@TK2MSFTNGP12.phx.gbl...

"source" <so***********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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.
Can we do this in CSharp?
If no, then can anybody tell me how to go about coming with the

solution.

Of course it can be done, except this sounds an awful lot like you are
trying to get someone to do your homework.


Traverse the list with one reference, and have another one that follows
along 5 elements back. When you reach the end of the list with the first
reference, the second one will be 5 from the end. You would need a counter
to make sure you have at least 5 elements, of course.

Nov 16 '05 #4

"source" <so***********@hotmail.com> wrote in message
news:ub**************@TK2MSFTNGP10.phx.gbl...
This is not my homework. I read this question on one website, and I am
preparing for an interview.
Wanted to know where I can get good source for creating linked list.
I tried looking up MSDN for examples that will implement ILIST interface,
but did not find any examples.
The only reference MSDN gives for using ILIST is for the Listbox control.

Anyway thanks for the explanation for the test cases.
But any good resource for ILIST examples will definately help. for linked lists
http://www.codeproject.com/csharp/Ph...=linked%7Clist

I don't have an example for IList, unfortunatly.

Thanks
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea**************@TK2MSFTNGP12.phx.gbl...
>
> "source" <so***********@hotmail.com> wrote in message
> news:ux**************@TK2MSFTNGP09.phx.gbl...
> >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.
> > Can we do this in CSharp?
> > If no, then can anybody tell me how to go about coming with the

solution.
>
> Of course it can be done, except this sounds an awful lot like you are
> trying to get someone to do your homework.


Traverse the list with one reference, and have another one that follows
along 5 elements back. When you reach the end of the list with the first
reference, the second one will be 5 from the end. You would need a
counter
to make sure you have at least 5 elements, of course.


Nov 16 '05 #5
Mabden,
I finally got my own code using sorted list, but now I think the real
challenge is to iterate through the SortedList without using the index.

If I use Foreach then I cannot think of anyway how am I going to introduce a
second reference which will be 5 from the end.
Basically how do I get the second reference to work.

Thanks daniel for the example, basically I was not looking at creating my
own linked list but a way to traverse through the linked list.
I was able to use SortedList data structure in .NET, but the way I could
traverse through was using the index,
but my problem is how do I get a second reference suggested by Mabden to
point to the element which will be 5 from behind if I use ForEach to iterate
through my list.

"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea**************@TK2MSFTNGP12.phx.gbl...

"source" <so***********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
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.
Can we do this in CSharp?
If no, then can anybody tell me how to go about coming with the

solution.

Of course it can be done, except this sounds an awful lot like you are
trying to get someone to do your homework.


Traverse the list with one reference, and have another one that follows
along 5 elements back. When you reach the end of the list with the first
reference, the second one will be 5 from the end. You would need a counter
to make sure you have at least 5 elements, of course.

Nov 16 '05 #6
[top posting fixed]
"source" <so***********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea**************@TK2MSFTNGP12.phx.gbl...

"source" <so***********@hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP09.phx.gbl...
>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.
> Can we do this in CSharp?
> If no, then can anybody tell me how to go about coming with the solution.

Of course it can be done, except this sounds an awful lot like you
are trying to get someone to do your homework.


Traverse the list with one reference, and have another one that

follows along 5 elements back. When you reach the end of the list with the first reference, the second one will be 5 from the end. You would need a counter to make sure you have at least 5 elements, of course.

Mabden,
I finally got my own code using sorted list, but now I think the real
challenge is to iterate through the SortedList without using the

index.
If I use Foreach then I cannot think of anyway how am I going to introduce a second reference which will be 5 from the end.
Basically how do I get the second reference to work.

Thanks daniel for the example, basically I was not looking at creating my own linked list but a way to traverse through the linked list.
I was able to use SortedList data structure in .NET, but the way I could traverse through was using the index,
but my problem is how do I get a second reference suggested by Mabden to point to the element which will be 5 from behind if I use ForEach to iterate through my list.


Please don't top-post.

If you can't figure out that from the clues given, I guess you don't
deserve the job (or an A, if it is homework). ;-)

--
Mabden
Nov 16 '05 #7
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:hC*******************@newssvr29.news.prodigy. com...
[top posting fixed]
"source" <so***********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:ea**************@TK2MSFTNGP12.phx.gbl...
>
> "source" <so***********@hotmail.com> wrote in message
> news:ux**************@TK2MSFTNGP09.phx.gbl...
> >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.
> > Can we do this in CSharp?
> > If no, then can anybody tell me how to go about coming with the
solution.
>
> Of course it can be done, except this sounds an awful lot like you are > trying to get someone to do your homework.

Traverse the list with one reference, and have another one that follows along 5 elements back. When you reach the end of the list with the first reference, the second one will be 5 from the end. You would need a counter to make sure you have at least 5 elements, of course.

Mabden,
I finally got my own code using sorted list, but now I think the real
challenge is to iterate through the SortedList without using the

index.

If I use Foreach then I cannot think of anyway how am I going to

introduce a
second reference which will be 5 from the end.
Basically how do I get the second reference to work.

Thanks daniel for the example, basically I was not looking at creating

my
own linked list but a way to traverse through the linked list.
I was able to use SortedList data structure in .NET, but the way I

could
traverse through was using the index,
but my problem is how do I get a second reference suggested by Mabden

to
point to the element which will be 5 from behind if I use ForEach to

iterate
through my list.


Please don't top-post.


Well, I thought about it a little more, and I don't think you can use
ForEach unless you build a new list inside the loop and then when the
loop exits, do a new foreach on the list you built which would be 5
smaller. This is a terrible way to program however.

If the index is numeric, you could use a regular for loop. Here's
something off the top of my head, but there's got to be a better way...
========================
using System;
using System.Collections;

class FollowTest
{
static void Main ()
{
Hashtable hashmonth = new Hashtable ();

hashmonth.Add (1, "January");
hashmonth.Add (2, "February");
hashmonth.Add (3, "March");
hashmonth.Add (4, "April");
hashmonth.Add (5, "May");
hashmonth.Add (6, "June");
hashmonth.Add (7, "July");
hashmonth.Add (8, "August");
hashmonth.Add (9, "September");
hashmonth.Add (10, "October");
hashmonth.Add (11, "November");
hashmonth.Add (12, "December");

// access as collection values (no follower)
Console.WriteLine ("\nCollection order\n");
ICollection values = hashmonth.Values;
foreach (string month in values)
{
Console.WriteLine ("Month: {0}", month);
}

// access by month number
// with follower 5 behind
int follow = 0;
int start_follow = 5;
Console.WriteLine ("\nMonth number order\n");
for (int i=0; i < 12; i++)
{
Console.Write ("Month: {0}", hashmonth[i+1]);
if (--start_follow < 1)
{
Console.Write ("\t\t(Follow Month: {0})", hashmonth[++follow]);
}
Console.Write ("\n");
}
}
}

========================
--
Mabden
Nov 16 '05 #8
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:hC*******************@newssvr29.news.prodigy. com...
[top posting fixed]
"source" <so***********@hotmail.com> wrote in message
news:uG**************@TK2MSFTNGP09.phx.gbl...
"Mabden" <Ma****@sbcglobal.net> wrote in message
news:Eh*******************@newssvr25.news.prodigy. com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:ea**************@TK2MSFTNGP12.phx.gbl...
>
> "source" <so***********@hotmail.com> wrote in message
> news:ux**************@TK2MSFTNGP09.phx.gbl...
> >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.
> > Can we do this in CSharp?
> > If no, then can anybody tell me how to go about coming with the
solution.
>
> Of course it can be done, except this sounds an awful lot like you are > trying to get someone to do your homework.

Traverse the list with one reference, and have another one that follows along 5 elements back. When you reach the end of the list with the first reference, the second one will be 5 from the end. You would need a counter to make sure you have at least 5 elements, of course.

Mabden,
I finally got my own code using sorted list, but now I think the real
challenge is to iterate through the SortedList without using the

index.

If I use Foreach then I cannot think of anyway how am I going to

introduce a
second reference which will be 5 from the end.
Basically how do I get the second reference to work.

Thanks daniel for the example, basically I was not looking at creating

my
own linked list but a way to traverse through the linked list.
I was able to use SortedList data structure in .NET, but the way I

could
traverse through was using the index,
but my problem is how do I get a second reference suggested by Mabden

to
point to the element which will be 5 from behind if I use ForEach to

iterate
through my list.


Please don't top-post.


Well, I thought about it a little more, and I don't think you can use
ForEach unless you build a new list inside the loop and then when the
loop exits, do a new foreach on the list you built which would be 5
smaller. This is a terrible way to program however.

If the index is numeric, you could use a regular for loop. Here's
something off the top of my head, but there's got to be a better way...
========================
using System;
using System.Collections;

class FollowTest
{
static void Main ()
{
Hashtable hashmonth = new Hashtable ();

hashmonth.Add (1, "January");
hashmonth.Add (2, "February");
hashmonth.Add (3, "March");
hashmonth.Add (4, "April");
hashmonth.Add (5, "May");
hashmonth.Add (6, "June");
hashmonth.Add (7, "July");
hashmonth.Add (8, "August");
hashmonth.Add (9, "September");
hashmonth.Add (10, "October");
hashmonth.Add (11, "November");
hashmonth.Add (12, "December");

// access as collection values (no follower)
Console.WriteLine ("\nCollection order\n");
ICollection values = hashmonth.Values;
foreach (string month in values)
{
Console.WriteLine ("Month: {0}", month);
}

// access by month number
// with follower 5 behind
int follow = 0;
int start_follow = 5;
Console.WriteLine ("\nMonth number order\n");
for (int i=0; i < 12; i++)
{
Console.Write ("Month: {0}", hashmonth[i+1]);
if (--start_follow < 1)
{
Console.Write ("\t\t(Follow Month: {0})", hashmonth[++follow]);
}
Console.Write ("\n");
}
}
}

========================
--
Mabden
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
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:
5
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...
10
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...
6
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...
12
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...
12
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...
51
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...
1
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...
0
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...
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.