单链表
class Program
{
static void Main(string[] args)
{
LinkedList<int> linkedList = new LinkedList<int>();
Node<int> node1 = new Node<int> { Data = 1 };
Node<int> node2 = new Node<int> { Data = 2 };
Node<int> node3 = new Node<int> { Data = 3 };
Node<int> node4 = new Node<int> { Data = 4 };
linkedList.Append(node1);
linkedList.Append(node2);
linkedList.Append(node3);
linkedList.Append(node4);
linkedList.Insert(4, new Node<int> { Data = 0 });
linkedList.Delete(2);
linkedList.Display();
//int length = linkedList.GetLength();
//Console.WriteLine(length);
Console.WriteLine(linkedList.GetDataByIndex(0).Data);
Console.ReadKey();
}
}
class LinkedList
{
public Node Head { get; set; }
public LinkedList()
{
Head = null;
}
#region 添加
public void Append(Node node)
{
Node foot = node;
if (Head == null)
{
Head = foot;
return;
}
Node A = Head;
while (A.Next != null)
{
A = A.Next;
}
A.Next = foot;
}
#endregion
#region 插入
public void Insert(int index, Node node)
{
if (Head == null || index > GetLength())
return;
if (index == 0)
{
Node temp = Head;
Head = node;
Head.Next = temp;
return;
}
//Node previous = GetDataByIndex(index - 1);
//node.Next = previous.Next;
//previous.Next = node;
Node A = new Node();
Node B = new Node();
B = Head;
int j = 0;
while (j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
{
Node C = node;
A.Next = C;
C.Next = B;
}
}
#endregion
#region 删除
public void Delete(int index)
{
if (Head == null || index >= GetLength())
return;
if (index == 0)
{
Head = Head.Next;
return;
}
//Node previous = GetDataByIndex(index - 1);
//previous.Next = previous.Next.Next;
Node A = new Node();
Node B = Head;
int j = 0;
while (B.Next != null && j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
A.Next = B.Next;
}
#endregion
#region 索引获取节点
public Node GetDataByIndex(int index)
{
if (index >= GetLength() || index < 0)
{
throw (new Exception("length out of range"));
}
if (index == 0)
return Head;
int count = 0;
Node temp = Head;
while (temp.Next != null && count < index)
{
temp = temp.Next;
count++;
}
return temp;
}
#endregion
#region 长度
public int GetLength()
{
if (Head == null)
return 0;
int length = 1;
Node A = Head;
while (A.Next != null)
{
A = A.Next;
length++;
}
return length;
}
#endregion
#region 打印
public void Display()
{
if (Head == null)
return;
Node temp = Head;
while (temp != null)
{
Console.WriteLine(temp.Data);
temp = temp.Next;
}
}
#endregion
}
class Node
{
public T Data { get; set; }
public Node Next { get; set; }
public Node(T item)
{
this.Data = item;
this.Next = null;
}
public Node()
{
this.Data = default(T);
this.Next = null;
}
}
双向链表
class Program
{
static void Main(string[] args)
{
Node<int> node1 = new Node<int> { Data = 1 };
Node<int> node2 = new Node<int> { Data = 2 };
Node<int> node3 = new Node<int> { Data = 3 };
Node<int> node4 = new Node<int> { Data = 4 };
Node<int> node5 = new Node<int> { Data = 5 };
DoublyLinkedList<int> doublyLinkedList = new DoublyLinkedList<int>();
doublyLinkedList.Append(node1);
doublyLinkedList.Append(node2);
doublyLinkedList.Append(node3);
doublyLinkedList.Append(node4);
doublyLinkedList.Append(node5);
doublyLinkedList.Insert(2, new Node<int> { Data = 9 });
doublyLinkedList.Display();
doublyLinkedList.Delete(2);
Console.WriteLine("====================");
doublyLinkedList.Display();
//Console.WriteLine(doublyLinkedList.GetNodeByIndex(5).Data);
//int length = doublyLinkedList.GetLength();
//Console.WriteLine(length);
Console.ReadKey();
}
}
class DoublyLinkedList
{
public Node Head { get; set; }
#region 打印
public void Display()
{
if (Head == null)
return;
Node temp = Head;
while (temp != null)
{
Console.WriteLine($"Prev: {temp.Prev?.Data.ToString() ?? string.Empty}" + ", "
+ $"Data: {temp.Data}" + ", "
+ $"Next: {temp.Next?.Data.ToString() ?? string.Empty}");
temp = temp.Next;
}
}
#endregion
#region 长度
public int GetLength()
{
if (Head == null)
return 0;
int count = 1;
Node temp = Head;
while (temp.Next != null)
{
temp = temp.Next;
count++;
}
return count;
}
#endregion
#region 索引获取节点
public Node GetNodeByIndex(int index)
{
if (index < 0 || index >= GetLength())
throw new Exception("index out of range");
if (Head == null)
return null;
int count = 0;
Node temp = Head;
while (temp.Next != null && count < index)
{
temp = temp.Next;
count++;
}
return temp;
}
#endregion
#region 添加
public void Append(Node node)
{
Node foot = node;
if (Head == null)
{
Head = foot;
return;
}
Node A = Head;
while (A.Next != null)
{
A = A.Next;
}
A.Next = foot;
foot.Prev = A;
}
#endregion
#region 插入
public void Insert(int index, Node node)
{
if (Head == null && index == 0)
{
Head = node;
return;
}
Node A = new Node();
Node B = Head;
int j = 0;
while (B.Next != null && j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
{
A.Next = node;
B.Prev = node;
node.Prev = A;
node.Next = B;
}
}
#endregion
#region 删除
public void Delete(int index)
{
if (Head == null)
return;
if (index < 0 || index >= GetLength())
throw new Exception("index out of range");
if (index == 0)
{
Head = Head.Next;
Head.Prev = null;
return;
}
Node A = new Node();
Node B = Head;
int j = 0;
while (B.Next != null && j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
{
A.Next = B.Next;
B.Next.Prev = A;
}
}
#endregion
}
class Node
{
public T Data { get; set; }
public Node Prev { get; set; }
public Node Next { get; set; }
}
双向循环链表
class Program
{
static void Main(string[] args)
{
Node<int> node1 = new Node<int> { Data = 1 };
Node<int> node2 = new Node<int> { Data = 2 };
Node<int> node3 = new Node<int> { Data = 3 };
Node<int> node4 = new Node<int> { Data = 4 };
Node<int> node5 = new Node<int> { Data = 5 };
DoublyCircularLinkedList<int> doublyLinkedList = new DoublyCircularLinkedList<int>();
doublyLinkedList.Append(node1);
doublyLinkedList.Append(node2);
doublyLinkedList.Append(node3);
doublyLinkedList.Append(node4);
doublyLinkedList.Append(node5);
//doublyLinkedList.Insert(5, new Node { Data = 9 });
doublyLinkedList.Display();
doublyLinkedList.Delete(4);
Console.WriteLine("====================");
doublyLinkedList.Display();
//Console.WriteLine(doublyLinkedList.GetNodeByIndex(0).Data);
//int length = doublyLinkedList.GetLength();
//Console.WriteLine(length);
Console.ReadKey();
}
}
class DoublyCircularLinkedList
{
public Node Head { get; set; }
#region 打印
public void Display()
{
if (Head == null)
return;
Node temp = Head;
while (temp != null)
{
Console.WriteLine($"Prev: {temp.Prev?.Data.ToString() ?? string.Empty}" + ", "
+ $"Data: {temp.Data}" + ", "
+ $"Next: {temp.Next?.Data.ToString() ?? string.Empty}");
temp = temp.Next;
if (temp == Head)
return;
}
}
#endregion
#region 长度
public int GetLength()
{
if (Head == null)
return 0;
int count = 1;
Node temp = Head;
while (temp.Next != null && temp.Next != Head)
{
temp = temp.Next;
count++;
}
return count;
}
#endregion
#region 索引获取节点
public Node GetNodeByIndex(int index)
{
if (index < 0 || index >= GetLength())
throw new Exception("index out of range");
if (Head == null)
return null;
int count = 0;
Node temp = Head;
while (temp.Next != null && count < index)
{
temp = temp.Next;
count++;
}
return temp;
}
#endregion
#region 添加
public void Append(Node node)
{
Node foot = node;
if (Head == null)
{
Head = foot;
return;
}
Node A = Head;
while (A.Next != null && A.Next != Head)
{
A = A.Next;
}
A.Next = foot;
foot.Prev = A;
foot.Next = Head;
Head.Prev = foot;
}
#endregion
#region 插入
public void Insert(int index, Node node)
{
if (index < 0 || index >= GetLength())
if (index == GetLength())
{
Append(node);
return;
}
else
{
throw new Exception("index out of range");
}
if (Head == null)
{
if (index == 0)
{
Head = node;
return;
}
else
{
throw new Exception("index out of range");
}
}
if (index == 0)
{
Node temp = Head;
int i = GetLength();
Node foot = GetNodeByIndex(i - 1);
Head = node;
node.Next = temp;
temp.Prev = node;
node.Prev = foot;
foot.Next = Head;
return;
}
Node A = new Node();
Node B = Head;
int j = 0;
while (B.Next != null && j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
{
A.Next = node;
B.Prev = node;
node.Prev = A;
node.Next = B;
}
}
#endregion
#region 删除
public void Delete(int index)
{
if (Head == null)
return;
if (index < 0 || index >= GetLength())
throw new Exception("index out of range");
if (index == 0)
{
Node foot = Head.Prev;
Head = Head.Next;
Head.Prev = foot;
foot.Next = Head;
return;
}
Node A = new Node();
Node B = Head;
int j = 0;
while (B.Next != null && j < index)
{
A = B;
B = B.Next;
j++;
}
if (j == index)
{
A.Next = B.Next;
B.Next.Prev = A;
}
}
#endregion
}
class Node
{
public T Data { get; set; }
public Node Prev { get; set; }
public Node Next { get; set; }
}