|
Linked Lists |
|
|
A linked list is a technique of creating a collection
of items where an item can be located based on another existing item,
unless the list contains only one item, in which case the item would be
located from the beginning (called the head).
|
Each item of a list be of a primitive type. If not, you can use an existing class or you can create your own class that would
represent each type of item of the eventual list. The class doesn't have to be
fancy. It can consist of a simple one item or it can contain as many members as
you judge necessary. Besides the desired value(s), the class should (must) also
contain a tag that would be used to locate one item from the next.
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinkedList1
{
public class Item
{
public double Value;
public Item Next;
}
public class Items
{
int size;
public Item Head;
public Items()
{
size = 0;
Head = null;
}
public int Count
{
get { return size; }
}
public int Add(Item NewItem)
{
Item Sample = new Item();
Sample = NewItem;
Sample.Next = Head;
Head = Sample;
return size++;
}
public Item Retrieve(int Position)
{
Item Current = Head;
for (int i = Count - 1; i > Position && Current != null; i--)
Current = Current.Next;
return Current;
}
public bool Delete()
{
if (Head == null)
{
Console.WriteLine("The list is empty");
return false;
}
Item Current;
Current = Head.Next;
Head.Next = Current.Next;
size--;
return true;
}
public bool Find(Item toFind)
{
Item Current = new Item();
if (toFind == null)
return false;
for (Current = Head; Current != null; Current = Current.Next)
{
if (Current.Value == toFind.Value)
return true;
}
return false;
}
}
public class Program
{
static int Main(string[] args)
{
Item real;
Items reals = new Items();
real = new Item();
real.Value = 2974.03;
reals.Add(real);
real = new Item();
real.Value = 748.25;
reals.Add(real);
real = new Item();
real.Value = 50883.82;
reals.Add(real);
real = new Item();
real.Value = 29.24;
reals.Add(real);
real = new Item();
real.Value = 772.85;
reals.Add(real);
real = new Item();
real.Value = 106628.06;
reals.Add(real);
Console.WriteLine("Number of items: {0}", reals.Count);
for (int i = 0; i < reals.Count; i++)
{
Item nbr = reals.Retrieve(i);
Console.WriteLine("Number[{0}] = {1}", i, nbr.Value);
}
reals.Delete();
Console.WriteLine("\nNumber of items: {0}", reals.Count);
for (int i = 0; i < reals.Count; i++)
{
Item nbr = reals.Retrieve(i);
Console.WriteLine("Number[{0}] = {1}", i, nbr.Value);
}
Item nbrToFind = new Item();
nbrToFind.Value = 26486.56;
bool Found = reals.Find(nbrToFind);
if (Found == true)
Console.WriteLine("\nThe number {0} was found in the list",
nbrToFind.Value);
else
Console.WriteLine("\nThe number {0} was NOT found in the list",
nbrToFind.Value);
nbrToFind = new Item();
nbrToFind.Value = 50883.82;
Found = reals.Find(nbrToFind);
if (Found == true)
Console.WriteLine("The number {0} was found in the list\n",
nbrToFind.Value);
else
Console.WriteLine("The number {0} was NOT found in the list\n",
nbrToFind.Value);
return 0;
}
}
}
This would produce:
Number of items: 6
Number[0] = 2974.03
Number[1] = 748.25
Number[2] = 50883.82
Number[3] = 29.24
Number[4] = 772.85
Number[5] = 106628.06
Number of items: 5
Number[0] = 2974.03
Number[1] = 748.25
Number[2] = 50883.82
Number[3] = 29.24
Number[4] = 106628.06
The number 26486.56 was NOT found in the list
The number 50883.82 was found in the list
Press any key to continue . . .
|
|