算法系列15天速成——第八天 線性表【下】

算法系列15天速成——第八天 線性表【下】


轉自:http://blog.csdn.net/m13666368773/article/details/7516488

一:線性表的簡單回顧

       上一篇跟大家聊過“線性表"順序存儲,通過實驗,大家也知道,如果我每次向

順序表的頭部插入元素,都會引起痙攣,效率比較低下,第二點我們用順序存儲時,容

易受到長度的限制,反之就會造成空間資源的浪費。

 

二:鏈表

      對於順序表存在的若干問題,鏈表都給出了相應的解決方案。

1. 概念:其實鏈表的“每個節點”都包含一個”數據域“和”指針域“。

            ”數據域“中包含當前的數據。

            ”指針域“中包含下一個節點的指針。

            ”頭指針”也就是head,指向頭結點數據。

            “末節點“作爲單向鏈表,因爲是最後一個節點,通常設置指針域爲null。

代碼段如下:

複製代碼
[csharp] view plain copy
  1. #region 鏈表節點的數據結構  
  2.  /// <summary>  
  3.  /// 鏈表節點的數據結構  
  4.  /// </summary>  
  5.      public class Node<T>  
  6.      {  
  7. /// <summary>  
  8.  /// 節點的數據域  
  9.  /// </summary>  
  10.          public T data;  
  11.    
  12.  /// <summary>  
  13.  /// 節點的指針域  
  14.  /// </summary>  
  15.          public Node<T> next;  
  16.      }  
  17.      #endregion  


2.常用操作:

    鏈表的常用操作一般有:

           ①添加節點到鏈接尾,②添加節點到鏈表頭,③插入節點。

           ④刪除節點,⑤按關鍵字查找節點,⑥取鏈表長度。

   

<1> 添加節點到鏈接尾:

          前面已經說過,鏈表是採用指針來指向下一個元素,所以說要想找到鏈表最後一個節點,

       必須從頭指針開始一步一步向後找,少不了一個for循環,所以時間複雜度爲O(N)。

 

代碼段如下:

複製代碼
[csharp] view plain copy
  1. #region 將節點添加到鏈表的末尾  
  2.          /// <summary>  
  3.  /// 將節點添加到鏈表的末尾  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <param name="head"></param>  
  7.  /// <param name="data"></param>  
  8.  /// <returns></returns>  
  9.          public Node<T> ChainListAddEnd<T>(Node<T> head, T data)  
  10.          {  
  11.              Node<T> node = new Node<T>();  
  12.    
  13.              node.data = data;  
  14.              node.next = null;  
  15.    
  16.              ///說明是一個空鏈表  
  17.              if (head == null)  
  18.              {  
  19.                  head = node;  
  20.                  return head;  
  21.              }  
  22.    
  23.              //獲取當前鏈表的最後一個節點  
  24.              ChainListGetLast(head).next = node;  
  25.    
  26.              return head;  
  27.          }  
  28.  #endregion  
  29.  #region 得到當前鏈表的最後一個節點  
  30.          /// <summary>  
  31.  /// 得到當前鏈表的最後一個節點  
  32.  /// </summary>  
  33.  /// <typeparam name="T"></typeparam>  
  34.  /// <param name="head"></param>  
  35.  /// <returns></returns>  
  36.          public Node<T> ChainListGetLast<T>(Node<T> head)  
  37.          {  
  38.              if (head.next == null)  
  39.                  return head;  
  40.              return ChainListGetLast(head.next);  
  41.          }  
  42.          #endregion  

 

<2> 添加節點到鏈表頭:

          大家現在都知道,鏈表是採用指針指向的,要想將元素插入鏈表頭,其實還是很簡單的,

      思想就是:① 將head的next指針給新增節點的next。②將整個新增節點給head的next。

      所以可以看出,此種添加的時間複雜度爲O(1)。

 

效果圖:

代碼段如下:

