Learn Computer Science programming - C# and C++ Algorithms

 
C# Linked List 
Linked List implementation in C#. This is a simple Data Structure that create a linked list of nodes and point one to the other from head to tail. It  consists of a sequence of data records such that in each record there is a field that contains a reference to another nod
Picture
Code
c_linked_list.zip
File Size: 28 kb
File Type: zip
Download File


using System;


namespace LinkedList
{
/// <summary>
/// Summary description for LinkedList.
/// </summary>
public class LinkedList
{
int iCount = 1;
int iCurrent = 0;
Node nCurrent;


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int  blaat = 5;
LinkedList ll = new LinkedList(blaat);
ll.AddNode(9);
ll.GoTo(0);


Console.WriteLine(ll.CurrentNode.Value);
ll.ToNext();
Console.WriteLine(ll.CurrentNode.Value);


ll.AddNode (8);
Console.WriteLine(ll.CurrentNode.Value);
ll.AddNode (88);
Console.WriteLine(ll.CurrentNode.Value);
ll.GoTo (0);
Console.WriteLine(ll.CurrentNode.Value);


Console.WriteLine("This is how many nodes: " + ll.Count);


Console.WriteLine("*** Program Ends Now ***");
Console.Read();
}


#region Properties
/// <summary>
/// Give back how many nodes there are.
/// </summary>
public int Count
{
get
{
return iCount;
}
}


/// <summary>
/// Gives back the current Node
/// </summary>
public Node CurrentNode
{
get
{
return nCurrent;
}
}


/// <summary>
/// Keeps track of the index where you are
/// </summary>
public int CurrentNodeIndex
{
get
{
return iCurrent;
}
}
#endregion


/// <summary>
/// Default and only Constructor
/// SetUp our LinkedList
/// </summary>
/// <param name="obj">Value for the first Node</param>
public LinkedList(object obj)
{
nCurrent = new Node(null, null, obj);
nCurrent.Next = null;
nCurrent.Previous = null;
}


/// <summary>
/// This function will add another Node
/// </summary>
/// <param name="obj">Value for the added Node</param>
public void AddNode(object obj)
{
if(nCurrent.Next == null)
{
nCurrent = nCurrent.Next = new Node(nCurrent, null, obj);
}
else
{
nCurrent = nCurrent.Next = new Node(nCurrent, nCurrent.Next, obj);
}
iCount++;
iCurrent++;
}


/// <summary>
/// Goes to the next Node
/// </summary>
public void ToNext()
{
// Checks whether the Next Node is null
// if so it throws an exception.
// You can also do nothing.
if(nCurrent.Next == null)
{
throw new Exception("There is no next node!");
}
else // If everything is OK
{
nCurrent = nCurrent.Next;
iCurrent++;
}
}


/// <summary>
/// Goes to the previous Node
/// </summary>
public void ToPrevious()
{
// Look at ToNext();
if(nCurrent.Previous == null)
{
throw new Exception("There is no previous node!");
}
else
{
nCurrent = nCurrent.Previous;
iCurrent--;
}
}


/// <summary>
/// Goes to the index you fill in
/// </summary>
/// <param name="index">Index Where to go?</param>
public void GoTo(int index)
{


while (iCurrent - index != 0)
{
if(iCurrent < index)
{
ToNext();
}
else if(iCurrent > index)
{
ToPrevious();
}
}
}



public class Node
{
protected Node nPrevious;
protected Node nNext;
protected object Object;


#region Properties
public object Value
{
get
{
return Object;
}
set
{
Object = value;
}
}


public Node Previous
{
get
{
return nPrevious;
}
set
{
nPrevious = value;
}
}


public Node Next
{
get
{
return nNext;
}
set
{
nNext = value;
}
}
#endregion


public Node(Node PrevNode, Node NextNode, object obj)
{
nPrevious = PrevNode;
nNext  = NextNode;
Object  = obj;
}
}
}
}