线性表的接口定义及使用

定义接口

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;
        }
    }
}
相关推荐
文牧之1 小时前
MySQL的字符集(Character Set)和排序规则(Collation)
运维·数据库·mysql
Excuse_lighttime1 小时前
选择排序
java·开发语言·数据结构·算法·排序算法
Excuse_lighttime1 小时前
插入排序和希尔排序
java·开发语言·数据结构·算法·排序算法
☆cwlulu1 小时前
一句话总结一种排序算法,精炼
数据结构·算法·排序算法
胡晔可可1 小时前
数据库中存储时候将字段为空串时转换成null
java·数据库
爱是小小的癌1 小时前
数据结构与算法之排序算法-快速排序(分治)
java·开发语言·数据结构·算法·排序算法
✿ ༺ ོIT技术༻1 小时前
剑指offer第2版:搜索算法(二分/DFS/BFS)
数据结构·算法
技术小泽1 小时前
算法基础之排序算法大总结1!!
java·数据结构·后端·算法·排序算法
不能只会打代码2 小时前
Python人工智能技术全景:从基础框架到DeepSeek的突破性创新
开发语言·人工智能·python·deepseek
m0_663234012 小时前
flask后端开发(8):Flask连接MySQL数据库+ORM增删改查
数据库·mysql·flask