线性表的接口定义及使用

定义接口

cs 复制代码
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 tiem, int index);
        T Delete(int index);
        T this[int index] { get; }
        T GetEle(int index);
        int Locate(T value);
    }
}

使用顺序表

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_线性表
{
    /// <summary>
    /// 
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            /// <summary>
            /// 使用BCL中的线性表
            /// 三个字符串具有索引的先后关系,可以通过索引访问元素
            /// </summary>
            //List<string> strList = new List<string>();
            //strList.Add("123");//0
            //strList.Add("456");//1
            //strList.Add("789");//2
            //Console.WriteLine(strList[1]);
            //strList.Remove("789");//移除
            //Console.WriteLine(strList.Count);//大小
            //strList.Clear();//清除
            //Console.WriteLine(strList.Count);
            //Console.ReadKey();


            //使用我们自己的顺序表
            SeqList<string> seqList = new SeqList<string> ();
            seqList.Add("123");
            seqList.Add("456");
            seqList.Add("789");

            Console.WriteLine(seqList.GetEle(0));
            Console.WriteLine(seqList[0]);//通过索引器
            seqList.Insert("777", 1);
            for(int i=0;i<seqList.GetLength ();i++)
            {
                Console.Write(seqList[i] + " ");
            }
            Console.WriteLine();
            seqList.Delete(0);
            seqList.Clear();
            Console.WriteLine(seqList.GetLength());
            Console.ReadKey();
        }
    }
}

顺序表实现方式

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_线性表
{
    //顺序表实现方式
    class SeqList<T> : IListDS<T>
    {
        private T[] data;//用来存储数据
        private int count = 0;//数据个数

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="size"></param>
        public SeqList(int size)//size是最大容量
        {
            data = new T[size];
            count = 0;
        }
        public SeqList ():this(10)//默认构造函数容量为10
        {

        }
        //public T this[int index] => throw new NotImplementedException();

        public void Add(T item)
        {
            if(count == data.Length )
            {
                Console.WriteLine("当前顺序表已经存满");
            }
            else
            {
                data[count] = item;
                count++;
            }
        }

        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 this[int index]
        {
            get { return GetEle(index); }
        }

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

        public int GetLength()
        {
            return count;
        }

        public void Insert(T item, int index)
        {
            for(int i=count-1;i>=index;i--)//从后往前遍历,防止数据被覆盖
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            count++;
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public int Locate(T value)
        {
            for(int i=0;i<count;i++)
            {
                if(data[i].Equals (value ))
                {
                    return i;
                }
                
            }
            return -1;
        }
    }
}
相关推荐
小白考证进阶中几秒前
0基础可以考MySQL OCP么?备考时间需要多久?
数据库·mysql·开闭原则
观无6 分钟前
Redis远程链接应用案例
数据库·redis·缓存·c#
yuanpan10 分钟前
C#如何正确的停止一个多线程Task?CancellationTokenSource 的用法。
开发语言·c#
程高兴12 分钟前
单相交直交变频电路设计——matlab仿真+4500字word报告
开发语言·matlab
星星点点洲13 分钟前
【缓存与数据库结合方案】伪从技术 vs 直接同步/MQ方案的深度对比
数据库·缓存
努力奋斗的小杨17 分钟前
学习MySQL的第十二天
数据库·笔记·学习·mysql·navicat
8RTHT26 分钟前
数据结构(七)---链式栈
数据结构
我真的不会C1 小时前
QT中的事件及其属性
开发语言·qt
枫叶20001 小时前
OceanBase数据库-学习笔记1-概论
数据库·笔记·学习·oceanbase
仲夏plus1 小时前
MySQL:慢SQL索引优化-使用explain/analyze进行耗时分析的方法
数据库