@@ -18,107 +18,123 @@ access, such as random access, is not feasible. Arrays
18
18
have better cache locality as compared to linked lists.
19
19
20
20
![ Linked List] ( https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg )
21
- ## Pseudocode
21
+
22
+ ## Pseudocode for Basic Operations
22
23
23
24
### Insert
24
- Add(value)
25
- Pre: value is the value to add to the list
26
- Post: value has been placed at the tail of the list
27
- n ← node(value)
28
- if head = ø
29
- head ← n
30
- tail ← n
31
- else
32
- tail.next ← n
33
- tail ← n
34
- end if
35
- end Add
25
+
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
+ tail.next ← n
36
+ tail ← n
37
+ end if
38
+ end Add
39
+ ```
36
40
37
41
### Search
38
- Contains(head, value)
39
- Pre: head is the head node in the list
40
- value is the value to search for
41
- Post: the item is either in the linked list, true; otherwise false
42
- n ← head
43
- while n = ø and n.value = value
44
- n ← n.next
45
- end while
46
- if n = ø
47
- return false
48
- end if
49
- return true
50
- end Contains
42
+
43
+ ``` text
44
+ Contains(head, value)
45
+ Pre: head is the head node in the list
46
+ value is the value to search for
47
+ Post: the item is either in the linked list, true; otherwise false
48
+ n ← head
49
+ while n = ø and n.value = value
50
+ n ← n.next
51
+ end while
52
+ if n = ø
53
+ return false
54
+ end if
55
+ return true
56
+ end Contains
57
+ ```
51
58
52
59
### Delete
53
- Remove(head, value)
54
- Pre: head is the head node in the list
55
- Post: value is the value to remove from the list, true, otherwise false
56
- if head = ø
57
- return false
58
- end if
59
- n ← head
60
- if n.value = value
61
- if head = tail
62
- head ← ø
63
- tail ← ø
64
- else
65
- head ← head.next
66
- end if
67
- return true
68
- end if
69
- while n.next = ø and n.next.value = value
70
- n ← n.next
71
- end while
72
- if n.next = ø
73
- if n.next = tail
74
- tail ← n
75
- end if
76
- n.next ← n.next.next
77
- return true
78
- end if
79
- return false
80
- end Remove
60
+
61
+ ``` text
62
+ Remove(head, value)
63
+ Pre: head is the head node in the list
64
+ Post: value is the value to remove from the list, true, otherwise false
65
+ if head = ø
66
+ return false
67
+ end if
68
+ n ← head
69
+ if n.value = value
70
+ if head = tail
71
+ head ← ø
72
+ tail ← ø
73
+ else
74
+ head ← head.next
75
+ end if
76
+ return true
77
+ end if
78
+ while n.next = ø and n.next.value = value
79
+ n ← n.next
80
+ end while
81
+ if n.next = ø
82
+ if n.next = tail
83
+ tail ← n
84
+ end if
85
+ n.next ← n.next.next
86
+ return true
87
+ end if
88
+ return false
89
+ end Remove
90
+ ```
81
91
82
92
### Traverse
83
- Traverse(head)
84
- Pre: head is the head node in the list
85
- Post: the items in the list have been traversed
86
- n ← head
87
- while n = 0
88
- yield n.value
89
- n ← n.next
90
- end while
91
- end Traverse
93
+
94
+ ``` text
95
+ Traverse(head)
96
+ Pre: head is the head node in the list
97
+ Post: the items in the list have been traversed
98
+ n ← head
99
+ while n = 0
100
+ yield n.value
101
+ n ← n.next
102
+ end while
103
+ end Traverse
104
+ ```
92
105
93
106
### Traverse in Reverse
94
- ReverseTraversal(head, tail)
95
- Pre: head and tail belong to the same list
96
- Post: the items in the list have been traversed in reverse order
97
- if tail = ø
98
- curr ← tail
99
- while curr = head
100
- prev ← head
101
- while prev.next = curr
102
- prev ← prev.next
103
- end while
104
- yield curr.value
105
- curr ← prev
106
- end while
107
- yeild curr.value
108
- end if
109
- end ReverseTraversal
110
-
111
-
112
- ## Big * O*
107
+
108
+ ``` text
109
+ ReverseTraversal(head, tail)
110
+ Pre: head and tail belong to the same list
111
+ Post: the items in the list have been traversed in reverse order
112
+ if tail = ø
113
+ curr ← tail
114
+ while curr = head
115
+ prev ← head
116
+ while prev.next = curr
117
+ prev ← prev.next
118
+ end while
119
+ yield curr.value
120
+ curr ← prev
121
+ end while
122
+ yeild curr.value
123
+ end if
124
+ end ReverseTraversal
125
+ ```
126
+
127
+ ## Complexities
113
128
114
129
### Time Complexity
115
- Access: * O * ( * n * ) \
116
- Search: * O * ( * n * ) \
117
- Insert: * O * (1) \
118
- Delete: * O * (1)
130
+
131
+ | Access | Search | Insertion | Deletion |
132
+ | :-------: | :-------: | :-------: | :-------: |
133
+ | O(n) | O(n) | O(1) | O(1) |
119
134
120
135
### Space Complexity
121
- * O* (* n* )
136
+
137
+ O(n)
122
138
123
139
## References
124
140
0 commit comments