数据结构 —顺序表结构

什么是线性表

从逻辑定义,线性表是由n(n>=0)个数据元素 a1, a2, …, an组成的有限序列.对于一个非空的线性表,其逻辑结构特征如下:

  1. 有且只有一个开始结点,没有直接前趋结点,有且只有一个直接前趋的后续结点。
  2. 有且只有一个终结点,没有直接后续结点,有且只有一个直接前趋结点;
  3. 其余的内部结点都有仅有一个直接前趋结点和后趋结点;
  4. 对于同一线性表,个元素必须具有相同的数据类型,即同一线性表中各数据元素具有相同的类型,每个数据元素的长度相同。

线性表的基本运算

1 初始化 ,(InitList) 即构造一个空的线性表L;
2 计算表长 ,(ListLength)即计算线性表中结点的个数;
3 获取结点,(GetNode)即取出线性表L中第几个结点的数据;
4 查找结点,(LoachtNode )即在线表中查找值为X的结点,并返回该结点的位置,如果没有,返回null;
5 插入结点,(InsertList)即在线性表中第I个位置插入一个新的结点,使得其后的结点编号依次加1,在插入结点后,表的长度加1;
6 删除结点 (DeleteList)即删除线性表L中的第i个结点,使得其后结点编号一次减1;删除结点后,表的长度减1;

线性表案例

class DATA
    {
        public string key;         //结点的关键字
        public string name;
        public int age;           //
    }

    //定义表结构
    class SLType
    {
        public static int MAXLEN = 100;
        DATA[] listData = new DATA[MAXLEN + 1];
        int listLen;

        //初始化
        public void SLInit(SLType SL)
        {
            SL.listLen = 0;
        }

        //返回表的长度
        public int SLLength(SLType SL)
        {
            return SL.listLen;
        }

        //插入结点
        public int SLInsert(SLType SL, int n, DATA data)
        {
            int i;
            if (SL.listLen >= MAXLEN)
            {
                Console.WriteLine("表已满,不能插入");
                return 0;
            }

            if (n < 1 || n > SL.listLen - 1)
            {
                Console.WriteLine("插入序号错误,不能插入");
                return 0;
            }
            //b把表中数据往后移动一位
            for (i = SL.listLen; i >= n; i--)
            {
                SL.listData[i + 1] = SL.listData[i];
            }

            SL.listData[n] = data;                  //插入数据
            SL.listLen++;
            return 1;
        }

        //增加一个元素到表未
        public int AddSL_Type(SLType SL, DATA data)
        {
            if (SL.listLen >= MAXLEN)
            {
                Console.WriteLine("表已满,不能插入");
                return 0;
            }
            SL.listData[++SL.listLen] = data;                  //插入数据
            return 1;
        }

        public int DeleteSL_Type(SLType SL, int n)
        {
            int i;
            if (n < 1 || n > SL.listLen)
            {
                Console.WriteLine("删除序号错误,不能删除");
                return 0;
            }

            //数据往前移动一位
            for (i = SL.listLen; i >= n; i--)
            {
                SL.listData[i] = SL.listData[i + 1];
            }
            SL.listLen--;
            return 1;
        }

        //查找数据根据序号
        public DATA SLFindByNum(SLType SL, int n)
        {
            if (n < 1 || n > SL.listLen)
            {
                Console.WriteLine("查找序号错误,不能查找");
                return null;
            }
            return SL.listData[n];
        }

        public int SLFindByontC(SLType SL, string key)
        {
            int i;
            for (i = 0; i < SL.listLen; i++)
            {
                if (SL.listData[i].key.CompareTo(key) == 0)
                {
                    return i;
                }
            }
            return 0;
        }

        public int SLAll(SLType SL)
        {
            for (int i = 0; i < SL.listLen; i++)
            {
                Console.WriteLine(SL.listData[i].key + "====" + SL.listData[i].name + "=====" + SL.listData[i].age);
            }
            return 0;
        }
    }
发布了37 篇原创文章 · 获赞 11 · 访问量 6303

猜你喜欢

转载自blog.csdn.net/weixin_42422809/article/details/88356131