Below is the code:
package collection
type List struct {
head *Node
tail *Node
}
func (l *List) First() *Node {
return l.head
}
func (l *List) Push(value int) {
node := &Node{value: value}
if l.head == nil { // list is empty
l.head = node
} else {
l.tail.next = node
}
l.tail = node
}
func (l *List) String() string {
var list string
n := l.First()
for n != nil {
list = list + string(n.Value()) + " "
n = n.Next()
}
return list
}
type Node struct {
value int
next *Node
}
func (n *Node) Next() *Node {
return n.next
}
func (n *Node) Value() int {
return n.value
}
On debugging, elements get pushed successfully
But for list = list + string(n.Value()) + " "
, this is the debug output: list: " "
package main
import (
"fmt"
"github.com/myhub/cs61a/collection"
)
func main() {
l := &collection.List{}
l.Push(1)
l.Push(2)
l.Push(3)
fmt.Println(l)
}
1) Why list = list + string(n.Value()) + " "
does not concat integers?
2) How to support Node
for member value
of any type?