婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 算法系列15天速成 第七天 線性表【上】

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

熱門標簽:湖北穩定外呼系統 地圖標注和圖片標注 語音平臺系統 電銷機器人怎么收費 忻州外呼系統接口對接 醫院地圖標注 嘟聲的電銷機器人 洛陽便宜外呼系統廠家 滄州智能外呼系統收費

哈哈,我們的數據也一樣,存在這三種基本關系,用術語來說就是:

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,程序歇菜了,我也歇菜了。

好,現在是上代碼時間。

復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeqList
{
    public class Program
    {
        static void Main(string[] args)
        {
            SeqList seq = new SeqList();
            SeqListTypeStudent> list = new SeqListTypeStudent>();
            Console.WriteLine("\n********************** 添加二條數據 ************************\n");
            seq.SeqListAddStudent>(list, new Student() { ID = "1", Name = "一線碼農", Age = 23 });
            seq.SeqListAddStudent>(list, new Student() { ID = "3", Name = "huangxincheng520", Age = 23 });
            Console.WriteLine("添加成功");
            //展示數據
            Display(list);
            Console.WriteLine("\n********************** 正在搜索Name=“一線碼農”的實體 ************************\n");
            var student = seq.SeqListFindByKeyStudent, string>(list, "一線碼農", s => s.Name);
            Console.WriteLine("\n********************** 展示一下數據 ************************\n");
            if (student != null)
                Console.WriteLine("ID:" + student.ID + ",Name:" + student.Name + ",Age:" + student.Age);
            else
                Console.WriteLine("對不起,數據未能檢索到。");
            Console.WriteLine("\n********************** 插入一條數據 ************************\n");
            seq.SeqListInsert(list, 1, new Student() { ID = "2", Name = "博客園", Age = 40 });
            Console.WriteLine("插入成功");
            //展示一下
            Display(list);
            Console.WriteLine("\n********************** 刪除一條數據 ************************\n");
            seq.SeqListDelete(list, 0);
            Console.WriteLine("刪除成功");
            //展示一下數據
            Display(list);
            Console.Read();
        }

        ///summary>
/// 展示輸出結果
////summary>
        static void Display(SeqListTypeStudent> list)
        {
            Console.WriteLine("\n********************** 展示一下數據 ************************\n");
            if (list == null || list.ListLen == 0)
            {
                Console.WriteLine("嗚嗚,沒有數據");
                return;
            }
            for (int i = 0; i list.ListLen; i++)
            {
                Console.WriteLine("ID:" + list.ListData[i].ID + ",Name:" + list.ListData[i].Name + ",Age:" + list.ListData[i].Age);
            }
        }
    }

    #region 學生的數據結構
    ///summary>
/// 學生的數據結構
////summary>
    public class Student
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    #endregion

    #region 定義一個順序表的存儲結構
    ///summary>
/// 定義一個順序表的存儲結構
////summary>
    public class SeqListTypeT>
    {
        private const int maxSize = 100;
        public int MaxSize { get { return maxSize; } }
        //數據為100個存儲空間
        public T[] ListData = new T[maxSize];
        public int ListLen { get; set; }
    }
    #endregion

    #region 順序表的相關操作
    ///summary>
///順序表的相關操作
////summary>
    public class SeqList
    {
        #region 順序表初始化
        ///summary>
/// 順序表初始化
////summary>
///param name="t">/param>
        public void SeqListInitT>(SeqListTypeT> t)
        {
            t.ListLen = 0;
        }
        #endregion

        #region 順序表的長度
        ///summary>
/// 順序表的長度
////summary>
///param name="t">/param>
///returns>/returns>
        public int SeqListLenT>(SeqListTypeT> t)
        {
            return t.ListLen;
        }
        #endregion

        #region 順序表的添加
        ///summary>
///順序表的添加
////summary>
///param name="t">/param>
///returns>/returns>
        public bool SeqListAddT>(SeqListTypeT> t, T data)
        {
            //防止數組溢出
            if (t.ListLen == t.MaxSize)
                return false;
            t.ListData[t.ListLen++] = data;
            return true;
        }
        #endregion

        #region 順序表的插入操作
        ///summary>
/// 順序表的插入操作
////summary>
///param name="t">/param>
///param name="n">/param>
///param name="data">/param>
///returns>/returns>
        public bool SeqListInsertT>(SeqListTypeT> t, int n, T data)
        {
            //首先判斷n是否合法
            if (n 0 || n > t.MaxSize - 1)
                return false;
            //說明數組已滿,不能進行插入操作
            if (t.ListLen == t.MaxSize)
                return false;
            //需要將插入點的數組數字依次向后移動
            for (int i = t.ListLen - 1; i >= n; i--)
            {
                t.ListData[i + 1] = t.ListData[i];
            }

            //最后將data插入到騰出來的位置
            t.ListData[n] = data;
            t.ListLen++;
            return true;
        }
        #endregion

        #region 順序表的刪除操作
        ///summary>
/// 順序表的刪除操作
////summary>
///param name="t">/param>
///param name="n">/param>
///returns>/returns>
        public bool SeqListDeleteT>(SeqListTypeT> t, int n)
        {
            //判斷刪除位置是否非法
            if (n 0 || n > t.ListLen - 1)
                return false;
            //判斷數組是否已滿
            if (t.ListLen == t.MaxSize)
                return false;
            //將n處后的元素向前移位
            for (int i = n; i t.ListLen; i++)
                t.ListData[i] = t.ListData[i + 1];
            //去掉數組最后一個元素
            --t.ListLen;
            return true;
        }
        #endregion

        #region 順序表的按序號查找
        ///summary>
/// 順序表的按序號查找
////summary>
///param name="t">/param>
///param name="n">/param>
///returns>/returns>
        public T SeqListFindByNumT>(SeqListTypeT> t, int n)
        {
            if (n 0 || n > t.ListLen - 1)
                return default(T);
            return t.ListData[n];
        }
        #endregion

        #region  順序表的關鍵字查找
        ///summary>
/// 順序表的關鍵字查找
////summary>
///typeparam name="T">/typeparam>
///typeparam name="W">/typeparam>
///param name="t">/param>
///param name="key">/param>
///param name="where">/param>
///returns>/returns>
        public T SeqListFindByKeyT, W>(SeqListTypeT> t, string key, FuncT, W> where) where W : IComparable
        {

            for (int i = 0; i t.ListLen; i++)
            {
                if (where(t.ListData[i]).CompareTo(key) == 0)
                {
                    return t.ListData[i];
                }
            }
            return default(T);
        }
        #endregion
    }
    #endregion
}

