线性表的接口定义及使用

打印 上一主题 下一主题

主题 847|帖子 847|积分 2541

定义接口
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _001_线性表
  7. {
  8.     interface IListDS<T>//定义接口
  9.     {
  10.         int GetLength();
  11.         void Clear();
  12.         bool IsEmpty();
  13.         void Add(T item);
  14.         void Insert(T tiem, int index);
  15.         T Delete(int index);
  16.         T this[int index] { get; }
  17.         T GetEle(int index);
  18.         int Locate(T value);
  19.     }
  20. }
复制代码
使用顺序表
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _001_线性表
  7. {
  8.     /// <summary>
  9.     ///
  10.     /// </summary>
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             /// <summary>
  16.             /// 使用BCL中的线性表
  17.             /// 三个字符串具有索引的先后关系,可以通过索引访问元素
  18.             /// </summary>
  19.             //List<string> strList = new List<string>();
  20.             //strList.Add("123");//0
  21.             //strList.Add("456");//1
  22.             //strList.Add("789");//2
  23.             //Console.WriteLine(strList[1]);
  24.             //strList.Remove("789");//移除
  25.             //Console.WriteLine(strList.Count);//大小
  26.             //strList.Clear();//清除
  27.             //Console.WriteLine(strList.Count);
  28.             //Console.ReadKey();
  29.             //使用我们自己的顺序表
  30.             SeqList<string> seqList = new SeqList<string> ();
  31.             seqList.Add("123");
  32.             seqList.Add("456");
  33.             seqList.Add("789");
  34.             Console.WriteLine(seqList.GetEle(0));
  35.             Console.WriteLine(seqList[0]);//通过索引器
  36.             seqList.Insert("777", 1);
  37.             for(int i=0;i<seqList.GetLength ();i++)
  38.             {
  39.                 Console.Write(seqList[i] + " ");
  40.             }
  41.             Console.WriteLine();
  42.             seqList.Delete(0);
  43.             seqList.Clear();
  44.             Console.WriteLine(seqList.GetLength());
  45.             Console.ReadKey();
  46.         }
  47.     }
  48. }
复制代码
顺序表实现方式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _001_线性表
  7. {
  8.     //顺序表实现方式
  9.     class SeqList<T> : IListDS<T>
  10.     {
  11.         private T[] data;//用来存储数据
  12.         private int count = 0;//数据个数
  13.         /// <summary>
  14.         /// 构造方法
  15.         /// </summary>
  16.         /// <param name="size"></param>
  17.         public SeqList(int size)//size是最大容量
  18.         {
  19.             data = new T[size];
  20.             count = 0;
  21.         }
  22.         public SeqList ():this(10)//默认构造函数容量为10
  23.         {
  24.         }
  25.         //public T this[int index] => throw new NotImplementedException();
  26.         public void Add(T item)
  27.         {
  28.             if(count == data.Length )
  29.             {
  30.                 Console.WriteLine("当前顺序表已经存满");
  31.             }
  32.             else
  33.             {
  34.                 data[count] = item;
  35.                 count++;
  36.             }
  37.         }
  38.         public void Clear()
  39.         {
  40.             count = 0;
  41.         }
  42.         public T Delete(int index)
  43.         {
  44.             T temp = data[index];
  45.             for(int i=index+1;i<count;i++)
  46.             {
  47.                 data[i - 1] = data[i];
  48.             }
  49.             count--;
  50.             return temp;
  51.         }
  52.         public T this[int index]
  53.         {
  54.             get { return GetEle(index); }
  55.         }
  56.         public T GetEle(int index)
  57.         {
  58.             if(index >=0 && index <=count-1)//索引存在
  59.             {
  60.                 return data[index];
  61.             }
  62.             else
  63.             {
  64.                 Console.WriteLine("索引不存在");
  65.                 return default(T);
  66.             }
  67.         }
  68.         public int GetLength()
  69.         {
  70.             return count;
  71.         }
  72.         public void Insert(T item, int index)
  73.         {
  74.             for(int i=count-1;i>=index;i--)//从后往前遍历,防止数据被覆盖
  75.             {
  76.                 data[i + 1] = data[i];
  77.             }
  78.             data[index] = item;
  79.             count++;
  80.         }
  81.         public bool IsEmpty()
  82.         {
  83.             return count == 0;
  84.         }
  85.         public int Locate(T value)
  86.         {
  87.             for(int i=0;i<count;i++)
  88.             {
  89.                 if(data[i].Equals (value ))
  90.                 {
  91.                     return i;
  92.                 }
  93.                
  94.             }
  95.             return -1;
  96.         }
  97.     }
  98. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表