public object[] ToArray() { object[] array = new object[Count]; MyLinkedListNode node = _head; int i = 0; while (node != null) { array[i] = node.Value; i++; node = node.Next; } return(array); }
public void AddLast(object value) { MyLinkedListNode node = new MyLinkedListNode(value); if (_head == null) { _head = node; _tail = node; } else { _tail.Next = node; _tail = node; } Count++; }
public bool Remove(object value) { MyLinkedListNode node = _head; while (node != null) { if (node.Value.Equals(value)) { RemoveNode(node); return(true); } node = node.Next; } return(false); }
public bool RemoveByIndex(int index) { if (index >= Count || index < 0) { return(false); } MyLinkedListNode node = _head; for (int i = 0; i < index; i++) { node = node.Next; } RemoveNode(node); return(true); }
public int IndexOf(object value) { MyLinkedListNode current = _head; int i = 0; while (current != null) { if (current.Value.Equals(value)) { return(i); } current = current.Next; i++; } return(-1); }
public MyLinkedList CloneList() { MyLinkedList newList = new MyLinkedList(); if (_head == null) { return(newList); } MyLinkedListNode head = _head; while (head != null) { newList.AddLast(head.Value); head = head.Next; } return(newList); }
private protected override void RemoveNode(MyLinkedListNode node) { node = node as MyDoublyLinkedListNode; MyDoublyLinkedListNode previous = null; MyDoublyLinkedListNode current = _head as MyDoublyLinkedListNode; while (current != null) { if (current.Equals(node)) { if (previous != null) { previous.Next = current.Next; if (current.Next == null) { _tail = previous; } else { MyDoublyLinkedListNode temp = current.Next as MyDoublyLinkedListNode; temp.Previous = previous; } Count--; } else { RemoveFirst(); } } previous = current; current = current.Next as MyDoublyLinkedListNode; } }
internal override void Invalidate() { base.Invalidate(); Previous = null; }