Doubly LinkedList Sample

using System;

namespace List
{
public class DoublyLinkedList
{
public const int EQ   =  0;

private Node head     = null;
private Node tail     = null;
class Node: IComparable
{
public Node next;
public Node prev;
public object value;
public Node(object value)
{
this.value = value;
}

public int CompareTo(object value)
{
Type t  = this.value.GetType();
if(t != value.GetType())
throw(new ArgumentException());
if(t.ToString().Equals(“System.String”))
return this.value.ToString().CompareTo(value);
else
{
if((int)this.value == (int)value)
return 0;
else if((int)this.value < (int)value)
return -1;
else
return 1;
}
}
}

public void AddNodeToHead(object value)
{
Node node = new Node(value);
if(head == null)
{
head = node;
tail = node;
}
else
{
node.next = head;
head.prev = node;
head = node;
}
}

public void AddNodeToTail(object value)
{
Node node = new Node(value);
if(tail == null)
{
head = node;
tail = node;
}
else
{
node.prev = tail;
tail.next = node;
tail = node;
}
}

public void RemoveNodeFromHead()
{
if(head == null)
return ;
if(head == tail)
{
head = null;
tail = null;
}
else
{
head.next.prev = null;
head = head.next;
}
}

public void RemoveNodeFromTail()
{
if(tail == null)
return ;
if(head == tail)
{
head = null;
tail = null;
}
else
{
tail.prev.next = null;
tail = tail.prev;
}
}

public void PrintListFromHead()
{
Node node = head;
while( node != null )
{
if(node.value.GetType().ToString()==”System.String”)
Console.Write(“‘{0}’ “,node.value);
else
Console.Write(“{0} “,node.value);
node = node.next;
}
Console.WriteLine();
}

public void PrintListFromTail()
{
Node node = tail;
while( node != null )
{
if(node.value.GetType().ToString()==”System.String”)
Console.Write(“‘{0}’ “,node.value);
else
Console.Write(“{0} “,node.value);
node = node.prev;
}
Console.WriteLine();

}

public void AccessNodeOfList()
{
Node node = head;
Console.Write(“‘{0}’ “,node.value);
Console.Write(“‘{0}’ “,node.next.value);
Console.Write(“‘{0}’ “,node.next.next.value);
Console.Write(“‘{0}’ “,node.next.next.prev.value);
Console.Write(“‘{0}’\n”,node.next.next.prev.prev.value);
}

public object this[int index]
{
get
{
Node node = head;
while(index > 0)
{
node = node.next;
index–;
}
return node.value;
}
}

public int SearchNode(object value)
{
int index  = -1;
if(head == null)
return index;
Node node = head;
while(node != null)
{
index++;
try
{
if(node.CompareTo(value) == EQ)
{
return index;
}
}
catch { }
node = node.next;
}
return -1;
}
}

public class DoublyLinedListMain
{
static void Main(string[] args)
{

DoublyLinkedList list = new DoublyLinkedList();

Console.WriteLine(“Add ‘BIOS’ to Tail of List”);
list.AddNodeToTail(“BIOS”);
Console.WriteLine(“Add ‘ADSL’ to Head of List”);
list.AddNodeToHead(“ADSL”);
Console.WriteLine(“Add ‘CATV’ to Tail of List”);
list.AddNodeToTail(“CATV”);
Console.WriteLine(“Add  100   to Head of List”);
list.AddNodeToHead(100);
Console.WriteLine(“Add  1000  to Tail of List”);
list.AddNodeToTail(1000);

Console.WriteLine(“Print List From Head”);
list.PrintListFromHead();
int index = list.SearchNode(“CATV”);
if(index >= 0)
Console.WriteLine(“‘{0}’ is found.”,list[index]);

index = list.SearchNode(1000);
if(index >= 0)
Console.WriteLine(“{0} is found.”,list[index]);

}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s