mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
parent
37c7be15e9
commit
02b70d95d6
@ -18,6 +18,107 @@ 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
|
||||||
|
|
||||||
|
### 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
|
||||||
|
tail.next ← n
|
||||||
|
tail ← n
|
||||||
|
end if
|
||||||
|
end Add
|
||||||
|
|
||||||
|
### Search
|
||||||
|
Contains(head, value)
|
||||||
|
Pre: head is the head node in the list
|
||||||
|
value is the value to search for
|
||||||
|
Post: the item is either in the linked list, true; otherwise false
|
||||||
|
n ← head
|
||||||
|
while n = ø and n.value = value
|
||||||
|
n ← n.next
|
||||||
|
end while
|
||||||
|
if n = ø
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
return true
|
||||||
|
end Contains
|
||||||
|
|
||||||
|
### Delete
|
||||||
|
Remove(head, value)
|
||||||
|
Pre: head is the head node in the list
|
||||||
|
Post: value is the value to remove from the list, true, otherwise false
|
||||||
|
if head = ø
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
n ← head
|
||||||
|
if n.value = value
|
||||||
|
if head = tail
|
||||||
|
head ← ø
|
||||||
|
tail ← ø
|
||||||
|
else
|
||||||
|
head ← head.next
|
||||||
|
end if
|
||||||
|
return true
|
||||||
|
end if
|
||||||
|
while n.next = ø and n.next.value = value
|
||||||
|
n ← n.next
|
||||||
|
end while
|
||||||
|
if n.next = ø
|
||||||
|
if n.next = tail
|
||||||
|
tail ← n
|
||||||
|
end if
|
||||||
|
n.next ← n.next.next
|
||||||
|
return true
|
||||||
|
end if
|
||||||
|
return false
|
||||||
|
end Remove
|
||||||
|
|
||||||
|
### Traverse
|
||||||
|
Traverse(head)
|
||||||
|
Pre: head is the head node in the list
|
||||||
|
Post: the items in the list have been traversed
|
||||||
|
n ← head
|
||||||
|
while n = 0
|
||||||
|
yield n.value
|
||||||
|
n ← n.next
|
||||||
|
end while
|
||||||
|
end Traverse
|
||||||
|
|
||||||
|
### 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
|
||||||
|
|
||||||
|
|
||||||
|
## Big *O*
|
||||||
|
|
||||||
|
### Time Complexity
|
||||||
|
Access: *O*(*n*) \
|
||||||
|
Search: *O*(*n*) \
|
||||||
|
Insert: *O*(1) \
|
||||||
|
Delete: *O*(1)
|
||||||
|
|
||||||
|
### Space Complexity
|
||||||
|
*O*(*n*)
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user