diff options
author | Santo Cariotti <sancn@live.com> | 2017-08-06 17:24:19 +0200 |
---|---|---|
committer | Santo Cariotti <sancn@live.com> | 2017-08-06 17:24:19 +0200 |
commit | 9b354ee72bad2adc5f09caa80358859cbe5c2618 (patch) | |
tree | c9d6000d7422b33a0605b5dbc80688f8227e836a | |
parent | 46139898bccc2268d348fc5744594fac4ea6c34c (diff) |
push on tail
-rw-r--r-- | go/linkedlist.go | 37 |
1 files changed, 24 insertions, 13 deletions
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 + } +} |