1. 线性表接口:

public interface ILinearList<T>
{
//ILinearTable<T> Create(int length);
int GetLength();
void SetValues(T data, int positionIndex);
void Insert(T data, int positionIndex);
void Remove(int index);
void Remove(T data, bool isRemoveAll);
void RemoveDuplicate();
}
{
//ILinearTable<T> Create(int length);
int GetLength();
void SetValues(T data, int positionIndex);
void Insert(T data, int positionIndex);
void Remove(int index);
void Remove(T data, bool isRemoveAll);
void RemoveDuplicate();
}
2. 顺序表的实现:

public class SequenceList<T> : ILinearList<T>
{
private const int MAX_LENGTH = 1000;
private int length;
T[] tableData;
private SequenceList(int length)
{
this.tableData = new T[MAX_LENGTH];
this.length = length;
}
public static ILinearList<T> Create(int length)
{
if (length < 1)
throw new ArgumentOutOfRangeException("length must be large than 0.");
return new SequenceList<T>(length);
}
public int GetLength()
{
return length;
}
public void SetValues(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > length)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
tableData[positionIndex] = data;
}
public void Insert(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > length)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
for (int i = length; i > positionIndex; i--)
{
tableData[i] = tableData[i - 1];
}
tableData[positionIndex] = data;
length++;
}
public void Remove(int index)
{
if (index < 0 || index > length)
{
throw new ArgumentOutOfRangeException("index");
}
for (int i = index; i < length - 1; i++)
{
tableData[i] = tableData[i + 1];
}
tableData[length - 1] = default(T);
length--;
}
public void Remove(T data, bool isRemoveAll)
{
if (isRemoveAll)
{
int index = 0;
for (int i = 0; i < length; i++)
{
if (!tableData[i].Equals(data))
{
tableData[index++] = tableData[i];
}
}
for (int i = index; i < length; i++)
{
tableData[i] = default(T);
}
length = index;
}
else
{
int equalIndex = -1;
for (int i = 0; i < length - 1; i++)
{
if (tableData[i].Equals(data))
{
equalIndex = i;
break;
}
}
if (equalIndex > -1)
{
for (int i = equalIndex; i < length - 1; i++)
{
tableData[i] = tableData[i + 1];
}
length--;
}
}
}
public void RemoveDuplicate()
{
if (length < 2)
return;
int count = 1;
for (int i = 1; i < length; i++)
{
bool flag = false;
for (int j = 0; j < count; j++)
{
if (tableData[i].Equals(tableData[j]))
{
flag = true;
break;
}
}
if (!flag)
{
tableData[count++] = tableData[i];
}
}
for (int i = count; i < length; i++)
{
tableData[i] = default(T);
}
length = count;
}
public override string ToString()
{
if (length < 1)
return String.Empty;
StringBuilder str = new StringBuilder();
for (int i = 0; i < length; i++)
{
str.Append(tableData[i].ToString()).Append("--->");
}
return str.ToString().Substring(0, str.Length - 4);
}
}
{
private const int MAX_LENGTH = 1000;
private int length;
T[] tableData;
private SequenceList(int length)
{
this.tableData = new T[MAX_LENGTH];
this.length = length;
}
public static ILinearList<T> Create(int length)
{
if (length < 1)
throw new ArgumentOutOfRangeException("length must be large than 0.");
return new SequenceList<T>(length);
}
public int GetLength()
{
return length;
}
public void SetValues(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > length)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
tableData[positionIndex] = data;
}
public void Insert(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > length)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
for (int i = length; i > positionIndex; i--)
{
tableData[i] = tableData[i - 1];
}
tableData[positionIndex] = data;
length++;
}
public void Remove(int index)
{
if (index < 0 || index > length)
{
throw new ArgumentOutOfRangeException("index");
}
for (int i = index; i < length - 1; i++)
{
tableData[i] = tableData[i + 1];
}
tableData[length - 1] = default(T);
length--;
}
public void Remove(T data, bool isRemoveAll)
{
if (isRemoveAll)
{
int index = 0;
for (int i = 0; i < length; i++)
{
if (!tableData[i].Equals(data))
{
tableData[index++] = tableData[i];
}
}
for (int i = index; i < length; i++)
{
tableData[i] = default(T);
}
length = index;
}
else
{
int equalIndex = -1;
for (int i = 0; i < length - 1; i++)
{
if (tableData[i].Equals(data))
{
equalIndex = i;
break;
}
}
if (equalIndex > -1)
{
for (int i = equalIndex; i < length - 1; i++)
{
tableData[i] = tableData[i + 1];
}
length--;
}
}
}
public void RemoveDuplicate()
{
if (length < 2)
return;
int count = 1;
for (int i = 1; i < length; i++)
{
bool flag = false;
for (int j = 0; j < count; j++)
{
if (tableData[i].Equals(tableData[j]))
{
flag = true;
break;
}
}
if (!flag)
{
tableData[count++] = tableData[i];
}
}
for (int i = count; i < length; i++)
{
tableData[i] = default(T);
}
length = count;
}
public override string ToString()
{
if (length < 1)
return String.Empty;
StringBuilder str = new StringBuilder();
for (int i = 0; i < length; i++)
{
str.Append(tableData[i].ToString()).Append("--->");
}
return str.ToString().Substring(0, str.Length - 4);
}
}
3. 链表的实现:

