The following code demonstrates how to write and use a link list in C#.
using System;
namespace ThisExample
{
class LinkList
{
public string Val;
public object NextItem;
[STAThread]
static void Main(string[] args)
{
LinkList obList = new LinkList();
obList.Val = "Head";
LinkList obHead = obList;
// Add 10 items to the list.
for (int i = 0; i < 10; i++)
{
obList.NextItem = new LinkList();
obList = (LinkList)obList.NextItem;
obList.Val = i.ToString();
}
// Print the added items.
while (obHead != null)
{
Console.WriteLine("Item: {0}", obHead.Val);
obHead = (LinkList)obHead.NextItem;
}
}
}
}
No comments:
Post a Comment