public void Add(T element, int?position = null) { position = position ?? this.Length; if ((position < 0) || (position > this.Length)) { throw new ArgumentOutOfRangeException(); } if (position == 0) { this.first = new DoublyLinkedElement <T>(element, next: first); this.Length++; return; } DoublyLinkedElement <T> pointer = this.first; for (int i = 1; i < position; i++) { pointer = pointer.Next; } if (pointer.Next == null) { pointer.Next = new DoublyLinkedElement <T>(element, previous: pointer); } else { DoublyLinkedElement <T> inserted = new DoublyLinkedElement <T>(element, previous: pointer, next: pointer.Next); pointer.Next.Previous = inserted; pointer.Next = inserted; } this.Length++; }
public void Remove(int num) { if ((num < 0) || (num >= this.Length)) { return; } if (num == 0) { if (this.first.Next != null) { this.first.Next.Previous = null; } this.first = first.Next; this.Length--; return; } DoublyLinkedElement <T> pointer = this.first; for (int i = 0; i < num; i++) { pointer = pointer.Next; } if (pointer.Next != null) { pointer.Next.Previous = pointer.Previous; } pointer.Previous.Next = pointer.Next; this.Length--; }
public T[] ToArray() { T[] result = new T[this.Length]; DoublyLinkedElement <T> pointer = this.first; for (int i = 0; i < this.Length; i++) { result[i] = pointer.content; pointer = pointer.Next; } return(result); }
public override string ToString() { string s = ""; DoublyLinkedElement <T> pointer = this.first; for (int i = 0; i < this.Length - 1; i++) { s += String.Format("{0}<->", pointer.content.ToString()); pointer = pointer.Next; } s += String.Format("{0}", pointer.content.ToString()); return(s); }
public int IndexOf(T element) { DoublyLinkedElement <T> pointer = this.first; int num = 0; while (pointer != null) { if (pointer.content.Equals(element)) { return(num); } pointer = pointer.Next; num++; } return(-1); }
public DoublyLinkedElement(T el, DoublyLinkedElement <T> previous = null, DoublyLinkedElement <T> next = null) { content = el; this.Previous = previous; this.Next = next; }