数据结构-顺序表

大学以来一直没怎么认真学过数据结构,现在找工作了 都看重 学过数据结构和算法,所以现在开始认真学。

实现:接口(往后的链表文章都是使用这个接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_线性表
{
    interface IListDS<T>
    {
        int GetLength();
        void Clear();
        bool IsEmpty();
        void Add(T item);
        void Insert(T item, int index);
        T Delete(int index);
        T this[int index] { get; }
        T GetEle(int index);
        int Locate(T value);

    }
}

顺序表实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_线性表
{
    /// <summary>
    /// 顺序表 实现方式
    /// </summary>
    class SeqList<T> : IListDS<T>
    {
        private T[] data;//存储数据
        private int count = 0;//存了多少数据
        public SeqList(int size)  //size最大容量
        {
            data = new T[size];
            count = 0;
        }
        public SeqList() : this(10)//默认构造函数 容量是10
        {

        }

        /// <summary>
        /// 返回指定序列号得元素 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index] => GetEle(index);

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            if (count == data.Length)//当前数组已经存满
            {
                Console.WriteLine("当前顺序表已经存满,不允许再存入");
            }
            else
            {
                data[count] = item;
                count++;  
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            count = 0;

        }

        public T Delete(int index)
        {
            T temp = data[index];
            for (int i = index+1; i < count; i++) //
            {
                data[i-1] = data[i];//把数据向前移动
            }
            count--;
            return temp;
        }

        public T GetEle(int index)
        {
            if(count-1>=index && index>=0)//索引存在  
            {
                 return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
         
        }

        /// <summary>
        /// 取得数据个数
        /// </summary>
        /// <returns></returns>
        public int GetLength()
        {
            return count;  
        }


        /// <summary>
        /// 插入元素
        /// </summary>
        /// <param name="item"></param>
        /// <param name="index"></param>
        public void Insert(T item, int index)
        {
            for (int i = count - 1; i >= index; i--) //反序遍历  从后面开始移动元素
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            count++;
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return count == 0;
        }

        /// <summary>
        /// 寻找该值所在得位置
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Locate(T value)
        {
            for (int i = 0; i < count; i++)
            {
                if (data[i].Equals(value))
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
原文地址:https://www.cnblogs.com/rongweijun/p/8072564.html