Add pseudocodes to LinkedList.

This commit is contained in:
Oleksii Trekhleb 2018-08-13 10:38:19 +03:00
parent 02b70d95d6
commit f6c091bcb1

View File

@ -18,9 +18,12 @@ 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
```text
Add(value) Add(value)
Pre: value is the value to add to the list Pre: value is the value to add to the list
Post: value has been placed at the tail of the list Post: value has been placed at the tail of the list
@ -33,8 +36,11 @@ have better cache locality as compared to linked lists.
tail ← n tail ← n
end if end if
end Add end Add
```
### Search ### Search
```text
Contains(head, value) Contains(head, value)
Pre: head is the head node in the list Pre: head is the head node in the list
value is the value to search for value is the value to search for
@ -48,8 +54,11 @@ have better cache locality as compared to linked lists.
end if end if
return true return true
end Contains end Contains
```
### Delete ### Delete
```text
Remove(head, value) Remove(head, value)
Pre: head is the head node in the list Pre: head is the head node in the list
Post: value is the value to remove from the list, true, otherwise false Post: value is the value to remove from the list, true, otherwise false
@ -78,8 +87,11 @@ have better cache locality as compared to linked lists.
end if end if
return false return false
end Remove end Remove
```
### Traverse ### Traverse
```text
Traverse(head) Traverse(head)
Pre: head is the head node in the list Pre: head is the head node in the list
Post: the items in the list have been traversed Post: the items in the list have been traversed
@ -89,8 +101,11 @@ have better cache locality as compared to linked lists.
n ← n.next n ← n.next
end while end while
end Traverse end Traverse
```
### Traverse in Reverse ### Traverse in Reverse
```text
ReverseTraversal(head, tail) ReverseTraversal(head, tail)
Pre: head and tail belong to the same list Pre: head and tail belong to the same list
Post: the items in the list have been traversed in reverse order Post: the items in the list have been traversed in reverse order
@ -107,18 +122,19 @@ have better cache locality as compared to linked lists.
yeild curr.value yeild curr.value
end if end if
end ReverseTraversal end ReverseTraversal
```
## Complexities
## Big *O*
### 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