diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2018-12-11 22:18:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-11 22:18:40 +0100 |
commit | 239846a528eefa64e730afbbe770ad6381a7483a (patch) | |
tree | f263ac525c40b0d349c282e5557579824f9f8c15 /lib | |
parent | c58f430e2e7d393a65f80f4ee622c385f7641a1b (diff) | |
parent | 4e483bb01abf89b164e21c77003ead033fa92e08 (diff) | |
download | Nim-239846a528eefa64e730afbbe770ad6381a7483a.tar.gz |
Merge pull request #9805 from pacien/181126-list-append
add SinglyLinkedList.append procs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/collections/lists.nim | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 0b3708a7c..15ce5d074 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -140,11 +140,26 @@ proc contains*[T](L: SomeLinkedCollection[T], value: T): bool {.inline.} = ## exist, true otherwise. result = find(L, value) != nil +proc append*[T](L: var SinglyLinkedList[T], + n: SinglyLinkedNode[T]) {.inline.} = + ## appends a node `n` to `L`. Efficiency: O(1). + n.next = nil + if L.tail != nil: + assert(L.tail.next == nil) + L.tail.next = n + L.tail = n + if L.head == nil: L.head = n + +proc append*[T](L: var SinglyLinkedList[T], value: T) {.inline.} = + ## appends a value to `L`. Efficiency: O(1). + append(L, newSinglyLinkedNode(value)) + proc prepend*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} = ## prepends a node to `L`. Efficiency: O(1). n.next = L.head L.head = n + if L.tail == nil: L.tail = n proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} = ## prepends a node to `L`. Efficiency: O(1). |