運行結果:

您可能感興趣的文章:
  • java線性表排序示例分享
  • 算法系列15天速成 第八天 線性表【下】
  • php線性表順序存儲實現代碼(增刪查改)
  • 數據結構簡明備忘錄 線性表
  • C語言安全之數組長度與指針實例解析
  • C語言安全編碼數組記法的一致性
  • C語言安全編碼之數組索引位的合法范圍
  • C語言安全編碼之數值中的sizeof操作符
  • python和C語言混合編程實例
  • C語言線性表的順序表示與實現實例詳解

標簽:山南 96 內蒙古 日照 巴彥淖爾 防城港 定州 宜賓

巨人網絡通訊聲明:本文標題《算法系列15天速成 第七天 線性表【上】》,本文關鍵詞  算法,系列,15天,速成,第,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《算法系列15天速成 第七天 線性表【上】》相關的同類信息!
  • 本頁收集關于算法系列15天速成 第七天 線性表【上】的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 富阳市| 巴彦县| 紫阳县| 大荔县| 文水县| 汽车| 吉林市| 通化市| 南昌县| 搜索| 屯留县| 阳春市| 托里县| 鸡西市| 井研县| 岑溪市| 育儿| 罗山县| 开封县| 大石桥市| 宜兰县| 河南省| 金沙县| 体育| 同仁县| 米脂县| 湖北省| 虹口区| 嘉黎县| 永和县| 南召县| 波密县| 阿坝| 马关县| 贵定县| 温泉县| 泸州市| 高青县| 冀州市| 新源县| 厦门市|