W3cubDocs

/Nim

Module lists

Implementation of singly and doubly linked lists. Because it makes no sense to do so, the 'next' and 'prev' pointers are not hidden from you and can be manipulated directly for efficiency.

Types

DoublyLinkedNodeObj[T] = object
  next*, prev*: ref DoublyLinkedNodeObj[T]
  value*: T
a node a doubly linked list consists of
DoublyLinkedNode[T] = ref DoublyLinkedNodeObj[T]
SinglyLinkedNodeObj[T] = object
  next*: ref SinglyLinkedNodeObj[T]
  value*: T
a node a singly linked list consists of
SinglyLinkedNode[T] = ref SinglyLinkedNodeObj[T]
SinglyLinkedList[T] = object
  head*, tail*: SinglyLinkedNode[T]
a singly linked list
DoublyLinkedList[T] = object
  head*, tail*: DoublyLinkedNode[T]
a doubly linked list
SinglyLinkedRing[T] = object
  head*, tail*: SinglyLinkedNode[T]
a singly linked ring
DoublyLinkedRing[T] = object
  head*: DoublyLinkedNode[T]
a doubly linked ring

Procs

proc initSinglyLinkedList[T](): SinglyLinkedList[T]
creates a new singly linked list that is empty.
proc initDoublyLinkedList[T](): DoublyLinkedList[T]
creates a new doubly linked list that is empty.
proc initSinglyLinkedRing[T](): SinglyLinkedRing[T]
creates a new singly linked ring that is empty.
proc initDoublyLinkedRing[T](): DoublyLinkedRing[T]
creates a new doubly linked ring that is empty.
proc newDoublyLinkedNode[T](value: T): DoublyLinkedNode[T]
creates a new doubly linked node with the given value.
proc newSinglyLinkedNode[T](value: T): SinglyLinkedNode[T]
creates a new singly linked node with the given value.
proc `$`[T](L: SinglyLinkedList[T]): string
turns a list into its string representation.
proc `$`[T](L: DoublyLinkedList[T]): string
turns a list into its string representation.
proc `$`[T](L: SinglyLinkedRing[T]): string
turns a list into its string representation.
proc `$`[T](L: DoublyLinkedRing[T]): string
turns a list into its string representation.
proc find[T](L: SinglyLinkedList[T]; value: T): SinglyLinkedNode[T]
searches in the list for a value. Returns nil if the value does not exist.
proc find[T](L: DoublyLinkedList[T]; value: T): DoublyLinkedNode[T]
searches in the list for a value. Returns nil if the value does not exist.
proc find[T](L: SinglyLinkedRing[T]; value: T): SinglyLinkedNode[T]
searches in the list for a value. Returns nil if the value does not exist.
proc find[T](L: DoublyLinkedRing[T]; value: T): DoublyLinkedNode[T]
searches in the list for a value. Returns nil if the value does not exist.
proc contains[T](L: SinglyLinkedList[T]; value: T): bool {.inline.}
searches in the list for a value. Returns false if the value does not exist, true otherwise.
proc contains[T](L: DoublyLinkedList[T]; value: T): bool {.inline.}
searches in the list for a value. Returns false if the value does not exist, true otherwise.
proc contains[T](L: SinglyLinkedRing[T]; value: T): bool {.inline.}
searches in the list for a value. Returns false if the value does not exist, true otherwise.
proc contains[T](L: DoublyLinkedRing[T]; value: T): bool {.inline.}
searches in the list for a value. Returns false if the value does not exist, true otherwise.
proc prepend[T](L: var SinglyLinkedList[T]; n: SinglyLinkedNode[T]) {.inline.}
prepends a node to L. Efficiency: O(1).
proc prepend[T](L: var SinglyLinkedList[T]; value: T) {.inline.}
prepends a node to L. Efficiency: O(1).
proc append[T](L: var DoublyLinkedList[T]; n: DoublyLinkedNode[T])
appends a node n to L. Efficiency: O(1).
proc append[T](L: var DoublyLinkedList[T]; value: T)
appends a value to L. Efficiency: O(1).
proc prepend[T](L: var DoublyLinkedList[T]; n: DoublyLinkedNode[T])
prepends a node n to L. Efficiency: O(1).
proc prepend[T](L: var DoublyLinkedList[T]; value: T)
prepends a value to L. Efficiency: O(1).
proc remove[T](L: var DoublyLinkedList[T]; n: DoublyLinkedNode[T])
removes n from L. Efficiency: O(1).
proc append[T](L: var SinglyLinkedRing[T]; n: SinglyLinkedNode[T])
appends a node n to L. Efficiency: O(1).
proc append[T](L: var SinglyLinkedRing[T]; value: T)
appends a value to L. Efficiency: O(1).
proc prepend[T](L: var SinglyLinkedRing[T]; n: SinglyLinkedNode[T])
prepends a node n to L. Efficiency: O(1).
proc prepend[T](L: var SinglyLinkedRing[T]; value: T)
prepends a value to L. Efficiency: O(1).
proc append[T](L: var DoublyLinkedRing[T]; n: DoublyLinkedNode[T])
appends a node n to L. Efficiency: O(1).
proc append[T](L: var DoublyLinkedRing[T]; value: T)
appends a value to L. Efficiency: O(1).
proc prepend[T](L: var DoublyLinkedRing[T]; n: DoublyLinkedNode[T])
prepends a node n to L. Efficiency: O(1).
proc prepend[T](L: var DoublyLinkedRing[T]; value: T)
prepends a value to L. Efficiency: O(1).
proc remove[T](L: var DoublyLinkedRing[T]; n: DoublyLinkedNode[T])
removes n from L. Efficiency: O(1).

Iterators

iterator items[T](L: DoublyLinkedList[T]): T
yields every value of L.
iterator items[T](L: SinglyLinkedList[T]): T
yields every value of L.
iterator items[T](L: SinglyLinkedRing[T]): T
yields every value of L.
iterator items[T](L: DoublyLinkedRing[T]): T
yields every value of L.
iterator mitems[T](L: var DoublyLinkedList[T]): var T
yields every value of L so that you can modify it.
iterator mitems[T](L: var SinglyLinkedList[T]): var T
yields every value of L so that you can modify it.
iterator mitems[T](L: var SinglyLinkedRing[T]): var T
yields every value of L so that you can modify it.
iterator mitems[T](L: var DoublyLinkedRing[T]): var T
yields every value of L so that you can modify it.
iterator nodes[T](L: SinglyLinkedList[T]): SinglyLinkedNode[T]
iterates over every node of x. Removing the current node from the list during traversal is supported.
iterator nodes[T](L: DoublyLinkedList[T]): DoublyLinkedNode[T]
iterates over every node of x. Removing the current node from the list during traversal is supported.
iterator nodes[T](L: SinglyLinkedRing[T]): SinglyLinkedNode[T]
iterates over every node of x. Removing the current node from the list during traversal is supported.
iterator nodes[T](L: DoublyLinkedRing[T]): DoublyLinkedNode[T]
iterates over every node of x. Removing the current node from the list during traversal is supported.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/lists.html