侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

技术  /  管理员 发布于 7年前   239

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

<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();
            SeqListType<Student> list = new SeqListType<Student>();
            Console.WriteLine("\n********************** 添加二条数据 ************************\n");
            seq.SeqListAdd<Student>(list, new Student() { ID = "1", Name = "一线码农", Age = 23 });
            seq.SeqListAdd<Student>(list, new Student() { ID = "3", Name = "huangxincheng520", Age = 23 });
            Console.WriteLine("添加成功");
            //展示数据
            Display(list);
            Console.WriteLine("\n********************** 正在搜索Name=“一线码农”的实体 ************************\n");
            var student = seq.SeqListFindByKey<Student, 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(SeqListType<Student> 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 SeqListType<T>
    {
        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 SeqListInit<T>(SeqListType<T> t)
        {
            t.ListLen = 0;
        }
        #endregion

        #region 顺序表的长度
        ///<summary>
/// 顺序表的长度
///</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)
        {
            //防止数组溢出
            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;
            //说明数组已满,不能进行插入操作
            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 SeqListDelete<T>(SeqListType<T> 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 SeqListFindByNum<T>(SeqListType<T> 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 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
}

运行结果:


  • 上一条:
    算法系列15天速成 第九天 队列
    下一条:
    算法系列15天速成 第六天 五大经典查找【下】
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客