写一个MyList

时间:2022-10-02 07:06:11

首先定义接口

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

namespace _001_线性表
{
    interface IListDs<T>
    {
        int GetLenght();
        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>
    /// <typeparam name="T"></typeparam>
    class SeqList<T> : IListDs<T>
    {
        private T[] data; //存储数据
        private int count;//表示存了多少个数据

        public SeqList(int size)//创建数据
        {
            data = new T[size];
            count = ;
        }
        ){}//默认

        public T this[int index]
        {
            get
            {
                return 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 = ;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T Delete(int index)
        {
            T temp = data[index];
             && index <= count - )
            {
                ; i < count; i++)
                {
                    data[i - ] = data[i];//把数据向前移动
                }
                count--;
                return temp;
            }
            else
            {
                return default(T);
                Console.WriteLine("不存在索引");
            }
        }
        /// <summary>
        /// 获取元素
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T GetEle(int index)
        {
             && index <= count - )
            {
                return data[index];
            }
            else
            {
                Console.WriteLine("索引不存在");
                return default(T);
            }
        }

        /// <summary>
        /// 取得数据得长度
        /// </summary>
        /// <returns></returns>
        public int GetLenght()
        {
            return count;
        }
        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="item"></param>
        /// <param name="index"></param>
        public void Insert(T item, int index)
        {
             && index <= count - )
            {
                for (int i = count; i >= index; i--)
                {
                    data[i + ] = data[i];
                }
                data[index] = item;
                count++;
            }
            else
            {
                Console.WriteLine("不存在索引");
            }

        }

        public bool isEmpty()
        {
            ;
        }
        /// <summary>
        /// 数据的位置
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Locate(T value)
        {
            ; i < count; i++)
            {
                if (i.Equals(value))
                {
                    return i;
                }
            }
            ;
        }
    }
}

测试

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

namespace _001_线性表
{
    class Program
    {
        static void Main(string[] args)
        {
            SeqList<string> seqList = new SeqList<string>();
            seqList.Add(");
            seqList.Add("1zxc");
            seqList.Add("asd3");
            Console.WriteLine(seqList[]);
            Console.WriteLine(seqList.GetEle());

            seqList.Insert();
            ; i < seqList.GetLenght(); i++)
            {
                Console.Write(seqList[i]+" ");
            }
            Console.WriteLine();
            seqList.Delete();
            ; i < seqList.GetLenght(); i++)
            {
                Console.Write(seqList[i] + " ");
            }
            Console.WriteLine();
            seqList.Clear();
            Console.WriteLine(seqList.GetLenght());
        }
    }
}