文章知识点来至于大话数据结构里边章节知识, 这篇主要介绍线性表在计算机中的存储形式,以及线性表的顺序存储和链式存储方式。同时附上来线性表的顺序存储和链式存储的实现过程。 相关代码源码请查看文章最后, 文章末尾提供了源代码工程下载地址。

数据结构绪论

1、  数据结构

逻辑结构

         集合结构:元素之间没有关系

         线性结构:一对一

         树形结构:一对多

         图形结构:多对多

物理结构

定义:指数据的逻辑结构在计算机中的存储形式

储存类型

           顺序存储结构

链式存储结构

算法

1 算法定义

         算法是解决待定问题求解步骤的描述,在计算机中表现为指令的有序序列, 并且每条指令表示一个或多个操作

2 算法特性

         输入

         输出

         有穷性

         确定性

         可行性

3 算法时间复杂度

         T(n) = O(f(n)), 表示随着问题规模n的增大, 算法执行时间的增长率和f(n)的增加率相同, 称作算法的渐进时间复杂度

4 算法时间复杂度推导方法

         常数阶

         对数阶

         平方阶

5 算法的设计要求

         正确性

         可读性

         健壮性

         高效性

         低存储量需求

线性表

1 线性表的物理结构

         线性表的顺序存储结构

线性表的链式存储结构

2 线性表的顺序存储结构

                   优点

无需为表示表中元素间的逻辑关系增加额外的存储空间

可以快速的存取表中任意位置的元素

                   缺点

                            插入和删除需要移动大量元素

                            造成存储空间的碎片

3 线性表的链式存储结构

                   链表结构图

                         大话数据结构-线性表       

                   代码描述

                            Typeof strcut Node

                            {

                                     ElemType data,

                                     Struct *next

                            } Node;

                            Typeof stuct Node *LinkList;

                   数据描述

                            Ai元素数据域p->data, 指针域p->next

                            Ai+1元素数据域p->next->data = ai+1

4 单链表结构和数据存储结构优缺点

         存储分配方式

                   顺序存储结构用一段连续的存储单元依次存储线性表的数据元素

                   单链表存储结构采用链式存储结构, 用一组任意的存储单元存放线性表的元素

         时间性能

                   查找:顺序存储结构为O(1) 单链表O(n)

                   插入和删除:顺序存储为O(n), 单链表在现出某位置的指针后仅为O(1)

         空间性能

                   顺序存储需要预分配存储空间,单链表不需要预分配存储空间, 元素个数不受限制

5 线性表的存储实现源代码 

1、线性表的顺序存储:

public class CustomArray
    {
        private const int MAXLENGTH = 10000;
        private object[] _array;
        private readonly int _maxLength;
        private int _length;

        public CustomArray() : this(MAXLENGTH)
        {
        }

        public CustomArray(int maxLength)
        {
            if (maxLength > MAXLENGTH)
            {
                throw new ArgumentOutOfRangeException(string.Format("数组长度必须小于{0}", MAXLENGTH));
            }
            _maxLength = maxLength;
            _array = new object[100];
        }

        public void AddValue(object value)
        {
            if (GetLength() == _maxLength)
            {
                throw new ArgumentOutOfRangeException("数组已经达到最大值");
            }
            ExpandCustomArray();
            _array[GetLength()] = value;
            _length++;
        }
        /// <summary>
        /// 获取所引处的值
        /// </summary>
        /// <param name="index">具体位置, 从1开始</param>
        /// <returns></returns>
        public object GetValue(int index)
        {
            if (index < 1 || index > GetLength())
            {
                throw new ArgumentOutOfRangeException();
            }
            return _array[index - 1];
        }
        /// <summary>
        /// 在指定的索引位置插入值
        /// </summary>
        /// <param name="value">插入的值</param>
        /// <param name="index">索引位置</param>
        public void Insert(object value, int index)
        {
            if (GetLength() == _maxLength)
            {
                throw new ArgumentOutOfRangeException("数组已经达到最大值");
            }
            if (index < 1 || index > GetLength())
            {
                throw new ArgumentOutOfRangeException("索引已超出数组范围");
            }
            ExpandCustomArray();
          for (var i = GetLength() ; i >= index; i--)
          {
              _array[i] = _array[i - 1];
          }
            _array[index - 1] = value;
            _length++;
        }

        private void ExpandCustomArray()
        {
            if (_array.Length == GetLength())
            {
                var tempArray = new object[_array.Length + 100];
                for (var i = 0; i < _array.Length; i++)
                {
                    tempArray[i] = _array[i];
                }
                _array = tempArray;
            }
        }

        public int GetLength()
        {
            return _length;
        }

        public int IndexOf(object value)
        {
            for (var index = 1; index <= GetLength(); index++)
            {
                if (_array != null &&_array[index - 1].Equals(value))
                {
                    return index;
                }
            }
            return -1;
        }

        public object Delete(int index)
        {
            if (index > GetLength() || index < 1)
            {
                throw  new ArgumentOutOfRangeException();
            }
            var deletedValue = _array[index - 1];
            for (var i = index - 1; i < GetLength() - 1; i++)
            {
                _array[i] = _array[i + 1];
            }
            _length--;
            return deletedValue;
        }

        public void Clear()
        {
            _array = new object[0];
            _length = 0;
        }
    }
View Code

相关文章:

  • 2021-09-23
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-12-26
  • 2021-06-25
  • 2022-01-20
猜你喜欢
  • 2021-06-04
  • 2021-10-03
  • 2021-10-06
  • 2021-05-20
  • 2022-01-01
  • 2022-03-08
  • 2021-06-06
相关资源
相似解决方案