线性表的接口定义及使用

定义接口

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;
        }
    }
}
相关推荐
zhangyao94033012 小时前
关于js导入Excel时,Excel的(年/月/日)日期是五位数字的问题。以及对Excel日期存在的错误的分析和处理。
开发语言·javascript·excel
熙客12 小时前
TiDB:分布式关系型数据库
java·数据库·分布式·tidb
骑驴看星星a12 小时前
【Three.js--manual script】4.光照
android·开发语言·javascript
星释13 小时前
Rust 练习册 :Leap与日期计算
开发语言·后端·rust
悟能不能悟15 小时前
java的java.sql.Date和java.util.Date的区别,应该怎么使用
java·开发语言
循环过三天16 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
你想考研啊16 小时前
oracle导出 导入
数据库·oracle
_院长大人_17 小时前
设计模式-工厂模式
java·开发语言·设计模式
MATLAB代码顾问17 小时前
MATLAB实现决策树数值预测
开发语言·决策树·matlab
韩立学长18 小时前
基于Springboot的旧时月历史论坛4099k6s9(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端