473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9824

"source" <so***********@ hotmail.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP12.phx.gbl...

"source" <so***********@ hotmail.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.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****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea******** ******@TK2MSFTN GP12.phx.gbl...

"source" <so***********@ hotmail.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP10.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****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea******** ******@TK2MSFTN GP12.phx.gbl...
>
> "source" <so***********@ hotmail.com> wrote in message
> news:ux******** ******@TK2MSFTN GP09.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****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea******** ******@TK2MSFTN GP12.phx.gbl...

"source" <so***********@ hotmail.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP09.phx.gbl...
"Mabden" <Ma****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:ea******** ******@TK2MSFTN GP12.phx.gbl...

"source" <so***********@ hotmail.com> wrote in message
news:ux******** ******@TK2MSFTN GP09.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****@sbcglob al.net> wrote in message
news:hC******** ***********@new ssvr29.news.pro digy.com...
[top posting fixed]
"source" <so***********@ hotmail.com> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
"Mabden" <Ma****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:ea******** ******@TK2MSFTN GP12.phx.gbl...
>
> "source" <so***********@ hotmail.com> wrote in message
> news:ux******** ******@TK2MSFTN GP09.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.Collecti ons;

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.WriteLi ne ("\nCollecti on order\n");
ICollection values = hashmonth.Value s;
foreach (string month in values)
{
Console.WriteLi ne ("Month: {0}", month);
}

// access by month number
// with follower 5 behind
int follow = 0;
int start_follow = 5;
Console.WriteLi ne ("\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(Follo w Month: {0})", hashmonth[++follow]);
}
Console.Write ("\n");
}
}
}

=============== =========
--
Mabden
Nov 16 '05 #8
"Mabden" <Ma****@sbcglob al.net> wrote in message
news:hC******** ***********@new ssvr29.news.pro digy.com...
[top posting fixed]
"source" <so***********@ hotmail.com> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
"Mabden" <Ma****@sbcglob al.net> wrote in message
news:Eh******** ***********@new ssvr25.news.pro digy.com...
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:ea******** ******@TK2MSFTN GP12.phx.gbl...
>
> "source" <so***********@ hotmail.com> wrote in message
> news:ux******** ******@TK2MSFTN GP09.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.Collecti ons;

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.WriteLi ne ("\nCollecti on order\n");
ICollection values = hashmonth.Value s;
foreach (string month in values)
{
Console.WriteLi ne ("Month: {0}", month);
}

// access by month number
// with follower 5 behind
int follow = 0;
int start_follow = 5;
Console.WriteLi ne ("\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(Follo w 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
3084
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
859
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
10
15113
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
6
4590
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 potential pitfalls I'd be extremely grateful. Cheers Steve
12
15083
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 should be kept for references to previous element of list. Here is my solution below: struct node {
12
3939
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do...
51
8607
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 for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for...
1
15516
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 list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the...
0
8616
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL...
7
5760
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.