C#数据结构三:单链表Singly Linked List
/// <summary>
/// 单链表
/// </summary>
public class SinglyLinkList
{
public SinglyLinkList()
{
SinglyLinkedList<string> demoList = new SinglyLinkedList<string>();
/*添加*/
demoList.Append("Wang Hongjian");
demoList.Append("ZhangSan");
demoList.Append("LiSi");
for (int i = 0; i < demoList.GetLength(); i++)
{
Console.WriteLine("The {0} item is:\t{1}", i, demoList.GetElem(i));
}
/*插入*/
Console.WriteLine("Insert the item:");
demoList.Insert("Zhangyu", 1);
for (int i = 0; i < demoList.GetLength(); i++)
{
Console.WriteLine("The {0} item is:\t{1}", i, demoList.GetElem(i));
}
/*删除*/
Console.WriteLine("Delelte the item:");
demoList.Delete(3);
for (int i = 0; i < demoList.GetLength(); i++)
{
Console.WriteLine("The {0} item is:\t{1}", i, demoList.GetElem(i));
}
/*根据索引查找*/
Console.WriteLine("The 2st item is:\t{0}", demoList.GetElem(1));
/*根据值查找*/
Console.WriteLine("The position of the item 'Wang Hongjian' is:\t{0}", demoList.Locate("Wang Hongjian"));
/*清空*/
demoList.Clear();
Console.WriteLine("Now the list is empty");
}
}
class Node<T>
{
private T data;
private Node<T> next;
public Node() {
data = default(T);
next = null;
}
public Node(T val)
{
data = val;
next = null;
}
internal Node<T> Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
public class SinglyLinkedList<T> : IListDS<T>
{
Node<T> head;
/// <summary>
/// 插入指定位置
/// </summary>
public bool Insert(T item, int pos)
{
if (IsEmpty() || pos < 0)
{
Console.WriteLine("the list is empty");
return false;
}
Node<T> newNode = new Node<T>(item);
if (pos == 0) {///插入首位置
newNode.Next = head;
head = newNode;
return true;
}
Node<T> curr = head.Next;
Node<T> pre = head;
int index = 1;
while (curr.Next != null && index < pos)
{
pre = curr;
curr = curr.Next;
index++;
}
if (index == pos)
{
newNode.Next = curr;
pre.Next = newNode;
return true;
}
else {
Console.WriteLine("the position is error");
return false;
}
}
/// <summary>
/// 添加
/// </summary>
public void Append(T item)
{
Node<T> newNode = new Node<T>(item);
Node<T> curr = null;
if (head == null)
{
head = newNode;
return;
}
curr = head;
while (curr.Next != null)
{
curr = curr.Next;
}
curr.Next = newNode;
}
/// <summary>
/// 删除
/// </summary>
public T Delete(int pos)
{
if (IsEmpty() || pos < 0) {
Console.WriteLine("the list is empty");
return default(T);
}
T data = default(T);
if (pos == 0) {
data = head.Data;
head = head.Next;
return data;
}
Node<T> pre = this.head;
Node<T> curr = this.head.Next;
int index = 1;
while (curr.Next != null && index < pos)
{
index++;
pre = curr;
curr = curr.Next;
}
if (index == pos)
{
pre.Next = curr.Next;
return curr.Data;
}
else {
Console.WriteLine("the position is error");
return default(T);
}
}
/// <summary>
/// 根据位置,查值
/// </summary>
public T GetElem(int pos)
{
if (IsEmpty()||pos<0)
{
Console.WriteLine("The list is empty");
return default(T);
}
Node<T> curr = head;
int index = 0;
while (curr.Next != null && index < pos) {
index++;
curr = curr.Next;
}
if (index == pos)
return curr.Data;
else {
Console.WriteLine("the position is out of range");
return default(T);
}
}
/// <summary>
/// 根据值,查找位置
/// </summary>
public int Locate(T value)
{
if (IsEmpty())
{
Console.WriteLine("The list is empty");
return -1;
}
Node<T> curr = new Node<T>();
curr = head;
int pos = -1;
while (curr != null && curr.Data.Equals(value))
{
curr = curr.Next;
pos++;
}
if (curr == null) return -1;
return pos;
}
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
head = null;
}
/// <summary>
/// 是否为空
/// </summary>
public bool IsEmpty()
{
return head == null;
}
/// <summary>
/// 获取长度
/// </summary>
public int GetLength()
{
if (IsEmpty()) return 0;
int len = 0;
Node<T> curr = head;
while (curr != null)
{
len++;
curr = curr.Next;
}
return len;
}
}
/// <summary>
/// 顺序表实现接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IListDS<T>
{
bool Insert(T item,int pos);//插入元素
bool IsEmpty();//是否为空
int GetLength();//得到容量
void Append(T item);//添加新元素
void Clear();//清空
T Delete(int pos);//删除元素
T GetElem(int pos);//根据索引查找
int Locate(T value);//根据值查找
}
浙公网安备 33010602011771号