NimProgrammingBook icon indicating copy to clipboard operation
NimProgrammingBook copied to clipboard

The tail insert of linked object

Open geohuz opened this issue 2 years ago • 1 comments

In the chapter of "Reference and Pointers", I'm wondering how can I code the "tail insert" instead of the head insert of linked object? The current example insert item as reverse order.

type
  Friend = ref object
    name: string
    next: Friend  

var
  f: Friend           
  node: Friend
  n: string 

new(f)
node = f
while true:
  write(stdout, "Name of friend: ")
  n = readline(stdin)
  if n=="" or n=="quit":
    break
  node.next = Friend(name: n)
  node = node.next

while f!= nil:
  echo f.name
  f = f.next        

geohuz avatar Mar 12 '23 05:03 geohuz