Skip to content

Commit 044441e

Browse files
kiinlamtrekhleb
authored andcommittedOct 17, 2018
Add prepend and fix bug (trekhleb#227)
Add prepend operation and fix some mistake in pseudocode.
1 parent 26b8407 commit 044441e

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed
 

‎src/data-structures/linked-list/README.zh-CN.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ Add(value)
2828
end if
2929
end Add
3030
```
31-
31+
32+
```
33+
Prepend(value)
34+
Pre: value is the value to add to the list
35+
Post: value has been placed at the head of the list
36+
n ← node(value)
37+
n.next ← head
38+
head ← n
39+
if tail = ø
40+
tail ← n
41+
end
42+
end Prepend
43+
```
44+
3245
### 搜索
3346

3447
```text
@@ -67,10 +80,10 @@ Remove(head, value)
6780
end if
6881
return true
6982
end if
70-
while n.next = ø and n.next.value = value
83+
while n.next != ø and n.next.value != value
7184
n ← n.next
7285
end while
73-
if n.next = ø
86+
if n.next != ø
7487
if n.next = tail
7588
tail ← n
7689
end if
@@ -88,7 +101,7 @@ Traverse(head)
88101
Pre: head is the head node in the list
89102
Post: the items in the list have been traversed
90103
n ← head
91-
while n = 0
104+
while n != 0
92105
yield n.value
93106
n ← n.next
94107
end while
@@ -101,11 +114,11 @@ end Traverse
101114
ReverseTraversal(head, tail)
102115
Pre: head and tail belong to the same list
103116
Post: the items in the list have been traversed in reverse order
104-
if tail = ø
117+
if tail != ø
105118
curr ← tail
106-
while curr = head
119+
while curr != head
107120
prev ← head
108-
while prev.next = curr
121+
while prev.next != curr
109122
prev ← prev.next
110123
end while
111124
yield curr.value

0 commit comments

Comments
 (0)
Please sign in to comment.