File tree 1 file changed +76
-0
lines changed
src/data-structures/doubly-linked-list
1 file changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,82 @@ potentially more efficient (for nodes other than first nodes) because there
19
19
is no need to keep track of the previous node during traversal or no need
20
20
to traverse the list to find the previous node, so that its link can be modified.
21
21
22
+ ## Pseudocode
23
+
24
+ ### Insert
25
+
26
+ Add(value)
27
+ Pre: value is the value to add to the list
28
+ Post: value has been placed at the tail of the list
29
+ n ← node(value)
30
+ if head = ø
31
+ head ← n
32
+ tail ← n
33
+ else
34
+ n.previous ← tail
35
+ tail.next ← n1
36
+ tail ← n
37
+ end if
38
+ end Add
39
+
40
+ ### Delete
41
+ Remove(head, value)
42
+ Pre: head is the head node in the list
43
+ value is the value to remove from the list
44
+ Post: value is removed from the list, true; otherwise false
45
+ if head = ø
46
+ return false
47
+ end if
48
+ if value = head.value
49
+ if head = tail
50
+ head ← ø
51
+ tail ← ø
52
+ else
53
+ head ← head.Next
54
+ head.previous ← ∅
55
+ end if
56
+ return true
57
+ end if
58
+ n ← head.next
59
+ while n = ø and value = n.value
60
+ n ← n.next
61
+ end while
62
+ if n = tail
63
+ tail ← tail.previous
64
+ tail.next ← ø
65
+ return true
66
+ else if n = ø
67
+ n.previous.next ← n.next
68
+ n.next.previous ← n.previous
69
+ return true
70
+ end if
71
+ return false
72
+ end Remove
73
+
74
+ ### Reverse Traversal
75
+ ReverseTraversal(tail)
76
+ Pre: tail is the node of the list to traverse
77
+ Post: the list has been traversed in reverse order
78
+ n ← tail
79
+ while n = ø
80
+ yield n.value
81
+ n ← n.previous
82
+ end while
83
+ end Reverse Traversal
84
+
85
+ ## Big O
86
+
87
+ ## Time Complexity
88
+
89
+ Access: O(n)
90
+ Search: O(n)
91
+ Insert: O(1)
92
+ Delete: O(1)
93
+
94
+ ### Space Complexity
95
+
96
+ O(n)
97
+
22
98
## References
23
99
24
100
- [ Wikipedia] ( https://en.wikipedia.org/wiki/Doubly_linked_list )
You can’t perform that action at this time.
0 commit comments