public class LinkNode<T>
{
public T Data { set; get; }
public LinkNode<T> Next { set; get; }
public LinkNode(){}
public LinkNode(T data, LinkNode<T> next)
{
this.Data = data;
this.Next = next;
}
}
public class LinkList<T> : ILinearList<T>
{
public LinkNode<T> HeadNode { set; get; }
private LinkList(int length)
{
HeadNode = new LinkNode<T>
{
Data = default(T),
Next = null
};
LinkNode<T> node = HeadNode;
for (int i = 0; i < length - 1; i++)
{
node.Next = new LinkNode<T>
{
Data = default(T),
Next = null
};
node = node.Next;
}
}
public static ILinearList<T> Create(int length)
{
if (length < 1)
throw new ArgumentOutOfRangeException("length must be large than 0.");
return new LinkList<T>(length);
}
public int GetLength()
{
if(this.HeadNode == null)
{
return 0;
}
int length = 1;
LinkNode<T> node = HeadNode.Next;
while (node != null)
{
node = node.Next;
length++;
}
return length;
}
public void SetValues(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > this.GetLength() - 1)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
LinkNode<T> node = HeadNode.Next;
for (int i = 1; i < positionIndex; i++)
{
node = node.Next;
}
node.Data = data;
}
public void Insert(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > this.GetLength())
{
throw new ArgumentOutOfRangeException("positionIndex");
}
LinkNode<T> node = null;
if (positionIndex == 0)
{
node = new LinkNode<T>
{
Data = data,
Next = HeadNode
};
HeadNode = node;
}
else
{
node = HeadNode.Next;
for (int i = 1; i < positionIndex - 1; i++)
{
node = node.Next;
}
LinkNode<T> newNode = new LinkNode<T>
{
Data = data,
Next = node.Next
};
node.Next = newNode;
}
}
public void Remove(int index)
{
if (index < 0 || index > this.GetLength() - 1)
{
throw new ArgumentOutOfRangeException("index");
}
if (index == 0)
{
HeadNode = HeadNode.Next;
}
else
{
LinkNode<T> node = HeadNode.Next;
for (int i = 1; i < index - 1; i++)
{
node = node.Next;
}
if (node.Next.Next == null)
{
node.Next = null;
}
else
{
node.Next = node.Next.Next;
}
}
}
public void Remove(T data, bool isRemoveAll)
{
if (this.GetLength() < 1)
return;
LinkNode<T> node = HeadNode.Next, previousNode = HeadNode;
if (isRemoveAll)
{
if (node != null)
{
while (node != null)
{
if (node.Data.Equals(data))
{
previousNode.Next = node.Next;
}
else
{
previousNode = node;
}
node = node.Next;
}
}
if (HeadNode.Data.Equals(data))
{
HeadNode = HeadNode.Next;
}
}
else
{
if (node != null)
{
while (node != null)
{
if (node.Data.Equals(data))
{
previousNode.Next = node.Next;
return;
}
else
{
previousNode = node;
}
node = node.Next;
}
}
if (HeadNode.Data.Equals(data))
{
HeadNode = HeadNode.Next;
}
}
}
public void RemoveDuplicate()
{
if (this.GetLength() < 2)
{
return;
}
LinkNode<T> previousNode = HeadNode;
LinkNode<T> tempNode = null;
while (previousNode.Next != null)
{
tempNode = HeadNode;
while (tempNode != previousNode.Next)
{
if (previousNode.Next.Data.Equals(tempNode.Data))
{
previousNode.Next = previousNode.Next.Next;
break;
}
tempNode = tempNode.Next;
}
previousNode = previousNode.Next;
}
}
public override string ToString()
{
if (this.GetLength() < 1)
return String.Empty;
StringBuilder str = new StringBuilder();
var node = HeadNode;
while (node != null)
{
str.Append(node.Data.ToString()).Append("--->");
node = node.Next;
}
return str.ToString().Substring(0, str.Length - 4);
}
}
{
public T Data { set; get; }
public LinkNode<T> Next { set; get; }
public LinkNode(){}
public LinkNode(T data, LinkNode<T> next)
{
this.Data = data;
this.Next = next;
}
}
public class LinkList<T> : ILinearList<T>
{
public LinkNode<T> HeadNode { set; get; }
private LinkList(int length)
{
HeadNode = new LinkNode<T>
{
Data = default(T),
Next = null
};
LinkNode<T> node = HeadNode;
for (int i = 0; i < length - 1; i++)
{
node.Next = new LinkNode<T>
{
Data = default(T),
Next = null
};
node = node.Next;
}
}
public static ILinearList<T> Create(int length)
{
if (length < 1)
throw new ArgumentOutOfRangeException("length must be large than 0.");
return new LinkList<T>(length);
}
public int GetLength()
{
if(this.HeadNode == null)
{
return 0;
}
int length = 1;
LinkNode<T> node = HeadNode.Next;
while (node != null)
{
node = node.Next;
length++;
}
return length;
}
public void SetValues(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > this.GetLength() - 1)
{
throw new ArgumentOutOfRangeException("positionIndex");
}
LinkNode<T> node = HeadNode.Next;
for (int i = 1; i < positionIndex; i++)
{
node = node.Next;
}
node.Data = data;
}
public void Insert(T data, int positionIndex)
{
if (positionIndex < 0 || positionIndex > this.GetLength())
{
throw new ArgumentOutOfRangeException("positionIndex");
}
LinkNode<T> node = null;
if (positionIndex == 0)
{
node = new LinkNode<T>
{
Data = data,
Next = HeadNode
};
HeadNode = node;
}
else
{
node = HeadNode.Next;
for (int i = 1; i < positionIndex - 1; i++)
{
node = node.Next;
}
LinkNode<T> newNode = new LinkNode<T>
{
Data = data,
Next = node.Next
};
node.Next = newNode;
}
}
public void Remove(int index)
{
if (index < 0 || index > this.GetLength() - 1)
{
throw new ArgumentOutOfRangeException("index");
}
if (index == 0)
{
HeadNode = HeadNode.Next;
}
else
{
LinkNode<T> node = HeadNode.Next;
for (int i = 1; i < index - 1; i++)
{
node = node.Next;
}
if (node.Next.Next == null)
{
node.Next = null;
}
else
{
node.Next = node.Next.Next;
}
}
}
public void Remove(T data, bool isRemoveAll)
{
if (this.GetLength() < 1)
return;
LinkNode<T> node = HeadNode.Next, previousNode = HeadNode;
if (isRemoveAll)
{
if (node != null)
{
while (node != null)
{
if (node.Data.Equals(data))
{
previousNode.Next = node.Next;
}
else
{
previousNode = node;
}
node = node.Next;
}
}
if (HeadNode.Data.Equals(data))
{
HeadNode = HeadNode.Next;
}
}
else
{
if (node != null)
{
while (node != null)
{
if (node.Data.Equals(data))
{
previousNode.Next = node.Next;
return;
}
else
{
previousNode = node;
}
node = node.Next;
}
}
if (HeadNode.Data.Equals(data))
{
HeadNode = HeadNode.Next;
}
}
}
public void RemoveDuplicate()
{
if (this.GetLength() < 2)
{
return;
}
LinkNode<T> previousNode = HeadNode;
LinkNode<T> tempNode = null;
while (previousNode.Next != null)
{
tempNode = HeadNode;
while (tempNode != previousNode.Next)
{
if (previousNode.Next.Data.Equals(tempNode.Data))
{
previousNode.Next = previousNode.Next.Next;
break;
}
tempNode = tempNode.Next;
}
previousNode = previousNode.Next;
}
}
public override string ToString()
{
if (this.GetLength() < 1)
return String.Empty;
StringBuilder str = new StringBuilder();
var node = HeadNode;
while (node != null)
{
str.Append(node.Data.ToString()).Append("--->");
node = node.Next;
}
return str.ToString().Substring(0, str.Length - 4);
}
}
4. 测试:

