From 7207bcefb2662a67b5eeebd82f09b8402714dd2e Mon Sep 17 00:00:00 2001 From: Moshe Date: Tue, 14 Aug 2018 08:20:09 -0400 Subject: [PATCH] Update README.md (#164) Add Pseudocode for Doubly Linked List --- .../doubly-linked-list/README.md | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/data-structures/doubly-linked-list/README.md b/src/data-structures/doubly-linked-list/README.md index 58ffb8f3..83667e6a 100644 --- a/src/data-structures/doubly-linked-list/README.md +++ b/src/data-structures/doubly-linked-list/README.md @@ -19,6 +19,82 @@ potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified. +## Pseudocode + +### Insert + + Add(value) + Pre: value is the value to add to the list + Post: value has been placed at the tail of the list + n ← node(value) + if head = ø + head ← n + tail ← n + else + n.previous ← tail + tail.next ← n1 + tail ← n + end if + end Add + +### Delete + Remove(head, value) + Pre: head is the head node in the list + value is the value to remove from the list + Post: value is removed from the list, true; otherwise false + if head = ø + return false + end if + if value = head.value + if head = tail + head ← ø + tail ← ø + else + head ← head.Next + head.previous ← ∅ + end if + return true + end if + n ← head.next + while n = ø and value = n.value + n ← n.next + end while + if n = tail + tail ← tail.previous + tail.next ← ø + return true + else if n = ø + n.previous.next ← n.next + n.next.previous ← n.previous + return true + end if + return false + end Remove + +### Reverse Traversal + ReverseTraversal(tail) + Pre: tail is the node of the list to traverse + Post: the list has been traversed in reverse order + n ← tail + while n = ø + yield n.value + n ← n.previous + end while + end Reverse Traversal + +## Big O + +## Time Complexity + +Access: O(n) +Search: O(n) +Insert: O(1) +Delete: O(1) + +### Space Complexity + +O(n) + ## References - [Wikipedia](https://en.wikipedia.org/wiki/Doubly_linked_list)