国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 邏輯算法 > 正文

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

2024-09-08 23:18:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

哈哈,我們的數(shù)據(jù)也一樣,存在這三種基本關(guān)系,用術(shù)語(yǔ)來(lái)說(shuō)就是:

<1>  線性關(guān)系。
<2>  樹(shù)形關(guān)系。
<3>  網(wǎng)狀關(guān)系。

一: 線性表

      1 概念:
                 線性表也就是關(guān)系戶中最簡(jiǎn)單的一種關(guān)系,一對(duì)一。
                  如:學(xué)生學(xué)號(hào)的集合就是一個(gè)線性表。

      2 特征:
                 ① 有且只有一個(gè)“首元素“。
                 ② 有且只有一個(gè)“末元素”。
                 ③ 除“末元素”外,其余元素均有唯一的后繼元素。
                 ④ 除“首元素”外,其余元素均有唯一的前驅(qū)元素。

     3 存儲(chǔ)劃分:
                  ① 如果把線性表用“順序存儲(chǔ)”,那么就是“順序表”。
                  ② 如果把線性表用“鏈?zhǔn)酱鎯?chǔ)”,那么就是“鏈表”。

     4  常用操作:添加,刪除,插入,查找,遍歷,統(tǒng)計(jì)。

今天主要就說(shuō)說(shuō)“線性表”的“順序存儲(chǔ)”。

那么下面就簡(jiǎn)單的淺析一下這個(gè)操作的原理和復(fù)雜度。
     <1> 初始化順序表: 
                           這個(gè)操作其實(shí)還是蠻簡(jiǎn)單的,設(shè)置length=0,也就是O(1)的時(shí)間。
     <2> 求順序表長(zhǎng)度: 
                           這個(gè)不解釋,O(1)的時(shí)間。
     <3> 添加節(jié)點(diǎn):     
                           因?yàn)槭琼樞虮?,所以添加的?jié)點(diǎn)直接會(huì)放到數(shù)組的末尾,時(shí)間也是O(1)的。
     <4> 插入節(jié)點(diǎn):
                           這個(gè)還是有點(diǎn)小麻煩的,主要也就是說(shuō)分兩種情況:
                                    ①:當(dāng)插入節(jié)點(diǎn)在數(shù)組的最后,那么這個(gè)“插入”其實(shí)就是”添加“操作,時(shí)間當(dāng)然是O(1)。
                                    ②:當(dāng)插入節(jié)點(diǎn)在數(shù)組的開(kāi)頭,那就悲催了,被插入節(jié)點(diǎn)的后續(xù)元素都要向后移動(dòng)一位,
                                            也就讓整個(gè)數(shù)組一陣痙攣,效率低下可想而知,時(shí)間復(fù)雜度退化為O(n)。
      <5> 刪除節(jié)點(diǎn):     
                             這個(gè)跟“插入”的道理是一樣的,也要分兩個(gè)情況,
                                     ①:當(dāng)刪除的元素在數(shù)組的最后,不用移位,謝天謝地,時(shí)間為O(1)。
                                     ②: 當(dāng)刪除的元素在數(shù)組的開(kāi)頭,刪除節(jié)點(diǎn)處的元素都要統(tǒng)統(tǒng)向前移位,同樣也是一陣痙攣,
                                               時(shí)間復(fù)雜度也退化為O(n)。
      <6> 按序號(hào)查找節(jié)點(diǎn):
                               大家都知道,順序表的存儲(chǔ)地址是連續(xù)的,所以第N個(gè)元素地址公式為:(N-1)X 數(shù)據(jù)存儲(chǔ)長(zhǎng)度。
                                        哈哈,這就是順序表得瑟的地方,查找的時(shí)間復(fù)雜度為O(1)。
      <7> 按關(guān)鍵字查找: 
                                 嗯,這個(gè)在日常開(kāi)發(fā)中用的最多的,那么就避免不了將key的值在我們的list中查找,前期也說(shuō)過(guò),
                                        最快的查找是O(1),當(dāng)然他是用空間來(lái)?yè)Q取時(shí)間的,最慢的查找是O(n),那么這里我們就一個(gè)for
                                        循環(huán)搞定,時(shí)間復(fù)雜度為O(n)。

說(shuō)了這么多,目的就是預(yù)先評(píng)估算法的執(zhí)行效率,給我們帶來(lái)一手的參考資料,做到真正的運(yùn)籌帷幄,決勝千里之外。
這也是我們學(xué)習(xí)算法的目的,到時(shí)候不會(huì)讓我們說(shuō)tnd,程序歇菜了,我也歇菜了。

好,現(xiàn)在是上代碼時(shí)間。

