链表
单链表
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<T> { public Node<T> Head { get; set; } public LinkedList() { Head = null; } #region 添加 public void Append(Node<T> node) { Node<T> foot = node; if (Head == null) { Head = foot; return; } Node<T> A = Head; while (A.Next != null) { A = A.Next; } A.Next = foot; } #endregion #region 插入 public void Insert(int index, Node<T> node) { if (Head == null || index > GetLength()) return; if (index == 0) { Node<T> temp = Head; Head = node; Head.Next = temp; return; } //Node<T> previous = GetDataByIndex(index - 1); //node.Next = previous.Next; //previous.Next = node; Node<T> A = new Node<T>(); Node<T> B = new Node<T>(); B = Head; int j = 0; while (j < index) { A = B; B = B.Next; j++; } if (j == index) { Node<T> 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<T> previous = GetDataByIndex(index - 1); //previous.Next = previous.Next.Next; Node<T> A = new Node<T>(); Node<T> 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<T> GetDataByIndex(int index) { if (index >= GetLength() || index < 0) { throw (new Exception("length out of range")); } if (index == 0) return Head; int count = 0; Node<T> 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<T> A = Head; while (A.Next != null) { A = A.Next; length++; } return length; } #endregion #region 打印 public void Display() { if (Head == null) return; Node<T> temp = Head; while (temp != null) { Console.WriteLine(temp.Data); temp = temp.Next; } } #endregion } class Node<T> { public T Data { get; set; } public Node<T> 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<T> { public Node<T> Head { get; set; } #region 打印 public void Display() { if (Head == null) return; Node<T> 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<T> temp = Head; while (temp.Next != null) { temp = temp.Next; count++; } return count; } #endregion #region 索引获取节点 public Node<T> GetNodeByIndex(int index) { if (index < 0 || index >= GetLength()) throw new Exception("index out of range"); if (Head == null) return null; int count = 0; Node<T> temp = Head; while (temp.Next != null && count < index) { temp = temp.Next; count++; } return temp; } #endregion #region 添加 public void Append(Node<T> node) { Node<T> foot = node; if (Head == null) { Head = foot; return; } Node<T> A = Head; while (A.Next != null) { A = A.Next; } A.Next = foot; foot.Prev = A; } #endregion #region 插入 public void Insert(int index, Node<T> node) { if (Head == null && index == 0) { Head = node; return; } Node<T> A = new Node<T>(); Node<T> 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<T> A = new Node<T>(); Node<T> 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<T> { public T Data { get; set; } public Node<T> Prev { get; set; } public Node<T> 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<int> { 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<T> { public Node<T> Head { get; set; } #region 打印 public void Display() { if (Head == null) return; Node<T> 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<T> temp = Head; while (temp.Next != null && temp.Next != Head) { temp = temp.Next; count++; } return count; } #endregion #region 索引获取节点 public Node<T> GetNodeByIndex(int index) { if (index < 0 || index >= GetLength()) throw new Exception("index out of range"); if (Head == null) return null; int count = 0; Node<T> temp = Head; while (temp.Next != null && count < index) { temp = temp.Next; count++; } return temp; } #endregion #region 添加 public void Append(Node<T> node) { Node<T> foot = node; if (Head == null) { Head = foot; return; } Node<T> 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<T> 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<T> temp = Head; int i = GetLength(); Node<T> foot = GetNodeByIndex(i - 1); Head = node; node.Next = temp; temp.Prev = node; node.Prev = foot; foot.Next = Head; return; } Node<T> A = new Node<T>(); Node<T> 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<T> foot = Head.Prev; Head = Head.Next; Head.Prev = foot; foot.Next = Head; return; } Node<T> A = new Node<T>(); Node<T> 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<T> { public T Data { get; set; } public Node<T> Prev { get; set; } public Node<T> Next { get; set; } }
把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。