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");
}
}
}
========================