上文我們討論了一種最簡單的線性結構——順序表,這節我們要討論另一種線性結構——鏈表。
什麼是鏈表了,不要求邏輯上相鄰的資料元素在實體儲存體位置上也相鄰儲存的線性結構稱之為鏈表。舉個現實中的例子吧,假如一個公司召開了視頻會議的吧,能在北京總公司人看到上海分公司的人,他們就好比是邏輯上相鄰的資料元素,而物理上不相連。這樣就好比是個鏈表。 鏈表分為①單鏈表,②單向迴圈鏈表,③雙向鏈表,④雙向迴圈鏈表。
介紹各種各樣鏈表之前,我們要明白這樣一個概念。什麼是結點。在儲存資料元素時,除了儲存資料元素本身的資訊外,還要儲存與它相鄰的資料元素的儲存地址資訊。這兩部分資訊組成該資料元素的儲存映像(Image),稱為結點(Node)。在c語言這些面向過程語言中,實現節點是通過指標的形式,在。net中,是通過類比指標——類對象嵌套的形式。
然後,首先,介紹單鏈表。如果結點的參考網域只儲存該結點直接後繼結點的儲存地址, 則該鏈表叫單鏈表(Singly Linked List)。把該參考網域叫 next。單鏈表結點的結構,圖中 data表示結點的資料域。
現實中,就像一隊盲人過馬路。
把單鏈表結點看作是一個類,類名為 Node<T>。單鏈表結點類的實現如下所示。
複製代碼 代碼如下: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;
next = null;
}
//構造器
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;
}
}
}
是線性表(a1,a2,a3,a4,a5,a6)對應的鏈式儲存結構。
單鏈表類 LinkList<T>原始碼的實現說明如下所示。首先申明一下,他繼承與IListDS這個介面。
//這是一個盲人過馬路的類的類比
public class LinkList<T> : IListDS<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;
不懂的一切盡在圖例中
這個方法的演算法複雜度是O(n)
}
//就是在一隊中增加了插隊的人員
//在單鏈表的第i個結點的位置前插入一個值為item的結點
public void Insert(T item, int i)
{
if (IsEmpty() | | i < 1)
{
Console.WriteLine("List is empty or Position is error!");
return;
}
//是頭結點的位置,就把他的頭執政指向與他,把另外指標與他
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;
}
一切盡在圖例中
}
這個方法的演算法複雜度O(n)
//刪除單鏈表的第i個結點
public T Delete(int i)
{
//是不是盲人排隊的 或者排隊的位置不是正確的 這就返回了一個錯誤資訊
if (IsEmpty()|| i < 0)
{
Console.WriteLine("Link is empty or Position is
error!");
return default(T);
}
//是頭結點的 的就返回 第二個節點頂到第一個節點的位置
Node<T> q = new Node<T>();
if (i == 1)
{
q = head;
head = head.Next;
return q.Data;
}
此步驟為O(1)
//不是的頭位置的吧,就尋找相應位置的節點,在進行刪除。他這個排隊前面的人指向後面的人。這就是新的隊伍了 沒找到,就返回錯誤。
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
{
Console.WriteLine("The ith node is not exist!");
return default(T);
}
不懂的:
此方法的已耗用時間複雜度是O(n)
}
//獲得單鏈表的第i個資料元素
//知道隊伍 我要查詢出隊伍中第n個人是那位,
public T GetElem(int i)
{
//如果是空的就返回為錯誤的結果
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return default(T);
}
//從圖接點數 第n個的結果了
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
{
Console.WriteLine("The ith node is not exist!");
return default(T);
}
}
不懂的,一切盡在圖例中。
此方法的時間的複雜度是O(n)
//我要查詢張山的位於隊伍的第幾個位置
//在單鏈表中尋找值為value的結點
public int Locate(T value)
{
//空返回為假的的
if(IsEmpty())
{
Console.WriteLine("List is Empty!");
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;
這個演算法複雜度是O(n2)
}
}
這節我們討論鏈表的基本操作,並且畫圖以證明,下屆中我們將討論雙向鏈表,環形鏈表 應用舉例。