上一篇跟大家聊過“線性表"順序存儲,通過實驗,大家也知道,如果我每次向順序表的頭部插入元素,都會引起痙攣,效率比較低下,第二點我們用順序存儲時,容易受到長度的限制,反之就會造成空間資源的浪費。
1. 概念:其實鏈表的“每個節(jié)點”都包含一個”數(shù)據(jù)域“和”指針域“。
前面已經(jīng)說過,鏈表是采用指針來指向下一個元素,所以說要想找到鏈表最后一個節(jié)點,必須從頭指針開始一步一步向后找,少不了一個for循環(huán),所以時間復雜度為O(N)。
#region 將節(jié)點添加到鏈表的末尾
/// summary>
/// 將節(jié)點添加到鏈表的末尾
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListAddEndT>(NodeT> head, T data)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = null;
///說明是一個空鏈表
if (head == null)
{
head = node;
return head;
}
//獲取當前鏈表的最后一個節(jié)點
ChainListGetLast(head).next = node;
return head;
}
#endregion
#region 得到當前鏈表的最后一個節(jié)點
/// summary>
/// 得到當前鏈表的最后一個節(jié)點
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// returns>/returns>
public NodeT> ChainListGetLastT>(NodeT> head)
{
if (head.next == null)
return head;
return ChainListGetLast(head.next);
}
#endregion
1#region 將節(jié)點添加到鏈表的開頭
/// summary>
/// 將節(jié)點添加到鏈表的開頭
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="chainList">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListAddFirstT>(NodeT> head, T data)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = head;
head = node;
return head;
}
#endregion
#region 將節(jié)點插入到指定位置
/// summary>
/// 將節(jié)點插入到指定位置
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// param name="currentNode">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListInsertT, W>(NodeT> head, string key, FuncT, W> where, T data) where W : IComparable
{
if (head == null)
return null;
if (where(head.data).CompareTo(key) == 0)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = head.next;
head.next = node;
}
ChainListInsert(head.next, key, where, data);
return head;
}
#endregion
#region 將指定關鍵字的節(jié)點刪除
/// summary>
/// 將指定關鍵字的節(jié)點刪除
/// /summary>
/// typeparam name="T">/typeparam>
/// typeparam name="W">/typeparam>
/// param name="head">/param>
/// param name="key">/param>
/// param name="where">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListDeleteT, W>(NodeT> head, string key, FuncT, W> where) where W : IComparable
{
if (head == null)
return null;
//這是針對只有一個節(jié)點的解決方案
if (where(head.data).CompareTo(key) == 0)
{
if (head.next != null)
head = head.next;
else
return head = null;
}
else
{
//判斷一下此節(jié)點是否是要刪除的節(jié)點的前一節(jié)點
while (head.next != null where(head.next.data).CompareTo(key) == 0)
{
//將刪除節(jié)點的next域指向前一節(jié)點
head.next = head.next.next;
}
}
ChainListDelete(head.next, key, where);
return head;
}
#endregion
#region 通過關鍵字查找指定的節(jié)點
/// summary>
/// 通過關鍵字查找指定的節(jié)點
/// /summary>
/// typeparam name="T">/typeparam>
/// typeparam name="W">/typeparam>
/// param name="head">/param>
/// param name="key">/param>
/// param name="where">/param>
/// returns>/returns>
public NodeT> ChainListFindByKeyT, W>(NodeT> head, string key, FuncT, W> where) where W : IComparable
{
if (head == null)
return null;
if (where(head.data).CompareTo(key) == 0)
return head;
return ChainListFindByKeyT, W>(head.next, key, where);
}
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChainList
{
class Program
{
static void Main(string[] args)
{
ChainList chainList = new ChainList();
NodeStudent> node = null;
Console.WriteLine("將三條數(shù)據(jù)添加到鏈表的尾部:\n");
//將數(shù)據(jù)添加到鏈表的尾部
node = chainList.ChainListAddEnd(node, new Student() { ID = 2, Name = "hxc520", Age = 23 });
node = chainList.ChainListAddEnd(node, new Student() { ID = 3, Name = "博客園", Age = 33 });
node = chainList.ChainListAddEnd(node, new Student() { ID = 5, Name = "一線碼農(nóng)", Age = 23 });
Dispaly(node);
Console.WriteLine("將ID=1的數(shù)據(jù)插入到鏈表開頭:\n");
//將ID=1的數(shù)據(jù)插入到鏈表開頭
node = chainList.ChainListAddFirst(node, new Student() { ID = 1, Name = "i can fly", Age = 23 });
Dispaly(node);
Console.WriteLine("查找Name=“一線碼農(nóng)”的節(jié)點\n");
//查找Name=“一線碼農(nóng)”的節(jié)點
var result = chainList.ChainListFindByKey(node, "一線碼農(nóng)", i => i.Name);
DisplaySingle(node);
Console.WriteLine("將”ID=4“的實體插入到“博客園”這個節(jié)點的之后\n");
//將”ID=4“的實體插入到"博客園"這個節(jié)點的之后
node = chainList.ChainListInsert(node, "博客園", i => i.Name, new Student() { ID = 4, Name = "51cto", Age = 30 });
Dispaly(node);
Console.WriteLine("刪除Name=‘51cto‘的節(jié)點數(shù)據(jù)\n");
//刪除Name=‘51cto‘的節(jié)點數(shù)據(jù)
node = chainList.ChainListDelete(node, "51cto", i => i.Name);
Dispaly(node);
Console.WriteLine("獲取鏈表的個數(shù):" + chainList.ChanListLength(node));
}
//輸出數(shù)據(jù)
public static void Dispaly(NodeStudent> head)
{
Console.WriteLine("******************* 鏈表數(shù)據(jù)如下 *******************");
var tempNode = head;
while (tempNode != null)
{
Console.WriteLine("ID:" + tempNode.data.ID + ", Name:" + tempNode.data.Name + ",Age:" + tempNode.data.Age);
tempNode = tempNode.next;
}
Console.WriteLine("******************* 鏈表數(shù)據(jù)展示完畢 *******************\n");
}
//展示當前節(jié)點數(shù)據(jù)
public static void DisplaySingle(NodeStudent> head)
{
if (head != null)
Console.WriteLine("ID:" + head.data.ID + ", Name:" + head.data.Name + ",Age:" + head.data.Age);
else
Console.WriteLine("未查找到數(shù)據(jù)!");
}
}
#region 學生數(shù)據(jù)實體
/// summary>
/// 學生數(shù)據(jù)實體
/// /summary>
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
#endregion
#region 鏈表節(jié)點的數(shù)據(jù)結構
/// summary>
/// 鏈表節(jié)點的數(shù)據(jù)結構
/// /summary>
public class NodeT>
{
/// summary>
/// 節(jié)點的數(shù)據(jù)域
/// /summary>
public T data;
/// summary>
/// 節(jié)點的指針域
/// /summary>
public NodeT> next;
}
#endregion
#region 鏈表的相關操作
/// summary>
/// 鏈表的相關操作
/// /summary>
public class ChainList
{
#region 將節(jié)點添加到鏈表的末尾
/// summary>
/// 將節(jié)點添加到鏈表的末尾
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListAddEndT>(NodeT> head, T data)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = null;
///說明是一個空鏈表
if (head == null)
{
head = node;
return head;
}
//獲取當前鏈表的最后一個節(jié)點
ChainListGetLast(head).next = node;
return head;
}
#endregion
#region 將節(jié)點添加到鏈表的開頭
/// summary>
/// 將節(jié)點添加到鏈表的開頭
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="chainList">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListAddFirstT>(NodeT> head, T data)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = head;
head = node;
return head;
}
#endregion
#region 將節(jié)點插入到指定位置
/// summary>
/// 將節(jié)點插入到指定位置
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// param name="currentNode">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListInsertT, W>(NodeT> head, string key, FuncT, W> where, T data) where W : IComparable
{
if (head == null)
return null;
if (where(head.data).CompareTo(key) == 0)
{
NodeT> node = new NodeT>();
node.data = data;
node.next = head.next;
head.next = node;
}
ChainListInsert(head.next, key, where, data);
return head;
}
#endregion
#region 將指定關鍵字的節(jié)點刪除
/// summary>
/// 將指定關鍵字的節(jié)點刪除
/// /summary>
/// typeparam name="T">/typeparam>
/// typeparam name="W">/typeparam>
/// param name="head">/param>
/// param name="key">/param>
/// param name="where">/param>
/// param name="data">/param>
/// returns>/returns>
public NodeT> ChainListDeleteT, W>(NodeT> head, string key, FuncT, W> where) where W : IComparable
{
if (head == null)
return null;
//這是針對只有一個節(jié)點的解決方案
if (where(head.data).CompareTo(key) == 0)
{
if (head.next != null)
head = head.next;
else
return head = null;
}
else
{
//判斷一下此節(jié)點是否是要刪除的節(jié)點的前一節(jié)點
if (head.next != null where(head.next.data).CompareTo(key) == 0)
{
//將刪除節(jié)點的next域指向前一節(jié)點
head.next = head.next.next;
}
}
ChainListDelete(head.next, key, where);
return head;
}
#endregion
#region 通過關鍵字查找指定的節(jié)點
/// summary>
/// 通過關鍵字查找指定的節(jié)點
/// /summary>
/// typeparam name="T">/typeparam>
/// typeparam name="W">/typeparam>
/// param name="head">/param>
/// param name="key">/param>
/// param name="where">/param>
/// returns>/returns>
public NodeT> ChainListFindByKeyT, W>(NodeT> head, string key, FuncT, W> where) where W : IComparable
{
if (head == null)
return null;
if (where(head.data).CompareTo(key) == 0)
return head;
return ChainListFindByKeyT, W>(head.next, key, where);
}
#endregion
#region 獲取鏈表的長度
/// summary>
///// 獲取鏈表的長度
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// returns>/returns>
public int ChanListLengthT>(NodeT> head)
{
int count = 0;
while (head != null)
{
++count;
head = head.next;
}
return count;
}
#endregion
#region 得到當前鏈表的最后一個節(jié)點
/// summary>
/// 得到當前鏈表的最后一個節(jié)點
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="head">/param>
/// returns>/returns>
public NodeT> ChainListGetLastT>(NodeT> head)
{
if (head.next == null)
return head;
return ChainListGetLast(head.next);
}
#endregion
}
#endregion
}
當然,單鏈表操作中有很多是O(N)的操作,這給我們帶來了尷尬的局面,所以就有了很多的優(yōu)化方案,比如:雙向鏈表,循環(huán)鏈表。靜態(tài)鏈表等等,這些希望大家在懂得單鏈表的情況下待深一步的研究。