Creating LinkedList with Go - go

I'm new to golang and exploring the language.
I am creating a linked list, but it seems to go out of scope and does not store variables correctly.
When I print the list using this code, it prints blank, when aa, bb is expected.
package main
import "fmt"
type Node struct {
data string
nextNode *Node
}
type MyLinkedList struct {
head *Node
}
func (ll MyLinkedList) pushFront(data string) *Node {
node := Node{data, nil}
if ll.head == nil {
ll.head = &node
return ll.head
}
node.nextNode = ll.head
ll.head = &node
return ll.head
}
func print(ll MyLinkedList) {
currentNode := ll.head
for currentNode != nil {
fmt.Print(currentNode.data + " ")
currentNode = currentNode.nextNode
}
}
func main() {
var m = MyLinkedList{}
m.pushFront("aa")
m.pushFront("bb")
print(m)
}

I modified your code to make it work.
import "fmt"
type Node struct {
data string
nextNode *Node
}
type MyLinkedList struct {
head *Node
}
func (ll *MyLinkedList) pushFront(data string) {
node := &Node{data, nil}
if ll.head == nil {
ll.head = node
return
}
node.nextNode = ll.head
ll.head = node
}
func print(ll *MyLinkedList) {
currentNode := ll.head
for currentNode != nil {
fmt.Print(currentNode.data + " ")
currentNode = currentNode.nextNode
}
}
func main() {
var m = &MyLinkedList{}
m.pushFront("aa")
m.pushFront("bb")
print(m)
}
So in your code your just passing the struct by value
To modify struct in runtime you generally use a struct pointer
(Golang: I have a map of structs. Why can't I directly modify a field in a struct value?)

The pushFront function uses the MyLinkedList struct value instead of reference. So if you use value of a variable in a function, it's copy is created in stack. At the end of the function the variables are removed from stack.
In the first calling of pushFront function the assigning a node to ll.head is not your original MyLinklist struct it is just a copy of your struct.
In the second calling of the pushFront function, againt a copy of your MyLinklist struct will be copied to stack. So its head will be again nil. Then at the end of the function it will be removed from stack. Your original MyLinklist struct which is 'm' will not be changed. So, since it does not change it will print nothing.
The solution is that, you should pass your reference of your MyLinklist struct to pushFront function. It is something like following:
func (ll *MyLinkedList) pushFront(data string) *Node {
node := Node{data, nil}
if ll.head == nil {
ll.head = &node
return ll.head
}
node.nextNode = ll.head
ll.head = &node
return ll.head
}

Related

Searching and Returning a node using recursion in minheap binary tree

So I am trying to retrieve a node in a minheap tree by index. The way that it would be called is that I would intiatate a empty MinHeapNode struct and pass by its value via &node so that between recursive function calls, if a match was found it would then return. However it seems that even given a found result the newly assigned empty node would be overwritten by another recursive call that has an empty version of that node. I'm still getting used to the idea of pointers and addresses so I believed that passing the values address would get around this since it would be calling the same value at the same address between calls. But apparently this is something is not correct.
type MinHeapNode struct {
Parent *MinHeapNode
Left *MinHeapNode
Right *MinHeapNode
Value int
Index int
}
func (MHN *MinHeapNode) Insert(value int) {
if !MHN.hasLeftChild() {
MHN.Left = &MinHeapNode{Parent: MHN, Value: value}
return
}
if !MHN.hasRightChild() {
MHN.Right = &MinHeapNode{Parent: MHN, Value: value}
return
}
if MHN.hasLeftChild(){
MHN.Left.Insert(value)
return
}
if MHN.hasRightChild(){
MHN.Right.Insert(value)
return
}
}
func (MHN *MinHeapNode) setIndex(count *int){
index := *count
*count = *count +1
MHN.Index = index
if MHN.hasLeftChild(){
MHN.Left.setIndex(count)
}
if MHN.hasRightChild(){
MHN.Right.setIndex(count)
}
}
func (MHN *MinHeapNode) getIndex(index int, node *MinHeapNode){
if MHN == nil{
return
}
if MHN.Index == index{
node = MHN
return
}
MHN.Left.getIndex(index, node)
MHN.Right.getIndex(index,node)
}
}
type MinHeapTree struct {
Root MinHeapNode
Size int
}
func (MHT *MinHeapTree) getIndex(index int)(*MinHeapNode, error){
if MHT.Size < index +1 {
err := fmt.Errorf("index exceeds tree size")
return nil, err
}
var node MinHeapNode
MHT.Root.getIndex(index, &node)
return &node, nil
}
The issue you are facing appears to be with the statement node = MHN in getIndex (but as your code is incomplete I cannot confirm if this is the only issue).
node = MHN will update the value of node (a parameter, so passed by value and, its scope is the function body). This has no impact on the value of the MinHeapNode that node pointed to at the start of the function. To correct this use *node = *MHN.
This can be demonstrated with a simple program (playground)
type MinHeapNode struct {
Test string
}
func getIndexBad(node *MinHeapNode) {
newNode := MinHeapNode{Test: "Blah"}
node = &newNode
}
func getIndexGood(node *MinHeapNode) {
newNode := MinHeapNode{Test: "Blah"}
*node = newNode
}
func main() {
n := MinHeapNode{}
fmt.Println(n)
getIndexBad(&n)
fmt.Println(n)
getIndexGood(&n)
fmt.Println(n)
}
The output demonstrates that the "bad" function does not update the passed in node:
{}
{}
{Blah}

