C#中构造线性表的类有哪些

发布时间:2021-12-01 13:49:09 作者:小新
来源:亿速云 阅读:102

这篇文章将为大家详细讲解有关C#中构造线性表的类有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

让我们来看看C#数据结构与算法之构造线性表的类的代码使用:

public interface IListDS﹤T﹥   {      int GetLength();      void Clear();      bool IsEmpty();      bool IsFull();      void Append(T item);      void Insert(T item, int i);      T Delete(int i);      T GetElem(int i);      string Locate(T value);   }   public class TList﹤T﹥ : IListDS﹤T﹥  {      private T[] _list;      private int _len;      private int _lastOne;       public T this[int length]      {          get { return _list[length]; }          set { _list[length] = value; }      }       public int List      {          get { return _lastOne; }      }       public int Maxsize      {          get { return _len; }          set { _len = value; }      }       public TList(int size)      {          _list = new T[size];          _len = size;          _lastOne = -1;      }       public int GetLength()      {          return _lastOne + 1;      }       public bool IsEmpty()      {          if (_lastOne == -1)          { return true; }          else         { return false; }      }       public void Clear()      {          _lastOne = -1;      }       public bool IsFull()      {          if (_lastOne == _len - 1)          { return true; }          else         { return false; }      }       public void Append(T item)      {          if (IsFull())          {               throw new ArgumentOutOfRangeException("The list is full!");           }          _list[++_lastOne] = item;              }       public void Insert(T item, int i)      {          if (IsFull())          {              throw new ArgumentOutOfRangeException("The list is full!");          }          if (i ﹤ 0 || i ﹥ _len)          {              throw new ArgumentOutOfRangeException("Position Error!");          }           if (i == _lastOne)          {              _list[++_lastOne] = item;          }          else         {              for (int j = i; j ﹤ _len - 1; j++)              {                  _list[j + 1] = _list[j];              }              _list[i] = item;          }          ++_lastOne;      }       public T Delete(int i)       {          T t = default(T);          if (IsEmpty())          {              throw new ArgumentNullException("T", "List is empty!");          }          if (i ﹤ 0 || i ﹥ _lastOne)          {              throw new ArgumentOutOfRangeException("T", "Position is Error!");          }          if (i == _lastOne)          {              t = _list[_lastOne - 1];          }          else         {              t = _list[_lastOne];              for (int j = i; j ﹤ _lastOne; j++)              {                  _list[j] = _list[j + 1];              }          }          --_lastOne;          return t;      }       public T GetElem(int i)      {          if (IsEmpty())          {              throw new ArgumentNullException("T", "List is empty!");          }          if (i ﹤ 0 || i ﹥ _len)          {              throw new ArgumentOutOfRangeException("Position is Error!");          }           return _list[i];      }       public string Locate(T value)      {          if (IsEmpty())          {              throw new ArgumentNullException("T", "List is empty!");          }          int i = 0;          for (i = 0; i ﹤ _len; i++)          {              if (value.Equals(_list[i]))              {                   break;              }          }          if (i ﹥= _len)          {              return "-1";          }          return i.ToString();      }  }

C#数据结构与算法中构造线性表的类之调用线性表的操作:

TList﹤string﹥ TL = new TList﹤string﹥(5) { };   TL.Append("A");   TL.Append("B");

关于“C#中构造线性表的类有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. c#线性表中链表怎么用
  2. C#中的类02:构造对象

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:SQL Server日期函数CAST和CONVERT以及在业务中的使用是怎样的

下一篇:C#匿名方法怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》