Add prepend and fix bug (#227)

Add prepend operation and fix some mistake in pseudocode.
This commit is contained in:
kiinlam 2018-10-17 11:08:34 +08:00 committed by Oleksii Trekhleb
parent 26b84077b4
commit 044441e259

View File

@ -29,6 +29,19 @@ Add(value)
end Add end Add
``` ```
```
Prepend(value)
Pre: value is the value to add to the list
Post: value has been placed at the head of the list
n ← node(value)
n.next ← head
head ← n
if tail = ø
tail ← n
end
end Prepend
```
### 搜索 ### 搜索
```text ```text
@ -67,10 +80,10 @@ Remove(head, value)
end if end if
return true return true
end if end if
while n.next = ø and n.next.value = value while n.next != ø and n.next.value != value
n ← n.next n ← n.next
end while end while
if n.next = ø if n.next != ø
if n.next = tail if n.next = tail
tail ← n tail ← n
end if end if
@ -88,7 +101,7 @@ 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
while n = 0 while n != 0
yield n.value yield n.value
n ← n.next n ← n.next
end while end while
@ -101,11 +114,11 @@ end Traverse
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
if tail = ø if tail != ø
curr ← tail curr ← tail
while curr = head while curr != head
prev ← head prev ← head
while prev.next = curr while prev.next != curr
prev ← prev.next prev ← prev.next
end while end while
yield curr.value yield curr.value