标签:
单链表允许从一个结点直接访问它的后继结点,所以, 找直接后继结点的时间复杂度是 O(1)。但是,要找某个结点的直接前驱结点,只能从表的头引用开始遍历各结点。如果某个结点的 Next 等于该结点,那么,这个结点就是该结点的直接前驱结点。也就是说,找直接前驱结点的时间复杂度是 O(n),n是单链表的长度。当然,我们也可以在结点的引用域中保存直接前驱结点的地址而不是直接后继结点的地址。这样,找直接前驱结点的时间复杂度只有 O(1),但找直接后继结点的时间复杂度是 O(n)。如果希望找直接前驱结点和直接后继结点的时间复杂度都是 O(1),那么,需要在结点中设两个引用域,一个保存直接前驱结点的地址,叫 prev,一个直接后继结点的地址,叫 next,这样的链表就是双向链表(Doubly Linked List)。
双链表节点类的实现如下所示:
public class DbNode<T>//双向链表结点类 { private T data; private DbNode<T> prev; private DbNode<T> next; public DbNode(DbNode<T> p, T val) { next = p; data = val; } public DbNode(DbNode<T> p) { next = p; } public DbNode(T val) { next = null; data = val; } public DbNode() { next = null; data = default(T); } public T Data { get { return data; } set { data = value; } } public DbNode<T> Prev { get { return prev; } set { prev = value; } } public DbNode<T> Next { get { return next; } set { next = value; } } }
双链表接口的实现如下所示:
public interface ILinkList<T>//双向链表接口 { void AddFirst(T t); void AddLast(T t); void Clear(); int Count { get; } DbNode<T> Head { get; set; } DbNode<T> Tail { get; set; } void Insert(int index, T t); bool IsEmpty { get; } void RemoveAt(int index); void RemoveFirst(); void RemoveLast(); DbNode<T> this[int index] { get; } }
双链表的实现如下所示:
public class LinkList<T> : ILinkList<T> { private DbNode<T> head; private DbNode<T> tail; private int size; public DbNode<T> Head { get { return head; } set { head = value; } } public DbNode<T> Tail { get { return tail; } set { tail = value; } } public DbNode<T> this[int index] { get { if (head == null || index < 0 || index >= Count) { return null; } int i = 0; DbNode<T> current = new DbNode<T>(); if (i < size / 2) { current = head; while (true) { if (i == index) { break; } current = current.Next; i++; } return current; } else { current = tail; i = size; while (true) { if (i == index) { break; } current = current.Prev; i--; } return current.Next; } } } public bool IsEmpty { get { return head == null; } } public void AddFirst(T t) { DbNode<T> node = new DbNode<T>(); if (head == null) { head = node; tail = node; ++size; return; } node.Next = head; head.Prev = node; head = node; ++size; } public void AddLast(T t) { DbNode<T> node = new DbNode<T>(); if (head == null) { head = node; tail = node; ++size; return; } tail.Next = node; node.Prev = tail; tail = node; ++size; } public void Clear() { head = null; tail = null; size = 0; } public int Count { get { return size; } } public void Insert(int index, T t) { if (index < 0 || index >= Count) { return; } if (IsEmpty && index > 0) { return; } if (IsEmpty) { AddFirst(t); return; } DbNode<T> current = head; DbNode<T> node = new DbNode<T>(t); int i = 0; while (true) { if (i == index) break; current = current.Next; i++; } current.Prev.Next = node;//此处很重要 node.Prev = current.Prev; node.Next = current; current.Prev = node; size++; } public void RemoveAt(int index) { if (index < 0 || index >= Count) { return; } if (IsEmpty && index > 0) { return; } if (IsEmpty) { RemoveFirst(); return; } if (index == size - 1) { RemoveLast(); return; } DbNode<T> current = head; int i = 0; while (true) { if (i == index) break; current = current.Next; i++; } current.Prev.Next = current.Next; current.Next.Prev = current.Prev; size--; } public void RemoveFirst() { if (IsEmpty) { return; } if (size == 1) { Clear(); } head = head.Next; head.Prev = null; size--; } public void RemoveLast() { if (IsEmpty) { return; } if (size == 1) { Clear(); } tail = tail.Prev; tail.Next = null; size--; } }
标签:
原文地址:http://www.cnblogs.com/juji-baleite/p/5579635.html