From 044441e2594051ebb845294c700099c22c4483de Mon Sep 17 00:00:00 2001 From: kiinlam <516523522@qq.com> Date: Wed, 17 Oct 2018 11:08:34 +0800 Subject: [PATCH] Add prepend and fix bug (#227) Add prepend operation and fix some mistake in pseudocode. --- .../linked-list/README.zh-CN.md | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/data-structures/linked-list/README.zh-CN.md b/src/data-structures/linked-list/README.zh-CN.md index e135f7b1..55ec7ad6 100644 --- a/src/data-structures/linked-list/README.zh-CN.md +++ b/src/data-structures/linked-list/README.zh-CN.md @@ -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 @@ -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