代码改变世界

线性表 - 顺序存储结构

2011-08-12 21:25  DylanChan  阅读(562)  评论(0编辑  收藏  举报

线性表(概念):零个或多个具有相同类型的数据元素的有限序列

 


线性表的顺序存储结构:指的是用一段地址连续的存储单元依次存储线性表的数据元素。

通俗描述:在内存找了块地儿,把一定的内存空间给占了,然后把相同数据类型的数据元素依次存放在这块空地中。

描述顺序存储结构需要三个属性:

  ◆   存储空间的起始位置:数组data,它的存储位置就是存储空间的存储位置
     线性表的最大存储容量:数组长度MaxSize
     线性表的当前长度:length
数组长度:存放线性表存储空间的长度,存储分配后这个量一般是不变的。

线性表长度:线性表中数据元素的个数,随着线性表插入和删除操作进行的,这个量是变化的。
001   //线性表--顺序存储结构
002    public class SequenceList<T> : IEnumerable<T>
003    {
004        //定义最大容量,使用数组必须先确定容量
005        private int maxSize;
006        //定义数组,存放数据元素
007        private T[] tItems;
008        //定义线性表下标,获取线性表元素
009        private int elementCount;
010 
011        public int MaxSize
012        {
013            get { return maxSize; }
014            set { maxSize = value; }
015        }
016        //实现索引器,方便返回
017        public T this[int i]
018        {
019            get { return this.tItems[i]; }
020        }
021 
022        public int ElementCount
023        {
024            get { return this.elementCount; }
025            set { elementCount = value; }
026        }
027 
028        /// <summary>
029        /// 初始化线性表
030        /// </summary>
031        /// <param name="size">线性表容量
032        public SequenceList(int size)
033        {
034            if (size > 0)
035            {
036                //设置数组大小(初始化最大容量)
037                this.MaxSize = size;
038                //初始化数组
039                this.tItems = new T[size];
040                //默认为空表,容量设为-1
041                this.ElementCount = -1;
042            }
043            else
044            {
045                Console.WriteLine("This Linear List Is Not Inital!");
046            }
047        }
048 
049        /// <summary>
050        /// 是否超现容量
051        /// </summary>
052        /// <returns>true,超出容量</returns>
053        public bool IsFull()
054        {
055            return this.ElementCount + 1 == this.MaxSize;
056        }
057 
058        /// <summary>
059        /// 获取顺序线性表长度
060        /// </summary>
061        /// <returns>返回顺序线性表长度</returns>
062        public int ListCount()
063        {
064            return this.ElementCount + 1;
065        }
066        /// <summary>
067        /// 顺序线性表添加操作
068        /// </summary>
069        /// <param name="item">添加的项
070        public void ListAdd(T item)
071        {
072            if (this.IsFull())
073            {
074                Console.WriteLine("This Linear List Is Full,Can't Add Any Items!");
075                return;
076            }
077            else
078            {
079                this.tItems[++this.elementCount] = item;
080            }
081        }
082        /// <summary>
083        /// 线性表是否为空
084        /// </summary>
085        /// <returns>true,线性表为空</returns>
086        public bool ListEmpty()
087        {
088            return this.elementCount == -1;
089        }
090        /// <summary>
091        /// 获取某位置的元素
092        /// </summary>
093        /// <param name="i">线性表位置
094        /// <returns>返回线性表元素</returns>
095        public T GetElement(int i)
096        {
097            if (this.elementCount == -1)
098            {
099                Console.WriteLine("There Are No Elements In This Linear List!");
100                return default(T);
101            }
102            if (i < 0 || i > this.elementCount)
103            {
104                Console.WriteLine("Exceed the capability!");
105                return default(T);
106            }
107            return this.tItems[i];
108        }
109        /// <summary>
110        /// 获取线性表指定元素的位置
111        /// </summary>
112        /// <param name="item">线性表元素
113        /// <returns>返回线性表位置</returns>
114        public int LocateElement(T item)
115        {
116            if (this.ListCount() == 0)
117            {
118                Console.WriteLine("There Are No Elements In This Linear List!");
119                return 0;
120            }
121            for (int i = 0; i < this.ListCount(); i++)
122            {
123                //比较查找的元素是否与线性表对应的元素相同
124                if (this.tItems[i].Equals(item))
125                {
126                    return i + 1;
127                }
128            }
129            Console.WriteLine("Location Not Exist!");
130            return 0;
131        }
132        /// <summary>
133        /// 清空顺序线性表
134        /// </summary>
135        public void ListClear()
136        {
137            this.elementCount = -1;
138        }
139 
140        //实现foreach遍历
141        public IEnumerator<T> GetEnumerator()
142        {
143            for (int elemIndex = 0; elemIndex < this.ListCount(); elemIndex++)
144            {
145                yield return this.tItems[elemIndex];
146            }
147        }
148        IEnumerator System.Collections.IEnumerable.GetEnumerator()
149        {
150            return GetEnumerator();
151        }
152    }

顺序存储结构插入算法分析:

  ◆   线性表是否已满、插入位置是否合理

  ◆   从最后一个元素开始前向遍历到第 i 个元素位置,分别将它们都向后移动一个位置

  ◆   将要插入元素填入位置 i 处

  ◆   表长加1

/// 
        /// 在指定元素后面插入元素
        /// 
        /// 线性表元素
        /// 插入的元素
        public void ListInsertAfter(T item, T newItem)
        {
            if (IsFull())
            {
                Console.WriteLine("This Linear List Is Full, Can't Insert Any New Items!");
                return;
            }
            //获取线性表指定元素的位置
            int location = LocateElement(item);
            //插入的位置不在表尾
            if (location < this.elementCount && location > 0)
            {
                //将指定元素后面所有元素向后移动一个位置
                for (int i = this.elementCount; i >= location; i--)
                {
                    this.tItems[i + 1] = this.tItems[i];
                }
            }
            //插入元素
            this.tItems[location] = newItem;
            //表长增加1
            this.elementCount++;
        }
        /// 
        /// 在指定元素前面插入元素
        /// 
        /// 线性表元素
        /// 插入的元素
        public void ListInsertBefore(T item, T newItem)
        {
            if (IsFull())
            {
                Console.WriteLine("This Linear List Is Full, Can't Insert Any New Items!");
                return;
            }
            //获取线性表指定元素的位置
            int location = LocateElement(item);
            //插入的位置不在表尾
            if (location > 0)
            {
                ListInsert(newItem, location);
            }
        }
        /// 
        /// 插入操作
        /// 
        /// 插入的项
        /// 插入的位置,从 1 开始
        public void ListInsert(T item, int i)
        {
            // i 不在插入范围
            if (i < 1 || i > this.elementCount + 1)
            {
                Console.WriteLine("This Inserting Location Is Wrong!");
                return;
            }
            //顺序线性表已满
            if (this.IsFull())
            {
                Console.WriteLine("This Linear List Is Full, Can't Insert Any New Items!");
                return;
            }
            //插入的元素不在表尾
            if (i <= this.elementCount)
            {
                //从最后一个元素向前遍历到第i个元素,分别将它们向后移动一个位置
                for (int j = this.elementCount; j >= i - 1; j--)
                {
                    this.tItems[j + 1] = this.tItems[j];
                }
            }
            //将要插入的元素填入位置i处
            tItems[i - 1] = item;
            //表长加1
            this.elementCount++;
        }

顺序存储结构删除算法分析:

  ◆   删除位置是否合理

  ◆   从删除元素位置开始遍历到最后一个元素位置,分别将它们向将移动一个位置

  ◆   取出删除元素

  ◆   表长减1
        /// 
        /// 删除操作
        /// 
        /// 线性表位置,从 1 开始
        /// 返回删除的项
        public T ListDelete(int i)
        {
            if (this.elementCount == -1)
            {
                Console.WriteLine("There Are No Elements In This Linear List!");
                return default(T);
            }
            if (i < 0 || i > this.elementCount)
            {
                Console.WriteLine("Delete Location Is Wrong!");
                return default(T);
            }
            //删除的元素不在表尾
            if (i < this.elementCount)
            {
                //从删除元素位置开始遍历到最后一个元素位置,分别将它们都向前移动一个位置
                for (int j = i; j < this.elementCount; j++)
                {
                    this.tItems[j] = this.tItems[j + 1];
                }
            }
            //表长减1
            this.elementCount--;
            return this.tItems[i - 1];
        }