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
```
```
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
@ -67,10 +80,10 @@ Remove(head, value)
end if
return true
end if
while n.next = ø and n.next.value = value
while n.next != ø and n.next.value != value
n ← n.next
end while
if n.next = ø
if n.next != ø
if n.next = tail
tail ← n
end if
@ -88,7 +101,7 @@ 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
while n != 0
yield n.value
n ← n.next
end while
@ -101,11 +114,11 @@ end Traverse
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 = ø
if tail != ø
curr ← tail
while curr = head
while curr != head
prev ← head
while prev.next = curr
while prev.next != curr
prev ← prev.next
end while
yield curr.value