@@ -19,77 +19,84 @@ 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
22
+ ## Pseudocode for Basic Operations
23
23
24
24
### Insert
25
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
26
+ ``` text
27
+ Add(value)
28
+ Pre: value is the value to add to the list
29
+ Post: value has been placed at the tail of the list
30
+ n ← node(value)
31
+ if head = ø
32
+ head ← n
33
+ tail ← n
34
+ else
35
+ n.previous ← tail
36
+ tail.next ← n
37
+ tail ← n
38
+ end if
39
+ end Add
40
+ ```
39
41
40
42
### 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
43
+
44
+ ``` text
45
+ Remove(head, value)
46
+ Pre: head is the head node in the list
47
+ value is the value to remove from the list
48
+ Post: value is removed from the list, true; otherwise false
49
+ if head = ø
50
+ return false
51
+ end if
52
+ if value = head.value
53
+ if head = tail
54
+ head ← ø
55
+ tail ← ø
56
+ else
57
+ head ← head.Next
58
+ head.previous ← ø
59
+ end if
60
+ return true
61
+ end if
62
+ n ← head.next
63
+ while n = ø and value = n.value
64
+ n ← n.next
65
+ end while
66
+ if n = tail
67
+ tail ← tail.previous
68
+ tail.next ← ø
69
+ return true
70
+ else if n = ø
71
+ n.previous.next ← n.next
72
+ n.next.previous ← n.previous
73
+ return true
74
+ end if
75
+ return false
76
+ end Remove
77
+ ```
73
78
74
79
### 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
80
+
81
+ ``` text
82
+ ReverseTraversal(tail)
83
+ Pre: tail is the node of the list to traverse
84
+ Post: the list has been traversed in reverse order
85
+ n ← tail
86
+ while n = ø
87
+ yield n.value
88
+ n ← n.previous
89
+ end while
90
+ end Reverse Traversal
91
+ ```
84
92
85
- ## Big O
93
+ ## Complexities
86
94
87
95
## Time Complexity
88
96
89
- Access: O(n)
90
- Search: O(n)
91
- Insert: O(1)
92
- Delete: O(1)
97
+ | Access | Search | Insertion | Deletion |
98
+ | :-------: | :-------: | :-------: | :-------: |
99
+ | O(n) | O(n) | O(1) | O(1) |
93
100
94
101
### Space Complexity
95
102
0 commit comments