数据结构和算法基础之循环链表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { /// <summary> /// 循环链表 /// </summary> /// <typeparam name="T"></typeparam> public class CircleLinkList<T> { public Node<T> Head; public CircleLinkList() { Head = null; } /// <summary> /// 增加 /// </summary> /// <param name="data"></param> public void Add(T data) { Node<T> newNode = new Node<T>(data); Node<T> tmpNode = new Node<T>(); if(Head==null) { Head = new Node<T>(); } tmpNode = Head; while(tmpNode.Next!=null&& tmpNode.Next!= Head) { tmpNode = tmpNode.Next; } tmpNode.Next = newNode; newNode.Next = Head; } /// <summary> /// 在i位置前面插入节点 /// </summary> /// <param name="data"></param> /// <param name="i"></param> public void Insert(T data, int i) { Node<T> newNode = new Node<T>(data); Node<T> tmpNode = new Node<T>(); tmpNode = Head; int j = 0; while (j < i - 1 && tmpNode.Next != null) { tmpNode = tmpNode.Next; j++; } newNode.Next = tmpNode.Next; tmpNode.Next = newNode; } /// <summary> /// 删除节点 /// </summary> /// <param name="i"></param> public void Delete(int i) { int j = i; Node<T> tmpNode = new Node<T>(); tmpNode = Head; while (j > 0 && tmpNode.Next != null) { tmpNode = tmpNode.Next; j--; } Node<T> curNode = tmpNode.Next; tmpNode.Next = curNode.Next; } /// <summary> /// 输出整个链表 /// </summary> public void PrintLinkList() { Node<T> tmpNode = Head; while (tmpNode.Next != Head) { tmpNode = tmpNode.Next; Console.WriteLine(tmpNode.Data); } } } }