Fix SinglyLinkedRing in lists module
- SinglyLinkedRing's prepend was broken - needed a tail so that prepend can work properly - now append works as well, so I added it too - simple testcase added as well
This commit is contained in:
parent
2780e7a54a
commit
11a5a4a9a6
2 changed files with 51 additions and 4 deletions
|
|
@ -32,7 +32,7 @@ type
|
||||||
head*, tail*: DoublyLinkedNode[T]
|
head*, tail*: DoublyLinkedNode[T]
|
||||||
|
|
||||||
SinglyLinkedRing*[T] = object ## a singly linked ring
|
SinglyLinkedRing*[T] = object ## a singly linked ring
|
||||||
head*: SinglyLinkedNode[T]
|
head*, tail*: SinglyLinkedNode[T]
|
||||||
|
|
||||||
DoublyLinkedRing*[T] = object ## a doubly linked ring
|
DoublyLinkedRing*[T] = object ## a doubly linked ring
|
||||||
head*: DoublyLinkedNode[T]
|
head*: DoublyLinkedNode[T]
|
||||||
|
|
@ -267,13 +267,31 @@ proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
if n.prev != nil: n.prev.next = n.next
|
if n.prev != nil: n.prev.next = n.next
|
||||||
|
|
||||||
|
|
||||||
|
proc append*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
|
## appends a node `n` to `L`. Efficiency: O(1).
|
||||||
|
if L.head != nil:
|
||||||
|
n.next = L.head
|
||||||
|
assert(L.tail != nil)
|
||||||
|
L.tail.next = n
|
||||||
|
L.tail = n
|
||||||
|
else:
|
||||||
|
n.next = n
|
||||||
|
L.head = n
|
||||||
|
L.tail = n
|
||||||
|
|
||||||
|
proc append*[T](L: var SinglyLinkedRing[T], value: T) =
|
||||||
|
## appends a value to `L`. Efficiency: O(1).
|
||||||
|
append(L, newSinglyLinkedNode(value))
|
||||||
|
|
||||||
proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
## prepends a node `n` to `L`. Efficiency: O(1).
|
## prepends a node `n` to `L`. Efficiency: O(1).
|
||||||
if L.head != nil:
|
if L.head != nil:
|
||||||
n.next = L.head
|
n.next = L.head
|
||||||
L.head.next = n
|
assert(L.tail != nil)
|
||||||
|
L.tail.next = n
|
||||||
else:
|
else:
|
||||||
n.next = n
|
n.next = n
|
||||||
|
L.tail = n
|
||||||
L.head = n
|
L.head = n
|
||||||
|
|
||||||
proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
|
proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
|
||||||
|
|
|
||||||
29
tests/stdlib/tsinglylinkedring.nim
Normal file
29
tests/stdlib/tsinglylinkedring.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
discard """
|
||||||
|
output: '''[5]
|
||||||
|
[4, 5]
|
||||||
|
[3, 4, 5]
|
||||||
|
[2, 3, 4, 5]
|
||||||
|
[2, 3, 4, 5, 6]
|
||||||
|
[2, 3, 4, 5, 6, 7]
|
||||||
|
[2, 3, 4, 5, 6, 7, 8]
|
||||||
|
[1, 2, 3, 4, 5, 6, 7, 8]'''
|
||||||
|
"""
|
||||||
|
import lists
|
||||||
|
|
||||||
|
var r = initSinglyLinkedRing[int]()
|
||||||
|
r.prepend(5)
|
||||||
|
echo r
|
||||||
|
r.prepend(4)
|
||||||
|
echo r
|
||||||
|
r.prepend(3)
|
||||||
|
echo r
|
||||||
|
r.prepend(2)
|
||||||
|
echo r
|
||||||
|
r.append(6)
|
||||||
|
echo r
|
||||||
|
r.append(7)
|
||||||
|
echo r
|
||||||
|
r.append(8)
|
||||||
|
echo r
|
||||||
|
r.prepend(1)
|
||||||
|
echo r
|
||||||
Loading…
Add table
Add a link
Reference in a new issue