Linked list

  • Thread starter Thread starter source
  • Start date Start date
S

source

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.
 
source said:
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.
 
Daniel O'Connell said:
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.
 
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
 
source said:
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/PhSinglyLinkedLists.asp?target=linked|list

I don't have an example for IList, unfortunatly.
 
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.
 
[top posting fixed]
source said:
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 said:
[top posting fixed]
source said:
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");
}
}
}

========================
 
Back
Top