Skip to content

Commit

Permalink
Add prepend and fix bug (trekhleb#227)
Browse files Browse the repository at this point in the history
Add prepend operation and fix some mistake in pseudocode.
  • Loading branch information
kiinlam authored and trekhleb committed Oct 17, 2018
1 parent 26b8407 commit 044441e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/data-structures/linked-list/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ Add(value)
end if
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 044441e

Please sign in to comment.