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.
![Linked List](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
## Pseudocode
## Pseudocode for Basic Operations
### Insert
```text
Add(value)
Pre: value is the value to add to 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
end if
end Add
```
### Search
```text
Contains(head, value)
Pre: head is the head node in the list
value is the value to search for
@ -48,8 +54,11 @@ have better cache locality as compared to linked lists.
end if
return true
end Contains
```
### Delete
```text
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
@ -78,8 +87,11 @@ have better cache locality as compared to linked lists.
end if
return false
end Remove
```
### Traverse
```text
Traverse(head)
Pre: head is the head node in the list
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
end while
end Traverse
```
### Traverse in Reverse
```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
@ -107,18 +122,19 @@ have better cache locality as compared to linked lists.
yeild curr.value
end if
end ReverseTraversal
```
## Big *O*
## Complexities
### Time Complexity
Access: *O*(*n*) \
Search: *O*(*n*) \
Insert: *O*(1) \
Delete: *O*(1)
| Access | Search | Insertion | Deletion |
| :-------: | :-------: | :-------: | :-------: |
| O(n) | O(n) | O(1) | O(1) |
### Space Complexity
*O*(*n*)
O(n)
## References