public void Append(int value) { var node = new DoublyNode() { Value = value, Next = null, Previous = tail }; tail.Next = node; tail = node; length++; }
public void InsertLast(T t) { DoublyNode <T> newNode = new DoublyNode <T>() { Data = t }; if (IsEmpty()) { _first = newNode; } else { _last.Next = newNode; newNode.Previous = _last; } _last = newNode; }
public void DeleteLast() { if (!IsEmpty()) { if (_first.Next == null) { _first = null; } else { _last.Previous.Next = null; } _last = _last.Previous; } else { throw new Exception("The list is empty!"); } }
public void DeleteFirst() { if (!IsEmpty()) // { if (_first.Next == null) // if there is only one node in the list { _last = null; } else { _first.Next.Previous = null; // the list's first node will point to null } _first = _first.Next; } else { throw new Exception("The list is empty!"); } }
public void InsertFirst(T t) { DoublyNode <T> newNode = new DoublyNode <T>() { Data = t }; if (IsEmpty()) { _last = newNode; } else { _first.Previous = newNode; } newNode.Next = _first; // newNode--> old first _first = newNode; // first place }
private void ActualInsert(int index, int value, DoublyNode currentNode) { for (int i = 0; i <= index; i++) { if (i == index - 1) { var node = new DoublyNode() { Value = value, Next = currentNode.Next, Previous = currentNode }; currentNode.Next = node; } else { currentNode = currentNode.Next; } } length++; }
public DoublyLinkedListUser(List <int> values) { int i = 0; foreach (var value in values) { if (i == 0) { head = new DoublyNode() { Value = value, Next = null, Previous = null }; tail = head; length = 1; i++; } else { Append(value); } } }
public void Remove(int index) { if (index > length - 1) { Console.Write("Invalid Index"); } var currentNode = this.head; DoublyNode node = new DoublyNode(); for (int i = 0; i <= index + 1; i++) { if (i == index - 1) { node = currentNode; } else if (i == index + 1) { node.Next = currentNode; node.Previous = node; } currentNode = currentNode.Next; } length--; }
public DoublyLinkedList() { _first = null; _last = null; }