C#数据结构与算法之构造线性表的类有哪些呢?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();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
C#数据结构与算法中构造线性表的类之调用线性表的操作:
TList﹤string﹥ TL = new TList﹤string﹥(5) { };
TL.Append("A");
TL.Append("B");
- 1.
- 2.
- 3.
- 4.
- 5.
大家多多讨论,有不对的地方请多多指正。
C#数据结构与算法之构造线性表的类方面的内容就向大家介绍到这里,希望对大家学习C#数据结构与算法之构造线性表的类有所帮助。
【编辑推荐】