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,10 +18,13 @@ 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)
```text
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
n ← node(value) n ← node(value)
@ -32,10 +35,13 @@ have better cache locality as compared to linked lists.
tail.next ← n tail.next ← n
tail ← n tail ← n
end if end if
end Add end Add
```
### Search ### Search
Contains(head, value)
```text
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
Post: the item is either in the linked list, true; otherwise false Post: the item is either in the linked list, true; otherwise false
@ -47,10 +53,13 @@ have better cache locality as compared to linked lists.
return false return false
end if end if
return true return true
end Contains end Contains
```
### Delete ### Delete
Remove(head, value)
```text
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
if head = ø if head = ø
@ -77,10 +86,13 @@ have better cache locality as compared to linked lists.
return true return true
end if end if
return false return false
end Remove end Remove
```
### Traverse ### Traverse
Traverse(head)
```text
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
n ← head n ← head
@ -88,10 +100,13 @@ have better cache locality as compared to linked lists.
yield n.value yield n.value
n ← n.next n ← n.next
end while end while
end Traverse end Traverse
```
### Traverse in Reverse ### Traverse in Reverse
ReverseTraversal(head, tail)
```text
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
if tail = ø if tail = ø
@ -106,19 +121,20 @@ have better cache locality as compared to linked lists.
end while end while
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