#region test sequence list
ILinearList<int> linearList = SequenceList<int>.Create(10);
linearList.SetValues(0, 0);
linearList.SetValues(1, 1);
linearList.SetValues(2, 2);
linearList.SetValues(3, 3);
linearList.SetValues(4, 4);
linearList.SetValues(5, 5);
linearList.SetValues(6, 6);
linearList.SetValues(7, 7);
linearList.SetValues(8, 8);
linearList.SetValues(9, 9);
Console.WriteLine(linearList.ToString());
linearList.Insert(5, 6);
Console.WriteLine(linearList.ToString());
linearList.Remove(2);
Console.WriteLine(linearList.ToString());
linearList.Remove(5, true);
Console.WriteLine(linearList.ToString());
linearList.Insert(7, 7);
linearList.Remove(7, false);
Console.WriteLine(linearList.ToString());
linearList.Insert(7, 6);
linearList.RemoveDuplicate();
Console.WriteLine(linearList.ToString());
#endregion
#region test linked list
ILinearList<int> linkList = LinkList<int>.Create(10);
linkList.SetValues(0, 0);
linkList.SetValues(1, 1);
linkList.SetValues(2, 2);
linkList.SetValues(3, 3);
linkList.SetValues(4, 4);
linkList.SetValues(5, 5);
linkList.SetValues(6, 6);
linkList.SetValues(7, 7);
linkList.SetValues(8, 8);
linkList.SetValues(9, 9);
Console.WriteLine(linkList.ToString());
linkList.Insert(5, 6);
Console.WriteLine(linkList.ToString());
linkList.Remove(2);
Console.WriteLine(linkList.ToString());
linkList.Remove(5, true);
Console.WriteLine(linkList.ToString());
linkList.Insert(7, 7);
linkList.Remove(7, false);
Console.WriteLine(linkList.ToString());
linkList.Insert(7, 6);
linkList.RemoveDuplicate();
Console.WriteLine(linkList.ToString());
#endregion
ILinearList<int> linearList = SequenceList<int>.Create(10);
linearList.SetValues(0, 0);
linearList.SetValues(1, 1);
linearList.SetValues(2, 2);
linearList.SetValues(3, 3);
linearList.SetValues(4, 4);
linearList.SetValues(5, 5);
linearList.SetValues(6, 6);
linearList.SetValues(7, 7);
linearList.SetValues(8, 8);
linearList.SetValues(9, 9);
Console.WriteLine(linearList.ToString());
linearList.Insert(5, 6);
Console.WriteLine(linearList.ToString());
linearList.Remove(2);
Console.WriteLine(linearList.ToString());
linearList.Remove(5, true);
Console.WriteLine(linearList.ToString());
linearList.Insert(7, 7);
linearList.Remove(7, false);
Console.WriteLine(linearList.ToString());
linearList.Insert(7, 6);
linearList.RemoveDuplicate();
Console.WriteLine(linearList.ToString());
#endregion
#region test linked list
ILinearList<int> linkList = LinkList<int>.Create(10);
linkList.SetValues(0, 0);
linkList.SetValues(1, 1);
linkList.SetValues(2, 2);
linkList.SetValues(3, 3);
linkList.SetValues(4, 4);
linkList.SetValues(5, 5);
linkList.SetValues(6, 6);
linkList.SetValues(7, 7);
linkList.SetValues(8, 8);
linkList.SetValues(9, 9);
Console.WriteLine(linkList.ToString());
linkList.Insert(5, 6);
Console.WriteLine(linkList.ToString());
linkList.Remove(2);
Console.WriteLine(linkList.ToString());
linkList.Remove(5, true);
Console.WriteLine(linkList.ToString());
linkList.Insert(7, 7);
linkList.Remove(7, false);
Console.WriteLine(linkList.ToString());
linkList.Insert(7, 6);
linkList.RemoveDuplicate();
Console.WriteLine(linkList.ToString());
#endregion