復(fù)制代碼 代碼如下:

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();
            SeqListType<Student> list = new SeqListType<Student>();
            Console.WriteLine("/n********************** 添加二條數(shù)據(jù) ************************/n");
            seq.SeqListAdd<Student>(list, new Student() { ID = "1", Name = "一線碼農(nóng)", Age = 23 });
            seq.SeqListAdd<Student>(list, new Student() { ID = "3", Name = "huangxincheng520", Age = 23 });
            Console.WriteLine("添加成功");
            //展示數(shù)據(jù)
            Display(list);
            Console.WriteLine("/n********************** 正在搜索Name=“一線碼農(nóng)”的實(shí)體 ************************/n");
            var student = seq.SeqListFindByKey<Student, string>(list, "一線碼農(nóng)", s => s.Name);
            Console.WriteLine("/n********************** 展示一下數(shù)據(jù) ************************/n");
            if (student != null)
                Console.WriteLine("ID:" + student.ID + ",Name:" + student.Name + ",Age:" + student.Age);
            else
                Console.WriteLine("對(duì)不起,數(shù)據(jù)未能檢索到。");
            Console.WriteLine("/n********************** 插入一條數(shù)據(jù) ************************/n");
            seq.SeqListInsert(list, 1, new Student() { ID = "2", Name = "博客園", Age = 40 });
            Console.WriteLine("插入成功");
            //展示一下
            Display(list);
            Console.WriteLine("/n********************** 刪除一條數(shù)據(jù) ************************/n");
            seq.SeqListDelete(list, 0);
            Console.WriteLine("刪除成功");
            //展示一下數(shù)據(jù)
            Display(list);
            Console.Read();
        }

        ///<summary>
/// 展示輸出結(jié)果
///</summary>
        static void Display(SeqListType<Student> list)
        {
            Console.WriteLine("/n********************** 展示一下數(shù)據(jù) ************************/n");
            if (list == null || list.ListLen == 0)
            {
                Console.WriteLine("嗚嗚,沒(méi)有數(shù)據(jù)");
                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 學(xué)生的數(shù)據(jù)結(jié)構(gòu)
    ///<summary>
/// 學(xué)生的數(shù)據(jù)結(jié)構(gòu)
///</summary>
    public class Student
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
    #endregion

    #region 定義一個(gè)順序表的存儲(chǔ)結(jié)構(gòu)
    ///<summary>
/// 定義一個(gè)順序表的存儲(chǔ)結(jié)構(gòu)
///</summary>
    public class SeqListType<T>
    {
        private const int maxSize = 100;
        public int MaxSize { get { return maxSize; } }
        //數(shù)據(jù)為100個(gè)存儲(chǔ)空間
        public T[] ListData = new T[maxSize];
        public int ListLen { get; set; }
    }
    #endregion

    #region 順序表的相關(guān)操作
    ///<summary>
///順序表的相關(guān)操作
///</summary>
    public class SeqList
    {
        #region 順序表初始化
        ///<summary>
/// 順序表初始化
///</summary>
///<param name="t"></param>
        public void SeqListInit<T>(SeqListType<T> t)
        {
            t.ListLen = 0;
        }
        #endregion

        #region 順序表的長(zhǎng)度
        ///<summary>
/// 順序表的長(zhǎng)度
///</summary>
///<param name="t"></param>
///<returns></returns>
        public int SeqListLen<T>(SeqListType<T> t)
        {
            return t.ListLen;
        }
        #endregion

        #region 順序表的添加
        ///<summary>
///順序表的添加
///</summary>
///<param name="t"></param>
///<returns></returns>
        public bool SeqListAdd<T>(SeqListType<T> t, T data)
        {
            //防止數(shù)組溢出
            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 SeqListInsert<T>(SeqListType<T> t, int n, T data)
        {
            //首先判斷n是否合法
            if (n < 0 || n > t.MaxSize - 1)
                return false;
            //說(shuō)明數(shù)組已滿,不能進(jìn)行插入操作
            if (t.ListLen == t.MaxSize)
                return false;
            //需要將插入點(diǎn)的數(shù)組數(shù)字依次向后移動(dòng)
            for (int i = t.ListLen - 1; i >= n; i--)
            {
                t.ListData[i + 1] = t.ListData[i];
            }

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

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

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

        #region  順序表的關(guān)鍵字查找
        ///<summary>
/// 順序表的關(guān)鍵字查找
///</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 SeqListFindByKey<T, W>(SeqListType<T> t, string key, Func<T, 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
}

運(yùn)行結(jié)果:

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芜湖市| 青铜峡市| 晋宁县| 洪泽县| 盈江县| 富民县| 广汉市| 沛县| 罗山县| 璧山县| 色达县| 莱芜市| 长治县| 隆林| 北碚区| 乌拉特前旗| 延安市| 金湖县| 江永县| 沽源县| 利川市| 嘉义市| 日喀则市| 调兵山市| 大余县| 冷水江市| 黄冈市| 阿拉善左旗| 雷山县| 砚山县| 中山市| 永康市| 拜泉县| 鸡泽县| 嘉峪关市| 泸水县| 遵义市| 麦盖提县| 谷城县| 民勤县| 张北县|