算法系列15天速成——第七天 线性表【上】

16 篇文章 0 订阅

算法系列15天速成——第七天 线性表【上】


转自:http://blog.csdn.net/m13666368773/article/details/7516486

人活在社会上不可能孤立,比如跟美女有着千丝万缕的关系,有的是一对一,有的是一对多,有的是多对多。

哈哈,我们的数据也一样,存在这三种基本关系,用术语来说就是:

<1>  线性关系。

<2>  树形关系。

<3>  网状关系。

 

一: 线性表

      1 概念:

                 线性表也就是关系户中最简单的一种关系,一对一。

                  如:学生学号的集合就是一个线性表。

     

      2 特征:

                 ① 有且只有一个“首元素“。

                 ② 有且只有一个“末元素”。

                 ③ 除“末元素”外,其余元素均有唯一的后继元素。

                 ④ 除“首元素”外,其余元素均有唯一的前驱元素。

     

     3 存储划分:

                  ① 如果把线性表用“顺序存储”,那么就是“顺序表”。

                  ② 如果把线性表用“链式存储”,那么就是“链表”。

     

     4  常用操作:添加,删除,插入,查找,遍历,统计。

 

今天主要就说说“线性表”的“顺序存储”。

 

那么下面就简单的浅析一下这个操作的原理和复杂度。

     <1> 初始化顺序表:  

                           这个操作其实还是蛮简单的,设置length=0,也就是O(1)的时间。

     <2> 求顺序表长度:  

                           这个不解释,O(1)的时间。

     <3> 添加节点:      

                           因为是顺序表,所以添加的节点直接会放到数组的末尾,时间也是O(1)的。

     <4> 插入节点:

                           这个还是有点小麻烦的,主要也就是说分两种情况:

                                    ①:当插入节点在数组的最后,那么这个“插入”其实就是”添加“操作,时间当然是O(1)。

                                    ②:当插入节点在数组的开头,那就悲催了,被插入节点的后续元素都要向后移动一位,

                                            也就让整个数组一阵痉挛,效率低下可想而知,时间复杂度退化为O(n)。

      <5> 删除节点:      

                             这个跟“插入”的道理是一样的,也要分两个情况,

                                     ①:当删除的元素在数组的最后,不用移位,谢天谢地,时间为O(1)。

                                     ②: 当删除的元素在数组的开头,删除节点处的元素都要统统向前移位,同样也是一阵痉挛,

                                               时间复杂度也退化为O(n)。

      <6> 按序号查找节点:

                               大家都知道,顺序表的存储地址是连续的,所以第N个元素地址公式为:(N-1)X 数据存储长度。

                                        哈哈,这就是顺序表得瑟的地方,查找的时间复杂度为O(1)。

      <7> 按关键字查找:  

                                 嗯,这个在日常开发中用的最多的,那么就避免不了将key的值在我们的list中查找,前期也说过,

                                        最快的查找是O(1),当然他是用空间来换取时间的,最慢的查找是O(n),那么这里我们就一个for

                                        循环搞定,时间复杂度为O(n)。

 

说了这么多,目的就是预先评估算法的执行效率,给我们带来一手的参考资料,做到真正的运筹帷幄,决胜千里之外。

这也是我们学习算法的目的,到时候不会让我们说tnd,程序歇菜了,我也歇菜了。


好,现在是上代码时间。

