From 9b354ee72bad2adc5f09caa80358859cbe5c2618 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 6 Aug 2017 17:24:19 +0200 Subject: push on tail --- go/linkedlist.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'go') diff --git a/go/linkedlist.go b/go/linkedlist.go index 7f556e6..31aa77c 100644 --- a/go/linkedlist.go +++ b/go/linkedlist.go @@ -7,19 +7,7 @@ type node struct { next *node } -func main() { - lista := new(node) - lista.next = nil - - lista.carica(5) - - for lista != nil { - fmt.Printf("%d ",lista.v) - lista = lista.next - } -} - -func (head *node) carica(N int) { +func (head *node) load(N int) { if N < 1 { return } @@ -32,3 +20,26 @@ func (head *node) carica(N int) { fmt.Scanf("%d", &head.v) head.next = nil } + +func (head *node) pushTail(val int) { + for head.next != nil { + head = head.next + } + + head.next = new(node) + head = head.next + head.v = val + head.next = nil +} + +func main() { + lista := new(node) + lista.next = nil + + lista.load(5) + lista.pushTail(32) + for lista != nil { + fmt.Printf("%d ",lista.v) + lista = lista.next + } +} -- cgit v1.2.3-18-g5258