-3

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 1) In order to convert an int to a string, you need to use [`strconv.Itoa`](https://pkg.go.dev/strconv?tab=doc#Itoa). There's a [question covering that](https://stackoverflow.com/questions/10105935/how-to-convert-an-int-value-to-string-in-go). 2) That's harder in Go as it doesn't have generics, so you would need to use an `interface{}` (empty interface) to make it work. You would also have make many type assertions. A better way would be using normal slices as they can support many types with type safety – xarantolus Jun 07 '20 at 06:56
  • Try `strconv.Itoa(n.Value())` instead – Marc Jun 07 '20 at 06:56
  • @xarantolus Why `string(n.Value())` is not giving compile time error, when it cannot do type conversion? – overexchange Jun 07 '20 at 06:57
  • 3
    It does do a type conversion, they're just character values 1, 2, and 3 which are non-printable characters. – Marc Jun 07 '20 at 07:00
  • @Marc Got you... character with ascii value(or UTF-8 value may be) 1 is non-printable character, yes – overexchange Jun 07 '20 at 07:04
  • @Marc `strconv.Itoa()` does something different from type conversion? – overexchange Jun 07 '20 at 07:09
  • Yes, as spelled out in [the docs](https://golang.org/pkg/strconv/#example_Itoa), it returns the string representation of the integer in base 10. – Marc Jun 07 '20 at 07:13
  • You can check the [code](https://golang.org/src/strconv/itoa.go) also to find out. – Eklavya Jun 07 '20 at 07:15

1 Answers1

1

Use strconv.Itoa() to convert int into string.

list = list + strconv.Itoa(n.Value()) + " "

In a plain conversion, the value is interpreted as a Unicode code point, and the resulting string will contain the character represented by that code point, encoded in UTF-8.

s := string(97) // s == "a"

and for your case 1,2,3 are all non-printable characters

Eklavya
  • 17,618
  • 4
  • 28
  • 57