线性表的链存储称为链表。特点:存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素
的数据域,另一个是存储下一个结点地址的指针域。
链表有单链表,双向链表等
下面介绍单链表的实现方法:
public
class
Node
<
T
>
{
private T data;
private Node < T > next;
public Node(T val, Node < T > p)
{
data = val;
next = p;
}
public Node(Node < T > p)
{
next = p;
}
public Node(T val)
{
data = val;
}
public Node()
{
data = default (T);
next = null ;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node < T > Next
{
get { return next; }
set { next = value; }
}
}
public class LinkList < T >
{
private Node < T > head;
public Node < T > Head
{
get { return head; }
set { head = value; }
}
public LinkList()
{
head = null ;
}
public int GetLength()
{
Node < T > p = head;
int len = 0 ;
while (p != null )
{
++ len;
p = p.Next;
}
return len;
}
public void clear()
{
head = null ;
}
public bool isEmpty()
{
if (head == null )
return true ;
else
return false ;
}
public void Append(T item)
{
Node < T > q = new Node < T > (item);
Node < T > p = new Node < T > ();
if (head == null )
{
head = q;
return ;
}
p = head;
while (p.Next != null )
{
p = p.Next; // 把位置定义在最后
}
p.Next = q; // 添加元素
}
public void Insert(T item, int i)
{
if (isEmpty() || i < 1 )
{
throw new Exception( " Insert: Error! " );
}
if (i == 1 )
{
Node < T > q = new Node < T > (item);
q.Next = head;
head = q;
return ;
}
Node < T > p = head;
Node < T > r = new Node < T > ();
int j = 1 ;
while (p.Next != null && j < i)
{
r = p;
p = p.Next;
++ j;
}
if (j == i)
{
Node < T > q = new Node < T > (item);
q.Next = p;
r.Next = q;
}
else
{
throw new Exception( " Insert: Error " );
}
}
public T Delete( int i)
{
if (isEmpty() && i < 0 )
{
throw new Exception( " Error " );
}
Node < T > q = new Node < T > ();
if (i == 1 )
{
q = head;
head = head.Next;
return q.Data;
}
Node < T > p = head;
int j = 1 ;
while (p.Next != null && j < i)
{
++ j;
q = p;
p = p.Next;
}
if (j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
return default (T);
}
}
public T GetElem( int i)
{
if (isEmpty())
{
return default (T);
}
Node < T > p = new Node < T > ();
p = head;
int j = 1 ;
while (p.Next != null && j < i)
{
++ j;
p = p.Next;
}
if (j == i)
{
return p.Data;
}
else
{
return default (T);
}
}
public int Locate(T value)
{
if (isEmpty())
{
return - 1 ;
}
Node < T > p = new Node < T > ();
p = head;
int i = 1 ;
while ( ! p.Data.Equals(value) && p.Next != null )
{
p = p.Next;
++ i;
}
return i;
}
}
{
private T data;
private Node < T > next;
public Node(T val, Node < T > p)
{
data = val;
next = p;
}
public Node(Node < T > p)
{
next = p;
}
public Node(T val)
{
data = val;
}
public Node()
{
data = default (T);
next = null ;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node < T > Next
{
get { return next; }
set { next = value; }
}
}
public class LinkList < T >
{
private Node < T > head;
public Node < T > Head
{
get { return head; }
set { head = value; }
}
public LinkList()
{
head = null ;
}
public int GetLength()
{
Node < T > p = head;
int len = 0 ;
while (p != null )
{
++ len;
p = p.Next;
}
return len;
}
public void clear()
{
head = null ;
}
public bool isEmpty()
{
if (head == null )
return true ;
else
return false ;
}
public void Append(T item)
{
Node < T > q = new Node < T > (item);
Node < T > p = new Node < T > ();
if (head == null )
{
head = q;
return ;
}
p = head;
while (p.Next != null )
{
p = p.Next; // 把位置定义在最后
}
p.Next = q; // 添加元素
}
public void Insert(T item, int i)
{
if (isEmpty() || i < 1 )
{
throw new Exception( " Insert: Error! " );
}
if (i == 1 )
{
Node < T > q = new Node < T > (item);
q.Next = head;
head = q;
return ;
}
Node < T > p = head;
Node < T > r = new Node < T > ();
int j = 1 ;
while (p.Next != null && j < i)
{
r = p;
p = p.Next;
++ j;
}
if (j == i)
{
Node < T > q = new Node < T > (item);
q.Next = p;
r.Next = q;
}
else
{
throw new Exception( " Insert: Error " );
}
}
public T Delete( int i)
{
if (isEmpty() && i < 0 )
{
throw new Exception( " Error " );
}
Node < T > q = new Node < T > ();
if (i == 1 )
{
q = head;
head = head.Next;
return q.Data;
}
Node < T > p = head;
int j = 1 ;
while (p.Next != null && j < i)
{
++ j;
q = p;
p = p.Next;
}
if (j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
return default (T);
}
}
public T GetElem( int i)
{
if (isEmpty())
{
return default (T);
}
Node < T > p = new Node < T > ();
p = head;
int j = 1 ;
while (p.Next != null && j < i)
{
++ j;
p = p.Next;
}
if (j == i)
{
return p.Data;
}
else
{
return default (T);
}
}
public int Locate(T value)
{
if (isEmpty())
{
return - 1 ;
}
Node < T > p = new Node < T > ();
p = head;
int i = 1 ;
while ( ! p.Data.Equals(value) && p.Next != null )
{
p = p.Next;
++ i;
}
return i;
}
}
下面代码来使用链表
private
void
button1_Click(
object
sender, EventArgs e)
{
try
{
LinkList < string > str = new LinkList < string > ();
str.Head = new Node < string > ( " A " );
str.Append( " B " );
str.Append( " D " );
str.Append( " E " );
str.Insert( " C " , 5 );
richTextBox1.Text = str.GetElem( 1 ) + str.GetElem( 2 ) + str.GetElem( 3 ) + str.GetElem( 4 ) + str.GetElem( 5 );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
{
try
{
LinkList < string > str = new LinkList < string > ();
str.Head = new Node < string > ( " A " );
str.Append( " B " );
str.Append( " D " );
str.Append( " E " );
str.Insert( " C " , 5 );
richTextBox1.Text = str.GetElem( 1 ) + str.GetElem( 2 ) + str.GetElem( 3 ) + str.GetElem( 4 ) + str.GetElem( 5 );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}