波斯马BOSSMA Information Technology

C#线性表的顺序存储结构

发布时间:2008年7月28日 / 分类:DOTNET / 11,716 次浏览 / 评论

模仿Java线性表的顺序存储结构。
Main Functions:IsEmpty,IsFull,Get Length,Get Item,Set Item,Is Contains,Get Value,Index,Insert,Remove

public class LinerList
??? {
??????? private int[] _lists;
??????? private int _length;

??????? ///
??????? /// Constructor,Initialization
????? ///
??????? ///

??????? public LinerList(int length)
??????? {
??????????? _lists = new int[length];
??????????? this._length = 0;
??????? }

??????? ///
??????? /// 判断是否为空
??????? ///
??????? ///
??????? public bool IsEmpty()
??????? {
??????????? return _length == 0;
??????? }

??????? ///
??????? /// 判断是否已满
??????? ///
??????? ///
??????? public bool IsFull()
??????? {
??????????? return _length >= _lists.Length;
??????? }

??????? ///
??????? /// 获取线性的长度
??????? ///
??????? ///
??????? public int Length()
??????? {
??????????? return _length;
??????? }

??????? ///
??????? /// 获取指定位置positionIndex的值
??????? ///
??????? ///

??????? ///
??????? public int Get(int positionIndex)
??????? {
??????????? if (positionIndex > 0 && positionIndex <= _length)
??????????? {
??????????????? return _lists[positionIndex - 1];
??????????? }
??????????? else
??????????? {
??????????????? return -1;
??????????? }
??????? }

??????? ///
??????? /// 设置指定位置positionIndex的值value
??????? ///
??????? ///

??????? ///

??????? public void Set(int positionIndex, int value)
??????? {
??????????? if (positionIndex > 0 && positionIndex <= _length + 1)
??????????? {
??????????????? _lists[positionIndex - 1] = value;

??????????????? if (positionIndex == _length + 1)
??????????????????? _length++;
??????????? }
??????? }

??????? ///
??????? /// 判断是否包含指定的值
??????? ///
??????? ///

??????? ///
??????? public bool Contains(int value)
??????? {
??????????? if (IndexOf(value) > -1)
??????????????? return true;
??????????? else
??????????????? return false;
??????? }

??????? ///
??????? /// 返回指定的值的位置
??????? ///
??????? ///

??????? ///
??????? public int IndexOf(int value)
??????? {
??????????? int i = 0;
??????????? while (i < _length && _lists[i] != value)
??????????????? i++;

??????????? if (i >= 0 && i < _length)
??????????????? return i;
??????????? else
??????????????? return -1;
??????? }

??????? ///
??????? /// 在指定位置插入一个值
??????? ///
??????? ///

??????? public void Insert(int positionIndex, int value)
??????? {
??????????? int j;
??????????? if (!IsFull())
??????????? {
??????????????? if (positionIndex < 0)
??????????????????? positionIndex = 1;

??????????????? if (positionIndex > _length)
??????????????????? positionIndex = _length + 1;

??????????????? for (j = _length - 1; j >= positionIndex - 1; j--)
??????????????????? _lists[j + 1] = _lists[j];

??????????????? _lists[positionIndex - 1] = value;

??????????????? _length++;

??????????? }
??????? }

??????? ///
??????? /// 删除指定的值
??????? ///
??????? ///

??????? public void Remove(int value)
??????? {
??????????? int i = IndexOf(value);

??????????? if (i &amp;gt; -1)
??????????? {
??????????????? for (int j = i; j < _length-1; j++)
??????????????? {
??????????????????? _lists[j] = _lists[j + 1];
??????????????? }

??????????????? _lists[_length - 1] = 0;
??????????????? _length--;
??????????? }
??????????? else
??????????? {
??????????????? throw new System.IndexOutOfRangeException();
??????????? }
??????? }
}

//测试
?protected void Page_Load(object sender, EventArgs e)
??? {
??????? StringBuilder sb = new StringBuilder();

??????? LinerList ll = new LinerList(5);

??????? sb.Append("初始化顺序表长度为5
");
??????? sb.Append("返回顺序表的长度为:");
??????? sb.Append(ll.Length().ToString());
??????? sb.Append("
");

??????? sb.Append("Insert By Sort
");
??????? ll.Insert(1, 1);
??????? ll.Insert(2, 2);
??????? ll.Insert(3, 3);
??????? ll.Insert(4, 4);
??????? ll.Insert(5, 5);

??????? sb.Append("返回第2个元素的值:");
??????? sb.Append(ll.Get(2).ToString());
??????? sb.Append("
");

??????? sb.Append("设置第2个元素的值为2
");
??????? ll.Set(2, 2);

??????? sb.Append("返回第2个元素的值:");
??????? sb.Append(ll.Get(2).ToString());
??????? sb.Append("
");

??????? sb.Append("判断是否包含2:");
??????? sb.Append(ll.Contains(2).ToString());
??????? sb.Append("
");

??????? sb.Append("设置第4个元素的值为8
");
??????? ll.Set(4, 8);

??????? sb.Append("返回第4个元素的值:");
??????? sb.Append(ll.Get(4).ToString());
??????? sb.Append("
");

??????? sb.Append("返回顺序表的长度为:");
??????? sb.Append(ll.Length().ToString());
??????? sb.Append("
");

??????? sb.Append("删除值为8的元素
");
??????? ll.Remove(8);

??????? sb.Append("返回顺序表的长度为:");
??????? sb.Append(ll.Length().ToString());
??????? sb.Append("
");

??????? sb.Append("插入值为6的元素在位置5
");
??????? ll.Insert(5, 6);

??????? sb.Append("返回顺序表的长度为:");
??????? sb.Append(ll.Length().ToString());
??????? sb.Append("
");

??????? sb.Append("删除值为6的元素
");
??????? ll.Remove(6);

??????? sb.Append("插入值为8的元素在位置4
");
??????? ll.Insert(4, 8);

??????? sb.Append("返回第4个元素的值:");
??????? sb.Append(ll.Get(4).ToString());
??????? sb.Append("
");

??????? sb.Append("返回顺序表的长度为:");
??????? sb.Append(ll.Length().ToString());
??????? sb.Append("
");

??????? Label1.Text = sb.ToString();

??? }
本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯马,原文地址《C#线性表的顺序存储结构

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

发表评论