Double-linked list each data node has two pointers, pointing to its successor and the predecessor node respectively. It is more flexible than a single linked list can only access its successor node, and of course it takes up more storage space.
The previous single linked list and the double linked list here use empty header or dummy nodes, which is more convenient to realize ordered linked list.
Look directly at the code:
/*
* File:DoubleLinkedList.cs
* Author:zhenxing Zhou
* date:2008-12-06, 2008-12-07
* blog:http://www.xianfen.net/
*/
Using System;
Using System.Collections.Generic;
Namespace Xianfen.Net.DataStructure
{
public class Doublelinkedlistnode<t>
{
Private T m_value;
Private doublelinkedlistnode<t> M_next;
Private doublelinkedlistnode<t> M_prior;
Public T Value
{
get {return m_value;}
set {m_value = Value;}
}
Public doublelinkedlistnode<t> Next
{
get {return m_next;}
set {M_next = value;}
}
Public doublelinkedlistnode<t> Prior
{
get {return m_prior;}
set {M_prior = value;}
}
Public Doublelinkedlistnode ()
{
}
Public Doublelinkedlistnode (t)
{
m_value = t;
}
}
public class Doublelinkedlist<t>: ilinearlist<t>
{
protected int m_count;
protected doublelinkedlistnode<t> M_head;
protected doublelinkedlistnode<t> M_tail;
Public Doublelinkedlist ()
{
M_count = 0;
M_head = new doublelinkedlistnode<t> ();
M_tail = M_head;
}
Public Doublelinkedlist (t)
: This ()
{
M_count = 1;
M_head.next = new doublelinkedlistnode<t> (T);
M_tail = M_head.next;
M_tail.prior = M_head;
}
public void Add (t)
{
InsertAt (t, M_count);
}
public void AddHead (T-t)
{
InsertAt (t, 0);
}
public void AddTail (T-t)
{
ADD (t);
}
public void Clear ()
{
M_count = 0;
M_tail = M_head;
M_head.next = null;
M_head.prior = null;
}
public int Count
{
get {return m_count;}
}
public int find (T t)
{
doublelinkedlistnode<t> CurrentNode = M_head;
int pos = 0;
while ((CurrentNode = currentnode.next)!= null)
{
if (CurrentNode.Value.Equals (t))
{
return POS;
}
pos++;
}
return-1;
}
Public T GetAt (int pos)
{
Return GetNodeAt (POS). Value;
}
Public T GetHead ()
{
Return getnodeat (0). Value;
}
Public T GetTail ()
{
return m_tail.value;
}
public void InsertAt (T t, int POS)
{
if (pos > M_count | | pos < 0)
{
throw New IndexOutOfRangeException ("POS");
}
if (m_count = Int. MaxValue)
{
throw new ArithmeticException ();
}
doublelinkedlistnode<t> Insertnode = new doublelinkedlistnode<t> (T);
if (pos = = M_count)
{
Insertnode.prior = M_tail;
M_tail.next = Insertnode;
M_tail = Insertnode;
m_count++;
Return
}
Doublelinkedlistnode<t> CurrentNode = GetNodeAt (POS);
Insertnode.prior = Currentnode.prior;
Insertnode.next = CurrentNode;
CurrentNode.Prior.Next = Insertnode;
Currentnode.prior = Insertnode;
m_count++;
}
Private doublelinkedlistnode<t> getnodeat (int pos)
{
if (pos >= m_count | | pos < 0)
{
throw New IndexOutOfRangeException ("POS");
}
Doublelinkedlistnode<t> currentnode = null;
Nearest way to find the node in the POS location
if (pos > M_COUNT/2)
{
CurrentNode = M_tail;
pos = m_count-pos-1;
while (pos--> 0)
{
CurrentNode = Currentnode.prior;
}
}
Else
{
CurrentNode = M_head.next;
while (pos--> 0)
{
CurrentNode = Currentnode.next;
}
}
return currentnode;
}
public bool IsEmpty
{
get {return m_count = = 0;}
}
public void RemoveAll ()
{
Clear ();
}
public void RemoveAt (int pos)
{
if (pos = = m_count-1)
{
M_tail = M_tail.prior;
M_tail.next = null;
m_count--;
Return
}
Doublelinkedlistnode<t> CurrentNode = GetNodeAt (POS);
CurrentNode.Prior.Next = Currentnode.next;
CurrentNode.Next.Prior = Currentnode.prior;
m_count--;
}
public void RemoveHead ()
{
RemoveAt (0);
}
public void RemoveTail ()
{
RemoveAt (m_count-1);
}
public void SetAt (int pos, T t)
{
GetNodeAt (POS). Value = t;
}
Public ienumerator<t> GetEnumerator ()
{
doublelinkedlistnode<t> CurrentNode = M_head;
while ((CurrentNode = currentnode.next)!= null)
{
Yield return currentnode.value;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
}
}
If you find a bug please leave a message stating that the next article introduces the implementation of the circular chain list.