线性表的接口定义及使用

定义接口

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;
        }
    }
}
相关推荐
念何架构之路16 分钟前
Go进阶之panic
开发语言·后端·golang
亓才孓20 分钟前
[Properties]写配置文件前,必须初始化Properties(引用变量没执行有效对象,调用方法会报空指针错误)
开发语言·python
傻乐u兔24 分钟前
C语言进阶————指针3
c语言·开发语言
Σίσυφος190028 分钟前
PCL法向量估计 之 方向约束法向量(Orientation Guided Normal)
数据库
老毛肚31 分钟前
手写mybatis
java·数据库·mybatis
两点王爷34 分钟前
Java基础面试题——【Java语言特性】
java·开发语言
海山数据库37 分钟前
移动云大云海山数据库(He3DB)postgresql_anonymizer插件原理介绍与安装
数据库·he3db·大云海山数据库·移动云数据库
云飞云共享云桌面40 分钟前
高性能图形工作站的资源如何共享给10个SolidWorks研发设计用
linux·运维·服务器·前端·网络·数据库·人工智能
Swift社区41 分钟前
Gunicorn 与 Uvicorn 部署 Python 后端详解
开发语言·python·gunicorn
2501_927993531 小时前
SQL Server 2022安装详细教程(图文详解,非常详细)
数据库·sqlserver