mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add pseudocodes to LinkedList.
This commit is contained in:
parent
02b70d95d6
commit
f6c091bcb1
@ -18,107 +18,123 @@ access, such as random access, is not feasible. Arrays
|
|||||||
have better cache locality as compared to linked lists.
|
have better cache locality as compared to linked lists.
|
||||||
|
|
||||||
![Linked List](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
|
![Linked List](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
|
||||||
## Pseudocode
|
|
||||||
|
## Pseudocode for Basic Operations
|
||||||
|
|
||||||
### Insert
|
### Insert
|
||||||
Add(value)
|
|
||||||
Pre: value is the value to add to the list
|
```text
|
||||||
Post: value has been placed at the tail of the list
|
Add(value)
|
||||||
n ← node(value)
|
Pre: value is the value to add to the list
|
||||||
if head = ø
|
Post: value has been placed at the tail of the list
|
||||||
head ← n
|
n ← node(value)
|
||||||
tail ← n
|
if head = ø
|
||||||
else
|
head ← n
|
||||||
tail.next ← n
|
tail ← n
|
||||||
tail ← n
|
else
|
||||||
end if
|
tail.next ← n
|
||||||
end Add
|
tail ← n
|
||||||
|
end if
|
||||||
|
end Add
|
||||||
|
```
|
||||||
|
|
||||||
### Search
|
### Search
|
||||||
Contains(head, value)
|
|
||||||
Pre: head is the head node in the list
|
```text
|
||||||
value is the value to search for
|
Contains(head, value)
|
||||||
Post: the item is either in the linked list, true; otherwise false
|
Pre: head is the head node in the list
|
||||||
n ← head
|
value is the value to search for
|
||||||
while n = ø and n.value = value
|
Post: the item is either in the linked list, true; otherwise false
|
||||||
n ← n.next
|
n ← head
|
||||||
end while
|
while n = ø and n.value = value
|
||||||
if n = ø
|
n ← n.next
|
||||||
return false
|
end while
|
||||||
end if
|
if n = ø
|
||||||
return true
|
return false
|
||||||
end Contains
|
end if
|
||||||
|
return true
|
||||||
|
end Contains
|
||||||
|
```
|
||||||
|
|
||||||
### Delete
|
### Delete
|
||||||
Remove(head, value)
|
|
||||||
Pre: head is the head node in the list
|
```text
|
||||||
Post: value is the value to remove from the list, true, otherwise false
|
Remove(head, value)
|
||||||
if head = ø
|
Pre: head is the head node in the list
|
||||||
return false
|
Post: value is the value to remove from the list, true, otherwise false
|
||||||
end if
|
if head = ø
|
||||||
n ← head
|
return false
|
||||||
if n.value = value
|
end if
|
||||||
if head = tail
|
n ← head
|
||||||
head ← ø
|
if n.value = value
|
||||||
tail ← ø
|
if head = tail
|
||||||
else
|
head ← ø
|
||||||
head ← head.next
|
tail ← ø
|
||||||
end if
|
else
|
||||||
return true
|
head ← head.next
|
||||||
end if
|
end if
|
||||||
while n.next = ø and n.next.value = value
|
return true
|
||||||
n ← n.next
|
end if
|
||||||
end while
|
while n.next = ø and n.next.value = value
|
||||||
if n.next = ø
|
n ← n.next
|
||||||
if n.next = tail
|
end while
|
||||||
tail ← n
|
if n.next = ø
|
||||||
end if
|
if n.next = tail
|
||||||
n.next ← n.next.next
|
tail ← n
|
||||||
return true
|
end if
|
||||||
end if
|
n.next ← n.next.next
|
||||||
return false
|
return true
|
||||||
end Remove
|
end if
|
||||||
|
return false
|
||||||
|
end Remove
|
||||||
|
```
|
||||||
|
|
||||||
### Traverse
|
### Traverse
|
||||||
Traverse(head)
|
|
||||||
Pre: head is the head node in the list
|
```text
|
||||||
Post: the items in the list have been traversed
|
Traverse(head)
|
||||||
n ← head
|
Pre: head is the head node in the list
|
||||||
while n = 0
|
Post: the items in the list have been traversed
|
||||||
yield n.value
|
n ← head
|
||||||
n ← n.next
|
while n = 0
|
||||||
end while
|
yield n.value
|
||||||
end Traverse
|
n ← n.next
|
||||||
|
end while
|
||||||
|
end Traverse
|
||||||
|
```
|
||||||
|
|
||||||
### Traverse in Reverse
|
### Traverse in Reverse
|
||||||
ReverseTraversal(head, tail)
|
|
||||||
Pre: head and tail belong to the same list
|
|
||||||
Post: the items in the list have been traversed in reverse order
|
|
||||||
if tail = ø
|
|
||||||
curr ← tail
|
|
||||||
while curr = head
|
|
||||||
prev ← head
|
|
||||||
while prev.next = curr
|
|
||||||
prev ← prev.next
|
|
||||||
end while
|
|
||||||
yield curr.value
|
|
||||||
curr ← prev
|
|
||||||
end while
|
|
||||||
yeild curr.value
|
|
||||||
end if
|
|
||||||
end ReverseTraversal
|
|
||||||
|
|
||||||
|
```text
|
||||||
|
ReverseTraversal(head, tail)
|
||||||
|
Pre: head and tail belong to the same list
|
||||||
|
Post: the items in the list have been traversed in reverse order
|
||||||
|
if tail = ø
|
||||||
|
curr ← tail
|
||||||
|
while curr = head
|
||||||
|
prev ← head
|
||||||
|
while prev.next = curr
|
||||||
|
prev ← prev.next
|
||||||
|
end while
|
||||||
|
yield curr.value
|
||||||
|
curr ← prev
|
||||||
|
end while
|
||||||
|
yeild curr.value
|
||||||
|
end if
|
||||||
|
end ReverseTraversal
|
||||||
|
```
|
||||||
|
|
||||||
## Big *O*
|
## Complexities
|
||||||
|
|
||||||
### Time Complexity
|
### Time Complexity
|
||||||
Access: *O*(*n*) \
|
|
||||||
Search: *O*(*n*) \
|
| Access | Search | Insertion | Deletion |
|
||||||
Insert: *O*(1) \
|
| :-------: | :-------: | :-------: | :-------: |
|
||||||
Delete: *O*(1)
|
| O(n) | O(n) | O(1) | O(1) |
|
||||||
|
|
||||||
### Space Complexity
|
### Space Complexity
|
||||||
*O*(*n*)
|
|
||||||
|
O(n)
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user