@import url("http://www.blogger.com/css/blog_controls.css"); @import url("http://www.blogger.com/dyn-css/authorization.css?blogID=8706105"); Hardik Shah - Trying to be with the Technology
Networks

Monday, November 29, 2004

Linked List using C#.NET

Moving away from the college syllabus. Tried to Implement a linked list in C#.NET. A small code snippet on this..

public class List
{
private ListNode m_FirstNode;
private ListNode m_LastNode;
private string m_strListName;
private int m_intListCount;

// Construct empty list with specified name
public List(string strListname)
{
m_strListName = strListname;
m_intListCount = 0;
m_FirstNode = m_LastNode = null;
}

// Construct empty List with "List" as default name
public List() : this("List")
{
}

// IsEmpty Property Check if the list is Empty, returns true if list is Empty
public bool IsEmpty
{
get
{
lock(this)
{
return m_FirstNode == null;
}
}
}

// return number of elemets in List
public int Count
{
get
{
return m_intListCount;
}
}

// return first element in List
public ListNode First
{
get
{
return m_FirstNode;
}
}

// return Last element in List
public ListNode Last
{
get
{
return m_LastNode;
}
}

// Insert Object at front of list. If list is empty, first node
// & last node will refer to same object.
// Otherwise, first node refers to new node
public void InsertAtFront(object objItem)
{
lock(this)
{
if(IsEmpty)
m_FirstNode = m_LastNode = new ListNode(objItem);
else
m_FirstNode = new ListNode(objItem,m_FirstNode);
m_intListCount++;
}
}

// Insert Object at end of list. If list is empty, first node & last node will refer to same object.
//Otherwise, last node refers to new node
public void InsertAtBack(object objItem)
{
lock(this)
{
if(IsEmpty)
m_FirstNode = m_LastNode = new ListNode(objItem);
else
m_LastNode = m_LastNode.Next = new ListNode(objItem);
m_intListCount++;
}
}


// Insert and object in specific place in list
public void InsertAt(object objItem,int Index)
{
lock(this)
{
if(Index > m_intListCount || Index < 0)
throw new OutOfRangeException(Index.ToString());
if(Index ==0)
InsertAtFront(objItem);
else if(Index == (m_intListCount-1))
InsertAtBack(objItem);
else
{
ListNode currentNode = m_FirstNode;
for(int i=0; i<Index; i++)
{
currentNode = currentNode.Next;
}
ListNode newNode = new ListNode(objItem,currentNode.Next);
currentNode.Next = newNode;
m_intListCount++;
}
}
}



//reset first node and last node references
if(m_FirstNode == m_LastNode)
m_FirstNode = m_LastNode = null;
else
m_FirstNode = m_FirstNode.Next;
m_intListCount--;
return removeItem;
}
}

//remove last node from list
public object RemoveFromBack()
{
lock(this)
{
if(IsEmpty)
throw new EmptyListException(m_strListName);
object removeItem = m_LastNode.Data;
//reset first node and last node references
if(m_FirstNode == m_LastNode)
m_FirstNode = m_LastNode = null;
else
{
ListNode currentNode = m_FirstNode;
while(currentNode.Next != m_LastNode)
currentNode = currentNode.Next;
m_LastNode = currentNode;
currentNode.Next = null;
}
m_intListCount--;
return removeItem;
}
}
}


Refernence:
Data Structures using .NET : Leech

0 Comments:

Post a Comment

<< Home

Google
 
Web hardiks.blogspot.com