Removing an item in a Linked List with Go

I want to remove a Node from a linked list in Go, and I have this struct and these methods:
type Node struct {
Next *Node
Val int
}
func (n *Node) Append(val int) {
end := &Node{Val: val}
here := n
for here.Next != nil {
here = here.Next
}
here.Next = end
}
func Remove(n *Node, val int) *Node {
head := n
for head.Next != nil {
if head.Next.Val == val {
head.Next = head.Next.Next
return head
}
head = head.Next
}
return head
}
func NewNode(val int) *Node {
return &Node{Val: val}
}
I want to remove an item like this:
n := NewNode(1)
n.Append(2)
n.Append(3)
n.Append(4)
n.Append(5)
m := Remove(n, 3)
for m != nil {
fmt.Println(n.Val)
m = m.Next
}
The items that get printed out are 3 and 5, not 1,2,4and5`. I re-implemented this code in Python and got the expected answer. What is going on in Go? I have a feeling it has to do something with pointers.
You lose the head from returning a node you use to traverse. Also you are printing out the wrong object
type Node struct {
Next *Node
Val int
}
func (n *Node) Append(val int) {
end := &Node{Val: val}
here := n
for here.Next != nil {
here = here.Next
}
here.Next = end
}
func Remove(n *Node, val int) *Node {
traverser := n
for traverser.Next != nil {
if traverser.Next.Val == val {
traverser.Next = traverser.Next.Next
return n
}
traverser = traverser.Next
}
return n
}
func NewNode(val int) *Node {
return &Node{Val: val}
}
func main() {
n := NewNode(1)
n.Append(2)
n.Append(3)
n.Append(4)
n.Append(5)
m := Remove(n, 3)
for m != nil {
fmt.Println(m.Val)
m = m.Next
}
}

How to create a binary tree use struct in golang?

I want to create a binary tree and initialize the tree use golang.
And the codes like these:
package Tree
import "fmt"
type TreeNode struct {
Left *TreeNode
Right *TreeNode
Value int
}
func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
if tree == nil {
tree = node
}
if node.Value > tree.Value {
InsertNodeToTree(tree.Right, node)
}
if node.Value < tree.Value {
InsertNodeToTree(tree.Left, node)
}
}
func InitTree(values ...int) (root *TreeNode) {
rootNode := TreeNode{Value: values[0]}
for _, value := range values {
node := TreeNode{Value:value}
InsertNodeToTree(&rootNode, &node)
}
return &rootNode
}
func main() {
treeNode := InitTree(5, 4, 6, 8, 9, 7, 1, 3, 2)
fmt.Println(treeNode)
}
Why the tree's left and right are nil?
I pass the reference of the tree node, why not work?
In C/C++ programming language, you can use TreeNode *&tree.
But in golang programming language, you can not use *&.
tree is just a copy of the pointer, so you can't point the value to another TreeNode.
I modified your program, and it can run successfully now.
Maybe these codes you need:
package Tree
type TreeNode struct {
Left *TreeNode
Right *TreeNode
Value int
}
var DefaultValue int = -1024
func InsertNodeToTree(tree *TreeNode, node *TreeNode)(){
if tree == nil {
return
}
if tree.Value == DefaultValue {
tree.Value = node.Value
return
}
if node.Value > tree.Value {
if tree.Right == nil {
tree.Right = &TreeNode{Value: DefaultValue}
}
InsertNodeToTree(tree.Right, node)
}
if node.Value < tree.Value {
if tree.Left == nil {
tree.Left = &TreeNode{Value: DefaultValue}
}
InsertNodeToTree(tree.Left, node)
}
}
func InitTree(values ...int) (root *TreeNode) {
rootNode := TreeNode{Value: DefaultValue, Right: nil, Left: nil}
for _, value := range values {
node := TreeNode{Value:value}
InsertNodeToTree(&rootNode, &node)
}
return &rootNode
}
tree is only a copy of the pointer. Assigning to the variable is useless. Instead, you need to assign to an already existing node. For example:
https://play.golang.org/p/Agzby-Yinq
func InsertNodeToTree(tree *TreeNode, node *TreeNode) {
if tree == nil {
panic("cannot insert into nil root")
}
if node.Value > tree.Value {
if tree.Right == nil {
tree.Right = node
} else {
InsertNodeToTree(tree.Right, node)
}
}
if node.Value < tree.Value {
if tree.Left == nil {
tree.Left = node
} else {
InsertNodeToTree(tree.Left, node)
}
}
}

undefined attributes in an slice of node structs

er, I am trying to learn go by implementing a random graph. I get an error on n.value undefined (type int has no field or method value), and n.neigbours undefined (type int has no field or method neigbours). I can not understand that compilation error as i create a new slice of nodesnr size of empty nodes in the g.nodes = make([]node, g.nodesnr). What is the problem?
package main
import (
"fmt"
//"math/rand"
)
type node struct {
value int
neigbours []int
}
type edge struct {
source int
sink int
}
type graph struct {
nodesnr, edgesnr int
nodes []node
edges chan edge
}
func main() {
randomGraph()
}
func input(tname string) (number int) {
fmt.Println("input a number of " + tname)
fmt.Scan(&number)
return
}
func randomGraph() (g graph) {
g = graph{nodesnr: input("nodes"), edgesnr: input("edges")}
g.addNodes()
for i := 0; i < g.nodesnr; i++ {
fmt.Println(g.nodes[i].value)
}
//g.addEdges()
return
}
func (g *graph) addNodes() {
g.nodes = make([]node, g.nodesnr)
for n := range g.nodes {
n.value = 2
n.neigbours = nil
return
}
}
func (g *graph) addEdges() {
g.edges = make(chan edge)
for i := 0; i < g.edgesnr; i++ {
//g.newEdge()
return
}
}
/*
func (g* graph) newEdge(){
e := new(edge)
e.source, e.sink = rand.Intn(g.nodesnr), rand.Intn(g.nodesnr)
g.edges <-e*
//g.addEdge()
}
*/
func (g *graph) edgeCheck(ep *edge) string {
if ep.source == ep.sink {
return "self"
}
//if(g.neigbourCheck(g.nodes[ep.source].neigbours, ep.sink) OR g.neigbourCheck(g.nodes[ep.sink].neigbours, ep.source){
// return "present"
return "empty"
}
func (g *graph) neigbourCheck(neigbours []node, node int) bool {
for neigbour := range neigbours {
if node == neigbour {
return true
}
}
return false
}
func (g *graph) addEdge() {
e := <-g.edges
switch etype := g.edgeCheck(&e); etype {
case "present":
fallthrough
case "self":
fmt.Println("self")
//go g.newEdge()
case "empty":
//g.nodes[e.source] = append(g.nodes[e.source], e.sink),
//g.nodes[e.sink] = append(g.nodes[e.sink], e.source)
fmt.Println("empty")
default:
fmt.Println("something went wrong")
}
}
Playground
Your error lies on line 47
for n := range g.nodes
When iterating over a slice, when using only one value, that value (n) will be set to the index, which is of type int. What you need to do is to change the line to:
for _, n := range g.nodes
This means that you discard the index but put the value in n instead.
Edit
n will be a copy of the value which means any changes made to n will not affect the node in the slice. To edit the node in the slice, you should actually get the index instead of the value:
for i := range g.nodes {
g.nodes[i].value = 2
g.nodes[i].neigbours = nil
return
}

Compilation error in go program

here is the code and i use gccgo for compilation. this for a graph based organizer. I don't need advise on graph algorithms.
package element
import (
"fmt"
"strings"
"io"
"strconv"
)
type Node struct {
id int
name string
props map[string]string
links map[string][]*Node
}
var names = make(map[string]int , 8)
var nodes = make(map[string][]*Node , 8)
//========functions================
func new_node(Id int) *Node {
return &Node( Id, " ", nil, nil)
}
func getNode_byId(nodes []*Node, id int) *Node {
for _, node := range nodes{
if node.id == id {
return node
}
}
return nil
}
func addNode(store string, node *Node) {
nodes[store] = append(nodes[store], node)
}
func addLinkToNode(node, link *Node, property string) {
node.links[property] = append(node.links[property], link)
}
func nodeFromString(str string, typ string) {
lines := strings.Split(str, "\n")
lcount := len(lines)
if lines[0] == "[begin]" && lines[lcount] == "[end]" {
fmt.Println("common dude! something wrong with ur string")
return
}
fields := strings.Fields(lines[1])
id , _ := strconv.Atoi(fields[1])
nod := getNode_byId(nodes[typ], id )
if nod == nil { nod = new_node(id) }
addNode(typ, nod)
nod.name = typ
lines = lines[2:]
ind :=0
for index, line := range lines {
fields := strings.Fields(line)
if field := fields[0]; field[0] != '-' {
ind = index
break
}
nod.props[fields[0]] = fields[1]
}
lines = lines[ind:]
for index, line := range lines {
if line[0]!= '+' {
ind = index
break
}
pivot := strings.Index(line, " ")
field := line[0:pivot]
fields := strings.Split(line[pivot:], ",")
for _, value := range fields {
id, _ := strconv.Atoi(strings.TrimSpace(value))
var link *Node = getNode_byId(nodes[typ], id)
if link == nil { link = new_node(id) }
addNode(typ, link)
append(nod.links[field], link )
}
}
}
func equal_byId( nodeA, nodeB Node) bool {
return (nodeA.id == nodeB.id)
}
func equal_byProp( nodeA, nodeB Node, property string) bool {
return (nodeA.props[property] == nodeB.props[property])
}
//========methods on node==========
func (node Node) IsEqual_byId( comparand Node ) bool {
return equal_byId(node, comparand)
}
func (node Node) IsEqual_byProp( comparand Node, property string ) bool {
return equal_byProp(node, comparand, property)
}
func (node *Node) addLink (property string, link *Node){
addLinkToNode( node, link, property)
}
//===================
func main() {
fmt.Println("hello world")
}
and this is the error I got, I tried my best but I cannot resolve.
$ gccgo elements.go
elements.go:23:19: error: expected ‘)’
elements.go:23:34: error: expected ‘;’ or ‘}’ or newline
elements.go:23:2: error: too many values in return statement
elements.go:91:4: error: value computed is not used
I do not understand where I need to use the semi-colon and why.
The problem, I think, may be in func new_node:
return &Node( Id, " ", nil, nil)
Should be
return &Node{Id, " ", nil, nil}
See http://golang.org/ref/spec#Composite_literals
Also, I have a feeling that, in func nodeFromString (line 93-ish):
append(nod.links[field], link)
Should be:
nod.links[field] = append(nod.links[field], link)
Otherwise you'll get an error.

Resources