public DoublyLinkedListNode <T> Find(T value) { DoublyLinkedListNode <T> current = Head; while (current != null) { // Head -> 3 -> 5 -> 7 // Value: 5 if (current.Value.Equals(value)) { return(current); } current = current.Next; } return(null); }
public void insertingNodeAtTheEnd(T newData) { var newNode = new DoublyLinkedListNode <T>(newData); if (head == null) { head = newNode; return; } var temp = head; while (temp.Next != null) { temp = temp.Next; } newNode.Previous = temp; temp.Next = newNode; }
/// <summary> /// Removes the first occurance of the item from the list (searching /// from Head to Tail). /// </summary> /// <param name="item">The item to remove</param> /// <returns>True if the item was found and removed, false otherwise</returns> public bool Remove(T item) { DoublyLinkedListNode <T> found = Find(item); if (found == null) { return(false); } DoublyLinkedListNode <T> previous = found.Previous; DoublyLinkedListNode <T> next = found.Next; if (previous == null) { // we're removing the head node Head = next; if (Head != null) { Head.Previous = null; } } else { previous.Next = next; } if (next == null) { // we're removing the tail Tail = previous; if (Tail != null) { Tail.Next = null; } } else { next.Previous = previous; } Count--; return(true); }
/// <summary> /// Time complexity: O(1). /// </summary> internal void Union(DoublyLinkedList <T> newList) { if (Head == null) { Head = newList.Head; Tail = newList.Tail; return; } if (newList.Head == null) { return; } Head.Previous = newList.Tail; newList.Tail.Next = Head; Head = newList.Head; }
/// <summary> /// Add the node to the end of the list /// </summary> /// <param name="value">The node to add</param> public void AddTail(DoublyLinkedListNode <T> node) { if (Count == 0) { Head = node; } else { Tail.Next = node; // Before: Head -> 3 <-> 5 -> null // After: Head -> 3 <-> 5 <-> 7 -> null // 7.Previous = 5 node.Previous = Tail; } Tail = node; Count++; }
private static void DoubleLinkedListRemoveLast() { Console.WriteLine("Method Name : " + "DoubleLinkedListRemoveLast"); CustomDoublyLinkedList <int> CustomDoublyLinkedList = new CustomDoublyLinkedList <int>(); DoublyLinkedListNode <int> node1 = new DoublyLinkedListNode <int>(1); CustomDoublyLinkedList.AddToLast(node1); DoublyLinkedListNode <int> node2 = new DoublyLinkedListNode <int>(2); CustomDoublyLinkedList.AddToLast(node2); DoublyLinkedListNode <int> node3 = new DoublyLinkedListNode <int>(3); CustomDoublyLinkedList.AddToLast(node3); DoublyLinkedListNode <int> node4 = new DoublyLinkedListNode <int>(4); CustomDoublyLinkedList.AddToLast(node4); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); Console.WriteLine("Removing first item from the list"); CustomDoublyLinkedList.RemoveLast(); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); CustomDoublyLinkedList.RemoveLast(); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); CustomDoublyLinkedList.RemoveLast(); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); CustomDoublyLinkedList.RemoveLast(); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); CustomDoublyLinkedList.RemoveLast(); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); }
private static void DoubleLinkedListContains() { Console.WriteLine("Method Name : " + "DoubleLinkedListContains"); CustomDoublyLinkedList <int> CustomDoublyLinkedList = new CustomDoublyLinkedList <int>(); DoublyLinkedListNode <int> node1 = new DoublyLinkedListNode <int>(1); CustomDoublyLinkedList.AddToLast(node1); DoublyLinkedListNode <int> node2 = new DoublyLinkedListNode <int>(2); CustomDoublyLinkedList.AddToLast(node2); DoublyLinkedListNode <int> node3 = new DoublyLinkedListNode <int>(3); CustomDoublyLinkedList.AddToLast(node3); DoublyLinkedListNode <int> node4 = new DoublyLinkedListNode <int>(4); CustomDoublyLinkedList.AddToLast(node4); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("Does 1 exist in the list : " + CustomDoublyLinkedList.Contains(1)); Console.WriteLine("Does 10 exist in the list : " + CustomDoublyLinkedList.Contains(10)); }
/// <summary> /// Time complexity: O(1). /// </summary> /// <returns>The new node.</returns> public DoublyLinkedListNode <T> InsertFirst(T data) { var newNode = new DoublyLinkedListNode <T>(data); if (Head != null) { Head.Previous = newNode; } newNode.Next = Head; newNode.Previous = null; Head = newNode; if (Tail == null) { Tail = Head; } return(newNode); }
public bool MoveNext() { if (headNode == null) { return(false); } if (currentNode == null) { currentNode = headNode; return(true); } if (currentNode.Next != null) { currentNode = currentNode.Next; return(true); } return(false); }
/// <summary> /// Returns item if the list contains the specified item /// </summary> /// <returns>Returns item if found</returns> public T Search(T item) { if (!Check(item)) { throw new InvalidOperationException("Item not found in list."); } DoublyLinkedListNode <T> current = head; while (current != null) { if (current.Value.Equals(item)) { return(current.Value); } current = current.Next; } return(item); }
/// <summary> /// Insert right after this node. /// Time complexity: O(1). /// </summary> public DoublyLinkedListNode <T> InsertAfter(DoublyLinkedListNode <T> node, DoublyLinkedListNode <T> data) { if (node == null) { throw new Exception("Empty reference node"); } if (node == Head && node == Tail) { node.Next = data; node.Previous = null; data.Previous = node; data.Next = null; Head = node; Tail = data; return(data); } if (node != Tail) { data.Previous = node; data.Next = node.Next; node.Next.Previous = data; node.Next = data; } else { data.Previous = node; data.Next = null; node.Next = data; Tail = data; } return(data); }
/// <summary> /// Insert right before this node. /// Time complexity:O(1). /// </summary> public DoublyLinkedListNode <T> InsertBefore(DoublyLinkedListNode <T> node, DoublyLinkedListNode <T> data) { if (node == null) { throw new Exception("Empty node"); } if (node == Head && node == Tail) { node.Previous = data; node.Next = null; Tail = node; data.Previous = null; data.Next = node; Head = data; return(data); } if (node == Head) { data.Previous = null; data.Next = node; node.Previous = data; Head = data; } else { data.Previous = node.Previous; data.Next = node; node.Previous.Next = data; node.Previous = data; } return(data); }
/// <summary> /// Delete tail node. /// Time complexity: O(1) /// </summary> /// public T DeleteLast() { if (Tail == null) { throw new Exception("Empty list"); } var tailData = Tail.Data; if (Tail == Head) { Head = null; Tail = null; } else { Tail.Previous.Next = null; Tail = Tail.Previous; } return(tailData); }
private static void DoubleLinkedListAddToLast() { Console.WriteLine("Method Name : " + "DoubleLinkedListAddToLast"); CustomDoublyLinkedList <int> CustomDoublyLinkedList = new CustomDoublyLinkedList <int>(); DoublyLinkedListNode <int> node1 = new DoublyLinkedListNode <int>(1); Console.WriteLine("Entering the first node and printing"); CustomDoublyLinkedList.AddToLast(node1); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("Entering multiple Nodes and printing"); DoublyLinkedListNode <int> node2 = new DoublyLinkedListNode <int>(2); CustomDoublyLinkedList.AddToLast(node2); DoublyLinkedListNode <int> node3 = new DoublyLinkedListNode <int>(3); CustomDoublyLinkedList.AddToLast(node3); DoublyLinkedListNode <int> node4 = new DoublyLinkedListNode <int>(4); CustomDoublyLinkedList.AddToLast(node4); CustomDoublyLinkedList.PrintLinkedList(); Console.WriteLine("No of item in he list is : " + CustomDoublyLinkedList.Count); }
/// <summary> /// Finds the index of the specified item /// </summary> /// <param name="item"></param> /// <returns>the index of item</returns> public int Index(T item) { if (!Check(item)) { throw new InvalidOperationException("Item not in list"); } DoublyLinkedListNode <T> current = head; var currentIndex = 0; while (currentIndex != count) { if (current.Value.Equals(item)) { return(currentIndex); } current = current.Next; currentIndex++; } return(-1); }
/// <summary> /// Time complexity: O(1). /// </summary> public T DeleteFirst() { if (Head == null) { throw new Exception("Empty list"); } var headData = Head.Data; if (Head == Tail) { Head = null; Tail = null; } else { Head.Next.Previous = null; Head = Head.Next; } return(headData); }
/// <summary> /// Removes the last node from the list /// </summary> public void RemoveTail() { if (Count != 0) { if (Count == 1) { Head = null; Tail = null; } else { // Before: Head --> 3 --> 5 --> 7 // Tail = 7 // After: Head --> 3 --> 5 --> null // Tail = 5 // Null out 5's Next pointer Tail.Previous.Next = null; Tail = Tail.Previous; } Count--; } }
public bool MoveNext() { if (currentNode?.Next != null) { currentNode = currentNode.Next; return(true); } while (currentNode?.Next == null) { position++; if (position < length) { if (hashList[position] == null) { continue; } currentNode = hashList[position].Head; if (currentNode == null) { continue; } return(true); } else { break; } } return(false); }
/// <summary> /// Removes the first node from the list. /// </summary> public void RemoveHead() { if (Count != 0) { // Before: Head -> 3 <-> 5 // After: Head -------> 5 // Head -> 3 -> null // Head ------> null Head = Head.Next; Count--; if (Count == 0) { Tail = null; } else { // 5.Previous was 3, now null Head.Previous = null; } } }
/// <summary> /// Inserts the specified item at the specified index /// </summary> /// <param name="index"></param> /// <param name="item"></param> public void Insert(int index, T item) { if (index > count + 1 || (count == 0 && index > 0) || index < 0) { throw new IndexOutOfRangeException("Index out of range."); } if (count == 0 || index == count) { Add(item); return; } var currentIndex = 0; DoublyLinkedListNode <T> current = head; DoublyLinkedListNode <T> node = new DoublyLinkedListNode <T>(item); while (currentIndex <= index) { if (currentIndex == index) { DoublyLinkedListNode <T> previous = current.Prev; if (previous == null) { head = node; } node.Prev = previous; node.Next = current; //previous.Next = node; current.Prev = node; count++; return; } current = current.Next; currentIndex++; } return; }
public DoublyLinkedListNode(T value) { Value = value; Next = null; Previous = null; }
internal DoublyLinkedListEnumerator(ref DoublyLinkedListNode <T> headNode) { this.headNode = headNode; }
public void Dispose() { headNode = null; currentNode = null; }
public void Reset() { currentNode = headNode; }
public void Reset() { position = -1; currentNode = null; }