线性表的链式存储结构:n个结点链接成一个链表,每个结点由两部分构成:数据元素本身信息和其直接后
继元素的存储位置。
单链表:一个SingleLinkList类,里面封装所有关于链表的基本操作(方法)和头结点属性;一个LinkNode
类封装了value属性和指向其直接后继元素的(指针)信息。
代码实现:
单链表类
class LinkList
{
private LinkNode head;
public LinkList()
{
head = null;
}
public LinkNode Init()
{
head = new LinkNode();
return head;
}
public bool ListEmpty()
{
if (head.Next == null)
{
return true;
}
return false;
}
public void CleanList()
{
head.Next = null;
}
public int ListLength()
{//computing the length of list but does not include the head node
int count = 0;
LinkNode temp = null;
temp = head.Next;
while (temp != null)
{
count++;
temp = temp.Next;
}
return count;
}
public LinkNode GetElem(int index)
{
int count = 1;
LinkNode temp = null;
temp = head.Next;
while ((count < index) && (temp != null))
{
temp = temp.Next;
count++;
}
if ((temp == null))
return null;
return temp;
}
public int LocationElem(int value)
{
LinkNode temp = null;
int count = 0;
temp = head.Next;
while (temp != null)
{
count++;
if (temp.Value == value)
{
return count;
}
temp = temp.Next;
}
return Convert.ToInt32(EnumTool.FailResultSet.FindNothing);
}
public int InsertElem(int index, int value)
{
int count = 1;
LinkNode temp = null;
LinkNode newNode = null;
temp = head;
while ((count < index) && (temp != null))
{
count++;
temp = temp.Next;
}
if ((temp == null) || (count > index))
{
return Convert.ToInt32(EnumTool.FailResultSet.FindNothing);
}
newNode = new LinkNode();
newNode.Value = value;
newNode.Next = temp.Next;
temp.Next = newNode;
return Convert.ToInt32(EnumTool.FailResultSet.Success);
}
public int DeleteElem(int index)
{
int count = 1;
LinkNode temp = null;
temp = head.Next;
while ((temp != null) && (count < index))
{
count++;
temp = temp.Next;
}
temp = temp.Next.Next;
return Convert.ToInt32(EnumTool.FailResultSet.Success);
}
public void Traverse()
{
LinkNode temp = null;
temp = head.Next;
while (temp != null)
{
Console.Write("{0} ", temp.Value);
temp = temp.Next;
}
Console.WriteLine();
}
public void CreateListTail(int n)
{
Random rd = new Random();
for (int j = 1; j <= n; j++)
{
int val = rd.Next();
InsertElem(j, val);
}
}
public void CreateListHead(int n)
{
Random rd = new Random();
for (int j = 0; j < n; j++)
{
int val = rd.Next();
InsertElem(1, val);
}
}
}
枚举工具类
class EnumTool
{
public enum FailResultSet
{
OverflowEdge = -1024,
FindNothing = -1023,
MoreThanMax = -2048,
Success = -256,
EmptySet = -1000
}
}
测试代码
class Program
{
static void Main(string[] args)
{
int status;
LinkList linkList = new LinkList();
LinkNode head = linkList.Init();
Console.WriteLine("初始化后:ListLength(L)={0}", linkList.ListLength());
for (int j = 1; j <= 5; j++)
status = linkList.InsertElem(1, j);
Console.WriteLine("在list HEAD 插入1~5后:L.data=");
linkList.Traverse();
Console.WriteLine("初始化后:ListLength(L)={0}", linkList.ListLength());
Console.WriteLine("the list is empty? {0}", linkList.ListEmpty());
linkList.CleanList();
Console.WriteLine("the list is empty? {0}", linkList.ListEmpty());
for (int j = 1; j <= 5; j++)
status = linkList.InsertElem(j, j);
Console.WriteLine("在list TAIL 插入1~5后:L.data=");
linkList.Traverse();
//Create a linklist by inserting from head
//Create a linkList by inserting from tail
linkList.InsertElem(1, 0);
Console.Write("insert 0 to list head ,list is :");
linkList.Traverse();
Console.WriteLine("the forth elem of list is {0}", linkList.GetElem(4).Value);
Console.WriteLine("the value 3's location is {0}", linkList.LocationElem(3));
linkList.CleanList();
linkList.CreateListHead(8);
linkList.Traverse();
linkList.CleanList();
linkList.CreateListTail(7);
linkList.Traverse();
Console.ReadKey();
}
}
运行结果: