How to resolve the algorithm Doubly-linked list/Definition step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Doubly-linked list/Definition step by step in the Nim programming language

Table of Contents

Problem Statement

Define the data structure for a complete Doubly Linked List.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Doubly-linked list/Definition step by step in the Nim programming language

Source code in the nim programming language

type
  List[T] = object
    head, tail: Node[T]

  Node[T] = ref TNode[T]

  TNode[T] = object
    next, prev: Node[T]
    data: T

proc initList[T](): List[T] = discard

proc newNode[T](data: T): Node[T] =
  new(result)
  result.data = data

proc prepend[T](l: var List[T], n: Node[T]) =
  n.next = l.head
  if l.head != nil: l.head.prev = n
  l.head = n
  if l.tail == nil: l.tail = n

proc append[T](l: var List[T], n: Node[T]) =
  n.next = nil
  n.prev = l.tail
  if l.tail != nil:
    l.tail.next = n
  l.tail = n
  if l.head == nil:
    l.head = n

proc insertAfter[T](l: var List[T], r, n: Node[T]) =
  n.prev = r
  n.next = r.next
  n.next.prev = n
  r.next = n
  if r == l.tail: l.tail = n

proc remove[T](l: var List[T], n: Node[T]) =
  if n == l.tail: l.tail = n.prev
  if n == l.head: l.head = n.next
  if n.next != nil: n.next.prev = n.prev
  if n.prev != nil: n.prev.next = n.next

proc `$`[T](l: var List[T]): string =
  result = ""
  var n = l.head
  while n != nil:
    if result.len > 0: result.add(" -> ")
    result.add($n.data)
    n = n.next

var l = initList[int]()
var n = newNode(12)
var m = newNode(13)
var i = newNode(14)
var j = newNode(15)
l.append(n)
l.prepend(m)
l.insertAfter(m, i)
l.prepend(j)
l.remove(m)
echo l

var l2 = initList[string]()
l2.prepend newNode("hello")
l2.append newNode("world")
echo l2


  

You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the Oforth programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Best shuffle step by step in the Julia programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the BASIC programming language
You may also check:How to resolve the algorithm Loops/For step by step in the SNOBOL4 programming language