複製代碼
[csharp] view plain copy
  1. #region 將節點添加到鏈表的開頭  
  2.  /// <summary>  
  3.  /// 將節點添加到鏈表的開頭  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <param name="chainList"></param>  
  7.  /// <param name="data"></param>  
  8.  /// <returns></returns>  
  9.          public Node<T> ChainListAddFirst<T>(Node<T> head, T data)  
  10.          {  
  11.              Node<T> node = new Node<T>();  
  12.    
  13.              node.data = data;  
  14.              node.next = head;  
  15.    
  16.              head = node;  
  17.    
  18.              return head;  
  19.    
  20.          }  
  21.          #endregion  


<3> 插入節點:

           其實這個思想跟插入到”首節點“是一個模式,不過多了一步就是要找到當前節點的操作。然後找到

      這個節點的花費是O(N)。上圖上代碼,大家一看就明白。

 

效果圖:

代碼段:

複製代碼
[csharp] view plain copy
  1. #region 將節點插入到指定位置  
  2.  /// <summary>  
  3.  /// 將節點插入到指定位置  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <param name="head"></param>  
  7.  /// <param name="currentNode"></param>  
  8.  /// <param name="data"></param>  
  9.  /// <returns></returns>  
  10.          public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable  
  11.          {  
  12.              if (head == null)  
  13.                  return null;  
  14.    
  15.              if (where(head.data).CompareTo(key) == 0)  
  16.              {  
  17.                  Node<T> node = new Node<T>();  
  18.    
  19.                  node.data = data;  
  20.    
  21.                  node.next = head.next;  
  22.    
  23.                  head.next = node;  
  24.              }  
  25.    
  26.              ChainListInsert(head.next, key, where, data);  
  27.    
  28.              return head;  
  29.          }  
  30.          #endregion  

 

<4> 刪除節點:

        這個也比較簡單,不解釋,圖跟代碼更具有說服力,口頭表達反而讓人一頭霧水。

        當然時間複雜度就爲O(N),N是來自於查找到要刪除的節點。

 

效果圖:

代碼段:

複製代碼
[csharp] view plain copy
  1. #region 將指定關鍵字的節點刪除  
  2.          /// <summary>  
  3.  /// 將指定關鍵字的節點刪除  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <typeparam name="W"></typeparam>  
  7.  /// <param name="head"></param>  
  8.  /// <param name="key"></param>  
  9.  /// <param name="where"></param>  
  10.  /// <param name="data"></param>  
  11.  /// <returns></returns>  
  12.          public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable  
  13.          {  
  14.              if (head == null)  
  15.                  return null;  
  16.    
  17.              //這是針對只有一個節點的解決方案  
  18.              if (where(head.data).CompareTo(key) == 0)  
  19.              {  
  20.                  if (head.next != null)  
  21.                      head = head.next;  
  22.                  else  
  23.                      return head = null;  
  24.              }  
  25.              else  
  26.              {  
  27.                  //判斷一下此節點是否是要刪除的節點的前一節點  
  28.                  while (head.next != null && where(head.next.data).CompareTo(key) == 0)  
  29.                  {  
  30.                      //將刪除節點的next域指向前一節點  
  31.                      head.next = head.next.next;  
  32.                  }  
  33.              }  
  34.    
  35.              ChainListDelete(head.next, key, where);  
  36.    
  37.              return head;  
  38.          }  
  39.          #endregion  

 

<5> 按關鍵字查找節點:

         這個思想已經包含到“插入節點”和“刪除節點”的具體運用中的,其時間複雜度爲O(N)。

 

代碼段:

複製代碼
[csharp] view plain copy
  1. #region 通過關鍵字查找指定的節點  
  2.          /// <summary>  
  3.  /// 通過關鍵字查找指定的節點  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <typeparam name="W"></typeparam>  
  7.  /// <param name="head"></param>  
  8.  /// <param name="key"></param>  
  9.  /// <param name="where"></param>  
  10.  /// <returns></returns>  
  11.          public Node<T> ChainListFindByKey<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable  
  12.          {  
  13.              if (head == null)  
  14.                  return null;  
  15.    
  16.              if (where(head.data).CompareTo(key) == 0)  
  17.                  return head;  
  18.    
  19.              return ChainListFindByKey<T, W>(head.next, key, where);  
  20.          }  
  21.          #endregion  


