private SingleLinkedListNode <Entry> Remove(SingleLinkedListNode <Entry> rootNode, string key) { SingleLinkedListNode <Entry> prevNode = null; var currentNode = rootNode; while (currentNode != null) { if (currentNode.Value.Key == key) { if (prevNode != null) { RemoveAfter(prevNode); return(prevNode); } else { return(currentNode.Next); } } prevNode = currentNode; currentNode = currentNode.Next; } return(rootNode); }
/*public static SingleLinkedListNode<T> Remove<T>(SingleLinkedListNode<T> rootNode, T value) where T : struct * { * SingleLinkedListNode<T> prevNode = null; * var currentNode = rootNode; * * while (currentNode != null) * { * if (currentNode.Value.Equals(value)) * { * if (prevNode != null) * { * RemoveAfter(prevNode); * return rootNode; * } * else * { * return currentNode; * } * } * * prevNode = currentNode; * currentNode = currentNode.Next; * } * * return rootNode; * }*/ public static void RemoveAfter <T>(SingleLinkedListNode <T> singleLinkedListNode) { if (singleLinkedListNode.Next != null) { singleLinkedListNode.Next = singleLinkedListNode.Next.Next; } }
public static IEnumerable <SingleLinkedListNode <T> > Nodes <T>(SingleLinkedListNode <T> singleLinkedListNode) { while (singleLinkedListNode != null) { yield return(singleLinkedListNode); singleLinkedListNode = singleLinkedListNode.Next; } }
public static SingleLinkedListNode <T> Insert <T>(SingleLinkedListNode <T> singleLinkedListNode, T value) { var newNode = new SingleLinkedListNode <T> { Value = value, Next = singleLinkedListNode.Next }; singleLinkedListNode.Next = newNode; return(newNode); }
public int Pop() { if (_lastItem == null) { throw new Exception("Stack is empty"); } var lastItem = _lastItem; _lastItem = lastItem.Next; return(lastItem.Value); }
public void Enqueue(ValueType value) { if (_tail == null) { _tail = new SingleLinkedListNode <ValueType> { Value = value, Next = null }; _head = _tail; } else { _tail.Next = new SingleLinkedListNode <ValueType> { Value = value, Next = null }; _tail = _tail.Next; } }
public void Push(int value) { if (_lastItem == null) { _lastItem = new SingleLinkedListNode <int> { Value = value, Next = null } } ; else { var newItem = new SingleLinkedListNode <int> { Value = value, Next = _lastItem }; _lastItem = newItem; } }
public ValueType Dequeue() { if (_head == null) { throw new Exception("Queue is empty"); } var returnValue = _head.Value; _head = _head.Next; if (_head == null) { _tail = null; } return(returnValue); }
public void Add(string key) { if (ContainsKey(key)) { return; } var bucketIndex = Hash(key); if (_buckets[bucketIndex] == null) { _buckets[bucketIndex] = new SingleLinkedListNode <string> { Value = key, Next = null } } ; else { SingleLinkedList.Insert(_buckets[bucketIndex], key); } }
public static SingleLinkedListNode <T> Reverse <T>(SingleLinkedListNode <T> root) { var stack = new Stack <SingleLinkedListNode <T> >(); foreach (var entry in Nodes(root)) { stack.Push(entry); } var newRoot = stack.Pop(); var current = newRoot; while (current != null) { var next = stack.Count > 0 ? stack.Pop() : null; current.Next = next; current = next; } return(newRoot); }
public static SingleLinkedListNode <T> ReverseInPlace <T>(SingleLinkedListNode <T> root) { if (root == null || root.Next == null) { return(root); } var currentNode = root; var nextNode = root.Next; var nextNextNode = nextNode.Next; currentNode.Next = null; while (nextNode != null) { nextNode.Next = currentNode; currentNode = nextNode; nextNode = nextNextNode; nextNextNode = nextNode?.Next; } return(currentNode); }
public void Add(string key, TValueType value) { if (ContainsKey(key)) { return; } var bucketIndex = Hash(key); if (_buckets[bucketIndex] == null) { _buckets[bucketIndex] = new SingleLinkedListNode <Entry> { Value = new Entry { Key = key, Value = value }, Next = null } } ; else { Insert(_buckets[bucketIndex], new Entry { Key = key, Value = value }); } }