线性结构之顺序表(Sequence List)
直接实现IList<T>接口,而不自己定义接口。没有做表的自动扩张。从代码可以看出,顺序表应该谨慎使用Insert和Remove操作。
/// <summary>
/// 线性结构之顺序表(Sequence List)
/// </summary>
public class List<T> : IList<T> {
/// <summary>
/// 数据
/// </summary>
protected T[] buffer;
/// <summary>
/// 指针
/// </summary>
protected int last;
/// <summary>
/// 表长度
/// </summary>
protected int size;
/// <summary>
/// 默认初始化100个大小
/// </summary>
public List()
: this(100) {
}
/// <summary>
/// 初始化顺序表
/// </summary>
/// <param name="size">顺序表大小</param>
public List(int size) {
buffer = new T[size];
last = -1;
this.size = size;
}
/// <summary>
/// 返回表长度
/// </summary>
public int MaxSize { get { return size; } }
#region IList<T> 成员
/// <summary>
/// 搜索表
/// </summary>
/// <param name="item">元素值</param>
/// <returns>索引,如果没有找到,返回-1</returns>
public int IndexOf(T item) {
int i = 0;
for (i = 0; i <= last; i++) {
if (item.Equals(buffer[i]))
break;
}
if (i > last)
return -1;
return i;
}
/// <summary>
/// 在索引位置插入值
/// </summary>
/// <param name="index">索引位置</param>
/// <param name="item">值</param>
public void Insert(int index, T item) {
if (index == last + 2)
Add(item);
else {
for (int i = last; i >= index; i--) {
buffer[i + 1] = buffer[i];
}
buffer[index] = item;
++last;
}
}
/// <summary>
/// 删除索引位置的值
/// </summary>
/// <param name="index">索引位置</param>
public void RemoveAt(int index) {
for (int i = index; i < last; i++) {
buffer[i] = buffer[i + 1];
}
--last;
}
/// <summary>
/// 索引器
/// </summary>
/// <param name="index">索引位置</param>
/// <returns>返回值</returns>
public T this[int index] {
get {
return buffer[index];
}
set {
buffer[index] = value;
}
}
#endregion
#region ICollection<T> 成员
/// <summary>
/// 在尾部添加
/// </summary>
/// <param name="item"></param>
public void Add(T item) {
buffer[++last] = item;
}
/// <summary>
/// 清空
/// </summary>
public void Clear() {
last = -1;
}
/// <summary>
/// 是否存在值
/// </summary>
/// <param name="item">值</param>
/// <returns>是否存在</returns>
public bool Contains(T item) {
if (IndexOf(item) > -1)
return true;
return false;
}
[Obsolete("未实现该方法。")]
public void CopyTo(T[] array, int arrayIndex) {
throw new NotImplementedException();
}
/// <summary>
/// 当前使用长度
/// </summary>
public int Count {
get { return last + 1; }
}
public bool IsReadOnly {
get { return false; }
}
/// <summary>
/// 删除元素
/// </summary>
/// <param name="item">元素值</param>
/// <returns>是否删除成功</returns>
public bool Remove(T item) {
int index = IndexOf(item);
RemoveAt(index);
return true;
}
#endregion
#region IEnumerable<T> 成员
/// <summary>
/// 实现遍历器
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < Count; i++) {
yield return buffer[i];
}
}
#endregion
#region IEnumerable 成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return this as IEnumerator;
}
#endregion
}
/// 线性结构之顺序表(Sequence List)
/// </summary>
public class List<T> : IList<T> {
/// <summary>
/// 数据
/// </summary>
protected T[] buffer;
/// <summary>
/// 指针
/// </summary>
protected int last;
/// <summary>
/// 表长度
/// </summary>
protected int size;
/// <summary>
/// 默认初始化100个大小
/// </summary>
public List()
: this(100) {
}
/// <summary>
/// 初始化顺序表
/// </summary>
/// <param name="size">顺序表大小</param>
public List(int size) {
buffer = new T[size];
last = -1;
this.size = size;
}
/// <summary>
/// 返回表长度
/// </summary>
public int MaxSize { get { return size; } }
#region IList<T> 成员
/// <summary>
/// 搜索表
/// </summary>
/// <param name="item">元素值</param>
/// <returns>索引,如果没有找到,返回-1</returns>
public int IndexOf(T item) {
int i = 0;
for (i = 0; i <= last; i++) {
if (item.Equals(buffer[i]))
break;
}
if (i > last)
return -1;
return i;
}
/// <summary>
/// 在索引位置插入值
/// </summary>
/// <param name="index">索引位置</param>
/// <param name="item">值</param>
public void Insert(int index, T item) {
if (index == last + 2)
Add(item);
else {
for (int i = last; i >= index; i--) {
buffer[i + 1] = buffer[i];
}
buffer[index] = item;
++last;
}
}
/// <summary>
/// 删除索引位置的值
/// </summary>
/// <param name="index">索引位置</param>
public void RemoveAt(int index) {
for (int i = index; i < last; i++) {
buffer[i] = buffer[i + 1];
}
--last;
}
/// <summary>
/// 索引器
/// </summary>
/// <param name="index">索引位置</param>
/// <returns>返回值</returns>
public T this[int index] {
get {
return buffer[index];
}
set {
buffer[index] = value;
}
}
#endregion
#region ICollection<T> 成员
/// <summary>
/// 在尾部添加
/// </summary>
/// <param name="item"></param>
public void Add(T item) {
buffer[++last] = item;
}
/// <summary>
/// 清空
/// </summary>
public void Clear() {
last = -1;
}
/// <summary>
/// 是否存在值
/// </summary>
/// <param name="item">值</param>
/// <returns>是否存在</returns>
public bool Contains(T item) {
if (IndexOf(item) > -1)
return true;
return false;
}
[Obsolete("未实现该方法。")]
public void CopyTo(T[] array, int arrayIndex) {
throw new NotImplementedException();
}
/// <summary>
/// 当前使用长度
/// </summary>
public int Count {
get { return last + 1; }
}
public bool IsReadOnly {
get { return false; }
}
/// <summary>
/// 删除元素
/// </summary>
/// <param name="item">元素值</param>
/// <returns>是否删除成功</returns>
public bool Remove(T item) {
int index = IndexOf(item);
RemoveAt(index);
return true;
}
#endregion
#region IEnumerable<T> 成员
/// <summary>
/// 实现遍历器
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < Count; i++) {
yield return buffer[i];
}
}
#endregion
#region IEnumerable 成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return this as IEnumerator;
}
#endregion
}
/// <summary>
///
/// </summary>
[TestFixture]
public class ListTest {
[Test]
public void RunTest() {
BCD.List<int> list = new global::BHLib.Core.DataStructure.List<int>();
if (list.MaxSize >= 10) {
for (int i = 0; i < 10; i++) {
list.Add(i);
}
for (int i = 0; i < 10; i++) {
Assert.AreEqual(list[i], i);
}
}
if (list.MaxSize >= 11) {
list.Insert(5, 100);
for (int i = 0; i < 11; i++) {
if (i < 5)
Assert.AreEqual(list[i], i);
else if (i == 5)
Assert.AreEqual(list[i], 100);
else
Assert.AreEqual(list[i], i - 1);
}
}
list.RemoveAt(5);
for (int i = 0; i < 10; i++) {
Assert.AreEqual(list[i], i);
}
if (list.MaxSize >= 100) {
for (int i = 10; i < 100; i++) {
list.Add(i);
}
Assert.AreEqual(list.Count, 100);
}
if (list.MaxSize > list.Count) {
list.Insert(5, 1000);
Assert.AreEqual(true, false);
}
list.Remove(1);
Assert.AreEqual(list.Count, 99);
Assert.AreEqual(list.IndexOf(100), -1);
Assert.AreEqual(list.Contains(100), false);
list.Clear();
for (int i = 0; i < 10; i++) {
list.Add(i);
}
IEnumerator<int> etor = list.GetEnumerator();
int value = 0;
while (etor.MoveNext()) {
Assert.AreEqual(etor.Current, value++);
}
list.Insert(list.Count + 1, 1000);
Assert.AreEqual(list[list.Count - 1], 1000);
}
}
///
/// </summary>
[TestFixture]
public class ListTest {
[Test]
public void RunTest() {
BCD.List<int> list = new global::BHLib.Core.DataStructure.List<int>();
if (list.MaxSize >= 10) {
for (int i = 0; i < 10; i++) {
list.Add(i);
}
for (int i = 0; i < 10; i++) {
Assert.AreEqual(list[i], i);
}
}
if (list.MaxSize >= 11) {
list.Insert(5, 100);
for (int i = 0; i < 11; i++) {
if (i < 5)
Assert.AreEqual(list[i], i);
else if (i == 5)
Assert.AreEqual(list[i], 100);
else
Assert.AreEqual(list[i], i - 1);
}
}
list.RemoveAt(5);
for (int i = 0; i < 10; i++) {
Assert.AreEqual(list[i], i);
}
if (list.MaxSize >= 100) {
for (int i = 10; i < 100; i++) {
list.Add(i);
}
Assert.AreEqual(list.Count, 100);
}
if (list.MaxSize > list.Count) {
list.Insert(5, 1000);
Assert.AreEqual(true, false);
}
list.Remove(1);
Assert.AreEqual(list.Count, 99);
Assert.AreEqual(list.IndexOf(100), -1);
Assert.AreEqual(list.Contains(100), false);
list.Clear();
for (int i = 0; i < 10; i++) {
list.Add(i);
}
IEnumerator<int> etor = list.GetEnumerator();
int value = 0;
while (etor.MoveNext()) {
Assert.AreEqual(etor.Current, value++);
}
list.Insert(list.Count + 1, 1000);
Assert.AreEqual(list[list.Count - 1], 1000);
}
}