<6> 取鏈表長度:

          在單鏈表的操作中,取鏈表長度還是比較糾結的,因爲他不像順序表那樣是在內存中連續存儲的,

      因此我們就糾結的遍歷一下鏈表的總長度。時間複雜度爲O(N)。

 

代碼段:

複製代碼
 
[csharp] view plain copy
  1. #region 獲取鏈表的長度  
  2.          /// <summary>  
  3.  ///// 獲取鏈表的長度  
  4.  /// </summary>  
  5.  /// <typeparam name="T"></typeparam>  
  6.  /// <param name="head"></param>  
  7.  /// <returns></returns>  
  8.          public int ChanListLength<T>(Node<T> head)  
  9.          {  
  10.              int count = 0;  
  11.    
  12.              while (head != null)  
  13.              {  
  14.                  ++count;  
  15.                  head = head.next;  
  16.              }  
  17.    
  18.              return count;  
  19.          }  
  20.          #endregion  

 

 

好了,最後上一下總的運行代碼:

[csharp] view plain copy
  1. View Code   
  2.  using System;  
  3.  using System.Collections.Generic;  
  4.  using System.Linq;  
  5.  using System.Text;  
  6.    
  7.  namespace ChainList  
  8.  {  
  9.      class Program  
  10.      {  
  11.          static void Main(string[] args)  
  12.          {  
  13.              ChainList chainList = new ChainList();  
  14.    
  15.              Node<Student> node = null;  
  16.    
  17.              Console.WriteLine("將三條數據添加到鏈表的尾部:\n");  
  18.    
  19.              //將數據添加到鏈表的尾部  
  20.              node = chainList.ChainListAddEnd(node, new Student() { ID = 2, Name = "hxc520", Age = 23 });  
  21.              node = chainList.ChainListAddEnd(node, new Student() { ID = 3, Name = "博客園", Age = 33 });  
  22.              node = chainList.ChainListAddEnd(node, new Student() { ID = 5, Name = "一線碼農", Age = 23 });  
  23.    
  24.              Dispaly(node);  
  25.    
  26.              Console.WriteLine("將ID=1的數據插入到鏈表開頭:\n");  
  27.    
  28.              //將ID=1的數據插入到鏈表開頭  
  29.              node = chainList.ChainListAddFirst(node, new Student() { ID = 1, Name = "i can fly", Age = 23 });  
  30.    
  31.              Dispaly(node);  
  32.    
  33.              Console.WriteLine("查找Name=“一線碼農”的節點\n");  
  34.    
  35.              //查找Name=“一線碼農”的節點  
  36.              var result = chainList.ChainListFindByKey(node, "一線碼農", i => i.Name);  
  37.    
  38.              DisplaySingle(node);  
  39.    
  40.              Console.WriteLine("將”ID=4“的實體插入到“博客園”這個節點的之後\n");  
  41.    
  42.              //將”ID=4“的實體插入到"博客園"這個節點的之後  
  43.              node = chainList.ChainListInsert(node, "博客園", i => i.Name, new Student() { ID = 4, Name = "51cto", Age = 30 });  
  44.    
  45.              Dispaly(node);  
  46.    
  47.              Console.WriteLine("刪除Name=‘51cto‘的節點數據\n");  
  48.    
  49.              //刪除Name=‘51cto‘的節點數據  
  50.              node = chainList.ChainListDelete(node, "51cto", i => i.Name);  
  51.    
  52.              Dispaly(node);  
  53.    
  54.              Console.WriteLine("獲取鏈表的個數:" + chainList.ChanListLength(node));  
  55.          }  
  56.    
  57.          //輸出數據  
  58.          public static void Dispaly(Node<Student> head)  
  59.          {  
  60.              Console.WriteLine("******************* 鏈表數據如下 *******************");  
  61.              var tempNode = head;  
  62.    
  63.              while (tempNode != null)  
  64.              {  
  65.                  Console.WriteLine("ID:" + tempNode.data.ID + ", Name:" + tempNode.data.Name + ",Age:" + tempNode.data.Age);  
  66.                  tempNode = tempNode.next;  
  67.              }  
  68.    
  69.              Console.WriteLine("******************* 鏈表數據展示完畢 *******************\n");  
  70.          }  
  71.    
  72.          //展示當前節點數據  
  73.          public static void DisplaySingle(Node<Student> head)  
  74.          {  
  75.              if (head != null)  
  76.                  Console.WriteLine("ID:" + head.data.ID + ", Name:" + head.data.Name + ",Age:" + head.data.Age);  
  77.              else  
  78.                  Console.WriteLine("未查找到數據!");  
  79.          }  
  80.      }  
  81.   
  82.      #region 學生數據實體  
  83.      /// <summary>  
  84.  /// 學生數據實體  
  85.  /// </summary>  
  86.      public class Student  
  87.      {  
  88.          public int ID { getset; }  
  89.    
  90.          public string Name { getset; }  
  91.    
  92.          public int Age { getset; }  
  93.      }  
  94.      #endregion  
  95.   
  96.      #region 鏈表節點的數據結構  
  97.      /// <summary>  
  98.  /// 鏈表節點的數據結構  
  99.  /// </summary>  
  100.      public class Node<T>  
  101.      {  
  102.          /// <summary>  
  103.  /// 節點的數據域  
  104.  /// </summary>  
  105.          public T data;  
  106.    
  107.          /// <summary>  
  108.  /// 節點的指針域  
  109.  /// </summary>  
  110.          public Node<T> next;  
  111.      }  
  112.      #endregion  
  113.   
  114.      #region 鏈表的相關操作  
  115.      /// <summary>  
  116.  /// 鏈表的相關操作  
  117.  /// </summary>  
  118.      public class ChainList  
  119.      {  
  120.          #region 將節點添加到鏈表的末尾  
  121.          /// <summary>  
  122.  /// 將節點添加到鏈表的末尾  
  123.  /// </summary>  
  124.  /// <typeparam name="T"></typeparam>  
  125.  /// <param name="head"></param>  
  126.  /// <param name="data"></param>  
  127.  /// <returns></returns>  
  128.          public Node<T> ChainListAddEnd<T>(Node<T> head, T data)  
  129.          {  
  130.              Node<T> node = new Node<T>();  
  131.    
  132.              node.data = data;  
  133.              node.next = null;  
  134.    
  135.              ///說明是一個空鏈表  
  136.              if (head == null)  
  137.              {  
  138.                  head = node;  
  139.                  return head;  
  140.              }  
  141.    
  142.              //獲取當前鏈表的最後一個節點  
  143.              ChainListGetLast(head).next = node;  
  144.    
  145.              return head;  
  146.          }  
  147.          #endregion  
  148.   
  149.          #region 將節點添加到鏈表的開頭  
  150.          /// <summary>  
  151.  /// 將節點添加到鏈表的開頭  
  152.  /// </summary>  
  153.  /// <typeparam name="T"></typeparam>  
  154.  /// <param name="chainList"></param>  
  155.  /// <param name="data"></param>  
  156.  /// <returns></returns>  
  157.          public Node<T> ChainListAddFirst<T>(Node<T> head, T data)  
  158.          {  
  159.              Node<T> node = new Node<T>();  
  160.    
  161.              node.data = data;  
  162.              node.next = head;  
  163.    
  164.              head = node;  
  165.    
  166.              return head;  
  167.    
  168.          }  
  169.          #endregion  
  170.   
  171.          #region 將節點插入到指定位置  
  172.          /// <summary>  
  173.  /// 將節點插入到指定位置  
  174.  /// </summary>  
  175.  /// <typeparam name="T"></typeparam>  
  176.  /// <param name="head"></param>  
  177.  /// <param name="currentNode"></param>  
  178.  /// <param name="data"></param>  
  179.  /// <returns></returns>  
  180.          public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable  
  181.          {  
  182.              if (head == null)  
  183.                  return null;  
  184.    
  185.              if (where(head.data).CompareTo(key) == 0)  
  186.              {  
  187.                  Node<T> node = new Node<T>();  
  188.    
  189.                  node.data = data;  
  190.    
  191.                  node.next = head.next;  
  192.    
  193.                  head.next = node;  
  194.              }  
  195.    
  196.              ChainListInsert(head.next, key, where, data);  
  197.    
  198.              return head;  
  199.          }  
  200.          #endregion  
  201.   
  202.          #region 將指定關鍵字的節點刪除  
  203.          /// <summary>  
  204.  /// 將指定關鍵字的節點刪除  
  205.  /// </summary>  
  206.  /// <typeparam name="T"></typeparam>  
  207.  /// <typeparam name="W"></typeparam>  
  208.  /// <param name="head"></param>  
  209.  /// <param name="key"></param>  
  210.  /// <param name="where"></param>  
  211.  /// <param name="data"></param>  
  212.  /// <returns></returns>  
  213.          public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable  
  214.          {  
  215.              if (head == null)  
  216.                  return null;  
  217.    
  218.              //這是針對只有一個節點的解決方案  
  219.              if (where(head.data).CompareTo(key) == 0)  
  220.              {  
  221.                  if (head.next != null)  
  222.                      head = head.next;  
  223.                  else  
  224.                      return head = null;  
  225.              }  
  226.              else  
  227.              {  
  228.                  //判斷一下此節點是否是要刪除的節點的前一節點  
  229.                  if (head.next != null && where(head.next.data).CompareTo(key) == 0)  
  230.                  {  
  231.                      //將刪除節點的next域指向前一節點  
  232.                      head.next = head.next.next;  
  233.                  }  
  234.              }  
  235.    
  236.              ChainListDelete(head.next, key, where);  
  237.    
  238.              return head;  
  239.          }  
  240.          #endregion  
  241.   
  242.          #region 通過關鍵字查找指定的節點  
  243.          /// <summary>  
  244.  /// 通過關鍵字查找指定的節點  
  245.  /// </summary>  
  246.  /// <typeparam name="T"></typeparam>  
  247.  /// <typeparam name="W"></typeparam>  
  248.  /// <param name="head"></param>  
  249.  /// <param name="key"></param>  
  250.  /// <param name="where"></param>  
  251.  /// <returns></returns>  
  252.          public Node<T> ChainListFindByKey<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable  
  253.          {  
  254.              if (head == null)  
  255.                  return null;  
  256.    
  257.              if (where(head.data).CompareTo(key) == 0)  
  258.                  return head;  
  259.    
  260.              return ChainListFindByKey<T, W>(head.next, key, where);  
  261.          }  
  262.          #endregion  
  263.   
  264.          #region 獲取鏈表的長度  
  265.          /// <summary>  
  266.  ///// 獲取鏈表的長度  
  267.  /// </summary>  
  268.  /// <typeparam name="T"></typeparam>  
  269.  /// <param name="head"></param>  
  270.  /// <returns></returns>  
  271.          public int ChanListLength<T>(Node<T> head)  
  272.          {  
  273.              int count = 0;  
  274.    
  275.              while (head != null)  
  276.              {  
  277.                  ++count;  
  278.                  head = head.next;  
  279.              }  
  280.    
  281.              return count;  
  282.          }  
  283.          #endregion  
  284.   
  285.          #region 得到當前鏈表的最後一個節點  
  286.          /// <summary>  
  287.  /// 得到當前鏈表的最後一個節點  
  288.  /// </summary>  
  289.  /// <typeparam name="T"></typeparam>  
  290.  /// <param name="head"></param>  
  291.  /// <returns></returns>  
  292.          public Node<T> ChainListGetLast<T>(Node<T> head)  
  293.          {  
  294.              if (head.next == null)  
  295.                  return head;  
  296.              return ChainListGetLast(head.next);  
  297.          }  
  298.          #endregion  
  299.    
  300.      }  
  301.      #endregion  
  302.  }  


 

運行結果:

 

當然,單鏈表操作中有很多是O(N)的操作,這給我們帶來了尷尬的局面,所以就有了很多的

優化方案,比如:雙向鏈表,循環鏈表。靜態鏈表等等,這些希望大家在懂得單鏈表的情況下

待深一步的研究。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章