public void DeleteAt(T key) { if (this.Head == null) { return; } //handle head deletions. else if (Head.Data.CompareTo(key) == 0) { Head = Head.Next; } else { //handle deletions from middle. NodeSLL <T> tmp = Head; while (tmp.Next != null && tmp.Next.Data.CompareTo(key) != 0) { tmp = tmp.Next; } //handle miss if (tmp.Next == null) { return; } tmp.Next = tmp.Next.Next; } }
public void AddBefore(T key, T input) { if (this.Head == null) { return; } //handle insert before head if (Head.Data.CompareTo(key) == 0) { this.AddFirst(input); } else // handle insertions after head { NodeSLL <T> tmp = Head; while (tmp.Next != null && tmp.Next.Data.CompareTo(key) != 0) { tmp = tmp.Next; } //handle miss if (tmp.Next == null && tmp.Data.CompareTo(key) != 0) { return; } //do insert tmp.Next = new NodeSLL <T>(input, tmp.Next); } }
public void AddAfter(T key, T input) { NodeSLL <T> tmp = Head; while (tmp.Data.CompareTo(key) != 0 && tmp.Next != null) { tmp = tmp.Next; } if (tmp != null && tmp.Data.CompareTo(key) == 0) { tmp.Next = new NodeSLL <T>(input, tmp.Next); } }
public T GetLast() { try { NodeSLL <T> tmp = Head; while (tmp.Next != null) { tmp = tmp.Next; } return(tmp.Data); } catch { throw new ArgumentNullException(); } }
public void Reverse() { NodeSLL <T> curr = Head; NodeSLL <T> prev = null; NodeSLL <T> currPtr = null; while (curr != null) { currPtr = curr.Next; curr.Next = prev; prev = curr; curr = currPtr; } this.Head = prev; }
public int Count() { if (this.Head == null) { return(0); } int ctr = 1; NodeSLL <T> tmp = this.Head; while (tmp.Next != null) { tmp = tmp.Next; ctr++; } return(ctr); }
public void AddFirst(T data) { Head = new NodeSLL <T>(data, this.Head); }
public NodeSLL(T data, NodeSLL <T> next) { this.Data = data; this.Next = next; }
public MyLinkedList() { Head = null; }