using System;
using System.Collections.Generic;
using System.Text;
namespace SingleLinkedList
{
class Program
{
static void Main(string[] args)
{
//執行個體調用
}
}
//定義單鏈表的結點
//結點儲存資料和下一個結點的地址(引用).這裡用一個類表示.
public class Node<T>
{
private T data; //資料
private Node<T> next; //引用
//構造器
public Node(T val, Node<T> p)
{
data = val;
next = p;
}
//構造器2
public Node(Node<T> p)
{
next = p;
}
//構造器3
public Node(T val)
{
data = val;
next = null;
}
//構造器4
public Node()
{
data = default(T);
next = null;
}
//資料域屬性
public T Data
{
get
{
return data;
}
set
{
data = value;
}
}
//參考網域屬性
public Node<T> Next
{
get { return next; }
set { next = value; }
}
}
//單鏈表類--定義操作結點的一些方法(如刪除, 插入等).
//IList<T>是.net內建的一個介面. 這裡實現了這個介面.
public class LinkList<T> : IList<T>
{
private Node<T> head; //單鏈表的頭引用
//頭引用 屬性
public Node<T> Head
{
get { return head; }
set { head = value; }
}
//構造器
public LinkList()
{
head = null;
}
///<summary>
///求單鏈表的長度
///需要從表頭開始, 一個結點一個結點遍曆,直到表的末尾.
///</summary>
public int GetLength()
{
Node<T> p = head;
int len = 0;
while (p != null)
{
++len;
p = p.Next;
}
return len;
}
///<summary>
/// 清空單鏈表
/// head=null即可.
/// 單鏈表清空後,原來結點所佔用的空間不會一直保留, 而由記憶體回收行程進行回收.
///</summary>
public void Clear()
{
head = null;
}
///<summary>
/// 判斷單鏈表是否為空白
/// head==null,即為空白
///</summary>
public bool IsEmpty()
{
if (head == null)
{
return true;
}
else
{
return false;
}
}
///<summary>
///附加操作
///在單鏈表的末尾添加新元素
/// </summary>
public void Append(T item)
{
Node<T> q = new Node<T>(item);
Node<T> p = new Node<T>();
if (head == null)
{
head = q;
return;
}
p = head;
while (p.Next != null)
{
p = p.Next;
}
p.Next = q;
}
//在單鏈表的第 i 個結點位置前插入一個值為 item 的結點.
public void Insert(T item, int i)
{
if (IsEmpty() || i < 1)
{
Console.WriteLine("List is empty or Position is error!");
return;
}
//就一個head元素.(插入到head前即可)
if (i == 1)
{
Node<T> q = new Node<T>(item);
q.Next = head;
return;
}
//非head(中間某一元素前插入P)
Node<T> p = head;
Node<T> r = new Node<T>();
int j = 1;
while (p.Next != null && j < i)
{
r = p;
p = p.Next;
++j;
}
if (j == i)
{
Node<T> q = new Node<T>(item);
q.Next = p;
r.Next = q;
}
}
//在單鏈表的第 i 個結點的位置後插入一個值為 item的結點.
public void InsertPost(T item, int i)
{
if (IsEmpty() || i < 1)
{
Console.WriteLine("List is empty or Position is error!");
return;
}
if (i == 1)
{
Node<T> q = new Node<T>(item);
q.Next = head.Next;
head.Next = q;
return;
}
Node<T> p = head;
int j = 1;
while (p != null && j < i)
{
p = p.Next;
++j;
}
if (j == i)
{
Node<T> q = new Node<T>(item);
q.Next = p.Next;
p.Next = q;
}
}
//刪除單鏈表的第 i 個結點
public T Delete(int i)
{
if (IsEmpty() || i < 0)
{
Console.WriteLine("Link is empty or Position is error!");
return default(T);
}
Node<T> q = new Node<T>();
if (i == 1)
{
q = head;
head = head.Next;
return q.Data;
}
Node<T> p = head;
int j = 1;
//從頭一直找到 i 所在的位置
//條件是: (1).單鏈表沒有到末尾, (2).還沒有到 i 所在的位置 ( j< i).
while (p.Next != null && j < i)
{
++j;
q = p;
p = p.Next;
}
if (j == i)
{
q.Next = p.Next;
return p.Data;
}
else
{
Console.WriteLine("The item node is not exist!");
return default(T);
}
}
//獲得單鏈表的第 i 個資料元素
public T GetElem(int i)
{
if (IsEmpty())
{
Console.WriteLine("List is empty!");
return default(T);
}
Node<T> p = new Node<T>();
p = head;
int j = 1;
while (p.Next != null & j < i)
{
++j;
p = p.Next;
}
if (j == i)
{
return p.Data; //找到了.
}
else
{
Console.WriteLine("The item node is not exist!");
return default(T);
}
}
///<summary>
///在單鏈表中尋找值為 value 的結點
///</summary>
public int Locate(T value)
{
if (IsEmpty())
{
Console.WriteLine("List is Empty!");
return -1;
}
Node<T> p = new Node<T>();
p = head;
int i = 1;
while (!p.Data.Equals(value) && p.Next != null)
{
p = p.Next;
++i;
}
return i;
}
}
}