[csharp]  view plain  copy
  1. using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Linq;  
  4.  using System.Text;  
  5.    
  6.  namespace SeqList  
  7.  {  
  8.      public class Program  
  9.      {  
  10.          static void Main(string[] args)  
  11.          {  
  12.              SeqList seq = new SeqList();  
  13.              SeqListType<Student> list = new SeqListType<Student>();  
  14.              Console.WriteLine("\n********************** 添加二条数据 ************************\n");  
  15.              seq.SeqListAdd<Student>(list, new Student() { ID = "1", Name = "一线码农", Age = 23 });  
  16.              seq.SeqListAdd<Student>(list, new Student() { ID = "3", Name = "huangxincheng520", Age = 23 });  
  17.              Console.WriteLine("添加成功");  
  18.              //展示数据  
  19.              Display(list);  
  20.              Console.WriteLine("\n********************** 正在搜索Name=“一线码农”的实体 ************************\n");  
  21.              var student = seq.SeqListFindByKey<Student, string>(list, "一线码农", s => s.Name);  
  22.              Console.WriteLine("\n********************** 展示一下数据 ************************\n");  
  23.              if (student != null)  
  24.                  Console.WriteLine("ID:" + student.ID + ",Name:" + student.Name + ",Age:" + student.Age);  
  25.              else  
  26.                  Console.WriteLine("对不起,数据未能检索到。");  
  27.              Console.WriteLine("\n********************** 插入一条数据 ************************\n");  
  28.              seq.SeqListInsert(list, 1, new Student() { ID = "2", Name = "博客园", Age = 40 });  
  29.              Console.WriteLine("插入成功");  
  30.              //展示一下  
  31.              Display(list);  
  32.              Console.WriteLine("\n********************** 删除一条数据 ************************\n");  
  33.              seq.SeqListDelete(list, 0);  
  34.              Console.WriteLine("删除成功");  
  35.              //展示一下数据  
  36.              Display(list);  
  37.              Console.Read();  
  38.          }  
  39.    
  40.          ///<summary>  
  41.  /// 展示输出结果  
  42.  ///</summary>  
  43.          static void Display(SeqListType<Student> list)  
  44.          {  
  45.              Console.WriteLine("\n********************** 展示一下数据 ************************\n");  
  46.              if (list == null || list.ListLen == 0)  
  47.              {  
  48.                  Console.WriteLine("呜呜,没有数据");  
  49.                  return;  
  50.              }  
  51.              for (int i = 0; i < list.ListLen; i++)  
  52.              {  
  53.                  Console.WriteLine("ID:" + list.ListData[i].ID + ",Name:" + list.ListData[i].Name + ",Age:" + list.ListData[i].Age);  
  54.              }  
  55.          }  
  56.      }  
  57.   
  58.      #region 学生的数据结构  
  59.      ///<summary>  
  60.  /// 学生的数据结构  
  61.  ///</summary>  
  62.      public class Student  
  63.      {  
  64.          public string ID { getset; }  
  65.          public string Name { getset; }  
  66.          public int Age { getset; }  
  67.      }  
  68.      #endregion  
  69.   
  70.      #region 定义一个顺序表的存储结构  
  71.      ///<summary>  
  72.  /// 定义一个顺序表的存储结构  
  73.  ///</summary>  
  74.      public class SeqListType<T>  
  75.      {  
  76.          private const int maxSize = 100;  
  77.          public int MaxSize { get { return maxSize; } }  
  78.          //数据为100个存储空间  
  79.          public T[] ListData = new T[maxSize];  
  80.          public int ListLen { getset; }  
  81.      }  
  82.      #endregion  
  83.   
  84.      #region 顺序表的相关操作  
  85.      ///<summary>  
  86.  ///顺序表的相关操作  
  87.  ///</summary>  
  88.      public class SeqList  
  89.      {  
  90.          #region 顺序表初始化  
  91.          ///<summary>  
  92.  /// 顺序表初始化  
  93.  ///</summary>  
  94.  ///<param name="t"></param>  
  95.          public void SeqListInit<T>(SeqListType<T> t)  
  96.          {  
  97.              t.ListLen = 0;  
  98.          }  
  99.          #endregion  
  100.   
  101.          #region 顺序表的长度  
  102.          ///<summary>  
  103.  /// 顺序表的长度  
  104.  ///</summary>  
  105.  ///<param name="t"></param>  
  106.  ///<returns></returns>  
  107.          public int SeqListLen<T>(SeqListType<T> t)  
  108.          {  
  109.              return t.ListLen;  
  110.          }  
  111.          #endregion  
  112.   
  113.          #region 顺序表的添加  
  114.          ///<summary>  
  115.  ///顺序表的添加  
  116.  ///</summary>  
  117.  ///<param name="t"></param>  
  118.  ///<returns></returns>  
  119.          public bool SeqListAdd<T>(SeqListType<T> t, T data)  
  120.          {  
  121.              //防止数组溢出  
  122.              if (t.ListLen == t.MaxSize)  
  123.                  return false;  
  124.              t.ListData[t.ListLen++] = data;  
  125.              return true;  
  126.          }  
  127.          #endregion  
  128.   
  129.          #region 顺序表的插入操作  
  130.          ///<summary>  
  131.  /// 顺序表的插入操作  
  132.  ///</summary>  
  133.  ///<param name="t"></param>  
  134.  ///<param name="n"></param>  
  135.  ///<param name="data"></param>  
  136.  ///<returns></returns>  
  137.          public bool SeqListInsert<T>(SeqListType<T> t, int n, T data)  
  138.          {  
  139.              //首先判断n是否合法  
  140.              if (n < 0 || n > t.MaxSize - 1)  
  141.                  return false;  
  142.              //说明数组已满,不能进行插入操作  
  143.              if (t.ListLen == t.MaxSize)  
  144.                  return false;  
  145.              //需要将插入点的数组数字依次向后移动  
  146.              for (int i = t.ListLen - 1; i >= n; i--)  
  147.              {  
  148.                  t.ListData[i + 1] = t.ListData[i];  
  149.              }  
  150.    
  151.              //最后将data插入到腾出来的位置  
  152.              t.ListData[n] = data;  
  153.              t.ListLen++;  
  154.              return true;  
  155.          }  
  156.          #endregion  
  157.   
  158.          #region 顺序表的删除操作  
  159.          ///<summary>  
  160.  /// 顺序表的删除操作  
  161.  ///</summary>  
  162.  ///<param name="t"></param>  
  163.  ///<param name="n"></param>  
  164.  ///<returns></returns>  
  165.          public bool SeqListDelete<T>(SeqListType<T> t, int n)  
  166.          {  
  167.              //判断删除位置是否非法  
  168.              if (n < 0 || n > t.ListLen - 1)  
  169.                  return false;  
  170.              //判断数组是否已满  
  171.              if (t.ListLen == t.MaxSize)  
  172.                  return false;  
  173.              //将n处后的元素向前移位  
  174.              for (int i = n; i < t.ListLen; i++)  
  175.                  t.ListData[i] = t.ListData[i + 1];  
  176.              //去掉数组最后一个元素  
  177.              --t.ListLen;  
  178.              return true;  
  179.          }  
  180.          #endregion  
  181.   
  182.          #region 顺序表的按序号查找  
  183.          ///<summary>  
  184.  /// 顺序表的按序号查找  
  185.  ///</summary>  
  186.  ///<param name="t"></param>  
  187.  ///<param name="n"></param>  
  188.  ///<returns></returns>  
  189.          public T SeqListFindByNum<T>(SeqListType<T> t, int n)  
  190.          {  
  191.              if (n < 0 || n > t.ListLen - 1)  
  192.                  return default(T);  
  193.              return t.ListData[n];  
  194.          }  
  195.          #endregion  
  196.   
  197.          #region  顺序表的关键字查找  
  198.          ///<summary>  
  199.  /// 顺序表的关键字查找  
  200.  ///</summary>  
  201.  ///<typeparam name="T"></typeparam>  
  202.  ///<typeparam name="W"></typeparam>  
  203.  ///<param name="t"></param>  
  204.  ///<param name="key"></param>  
  205.  ///<param name="where"></param>  
  206.  ///<returns></returns>  
  207.          public T SeqListFindByKey<T, W>(SeqListType<T> t, string key, Func<T, W> where) where W : IComparable  
  208.          {  
  209.    
  210.              for (int i = 0; i < t.ListLen; i++)  
  211.              {  
  212.                  if (where(t.ListData[i]).CompareTo(key) == 0)  
  213.                  {  
  214.                      return t.ListData[i];  
  215.                  }  
  216.              }  
  217.              return default(T);  
  218.          }  
  219.          #endregion  
  220.      }  
  221.      #endregion  
  222.  }  


 

运行结果:

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值