如何将列表传递给函数?

huangapple go评论72阅读模式
英文:

go collection list, how to pass list in to function?

问题

package main
// 给定两个表示非负数字的链表。数字以相反的顺序存储,并且每个节点包含一个数字。将两个数字相加并将其作为链表返回。

// 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
// 输出:7 -> 0 -> 8
import (
	"container/list"
	"fmt"
)

func main() {
	l1 := list.New()
	l1.PushBack(4)
	l1.PushBack(5)
	l1.PushBack(2)

	l2 := list.New()
	l2.PushBack(7)
	l2.PushBack(3)

	l3 := list.New()
	l3 = addTwoNumbers(l1, l2)

	for e := l3.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
func addTwoNumbers(l1 list, l2 list) (l3 list) {
	carry := 0
	l4 := list.New()
	e1 := l1.Front()
	e2 := l2.Front()
	for {
		sum := carry
		if l1 != nil {
			sum += l1.Value
			l1 = l1.Next()
		}
		if l2 != nil {
			sum += l2.Value
			l2 = l2.Next()
		}
		l4.PushBack(sum % 10)
		carry = sum / 10

		if l1 == nil && l2 == nil && carry == 0 {
			break
		}
	}
	return l4
}

你遇到的错误是语法错误。在Go语言中,变量声明和赋值需要使用冒号(:)而不是等号(=)。你需要将int carry = 0改为carry := 0,将int sum = carry改为sum := carry。此外,你还需要将函数参数的类型从list更改为*list。修改后的代码如上所示。

英文:
package main
// You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8
import (
	"container/list"
	"fmt"
)

func main() {
	l1 := list.New()
	l1.PushBack(4)
	l1.PushBack(5)
	l1.PushBack(2)

	l2 := list.New()
	l2.PushBack(7)
	l2.PushBack(3)

	l3 := list.New()
	l3 = addTwoNumbers(l1, l2)

	for e := l3.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
func addTwoNumbers(l1 list, l2 list) (l3 list) {
	int carry = 0
	l4 := list.New()
	e1 := l1.Front()
	e2 := l2.Front()
	for ;; {
		int sum = carry
		if l1 != nil {
			sum += l1.Value
			l1 = l1.Next()
		}
		if l2 != nil {
			sum += l2.Value
			l2 = l2.Next()
		}
		l4.PushBack(sum % 10)
		carry = sum / 10

		if l1== nil && l2 == nil && carry == 0{
			break
		}
	}
	return l4
}

I got error:

./addTwoNumbers.go:26: syntax error: unexpected name, expecting semicolon or newline or }
./addTwoNumbers.go:31: syntax error: unexpected name, expecting semicolon or newline or }

But I dont know how to fix it. need help. thanks

答案1

得分: 2

你的代码中有一堆错误。

  • 在导入列表时,list是模块名而不是类型。应该使用list.List
  • 在Go语言中,类型是变量名的“后缀”。大多数情况下,你不需要声明它。
  • 在遍历列表时,使用element而不是listElement具有Value属性。

这是一个可工作的版本:

package main

// 给定两个表示非负数的链表,数字以相反的顺序存储,每个节点包含一个数字。将两个数字相加并将其作为链表返回。

// 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
// 输出:7 -> 0 -> 8
import (
	"container/list"
	"fmt"
)

func main() {
	l1 := list.New()
	l1.PushBack(4)
	l1.PushBack(5)
	l1.PushBack(2)

	l2 := list.New()
	l2.PushBack(7)
	l2.PushBack(3)

	l3 := list.New()
	l3 = addTwoNumbers(l1, l2)

	for e := l3.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}

func addTwoNumbers(l1 *list.List, l2 *list.List) (l3 *list.List) {
	carry := 0
	l4 := list.New()
	e1 := l1.Front()
	e2 := l2.Front()
	for {
		sum := carry
		if e1 != nil {
			sum += e1.Value.(int)
			e1 = e1.Next()
		}
		if e2 != nil {
			sum += e2.Value.(int)
			e2 = e2.Next()
		}
		l4.PushBack(sum % 10)
		carry = sum / 10

		if e1 == nil && e2 == nil && carry == 0 {
			break
		}
	}
	return l4
}

你可以在这里找到一个可运行的版本:http://play.golang.org/p/yys-OcxZz2

英文:

There are a bunch of errors in your code.

  • when importing list, list is the module name not the type. use list.List
  • in go the type is after the name of the variable. most of the time you don't need to declare it
  • when iterating over the list use element not list. Element has Value

Here is a working version

http://play.golang.org/p/yys-OcxZz2

package main
// You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8
import (
"container/list"
"fmt"
)
func main() {
l1 := list.New()
l1.PushBack(4)
l1.PushBack(5)
l1.PushBack(2)
l2 := list.New()
l2.PushBack(7)
l2.PushBack(3)
l3 := list.New()
l3 = addTwoNumbers(l1, l2)
for e := l3.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
}
func addTwoNumbers(l1 *list.List, l2 *list.List) (l3 *list.List) {
carry := 0
l4 := list.New()
e1 := l1.Front()
e2 := l2.Front()
for {
sum := carry
if e1 != nil {
sum += e1.Value.(int)
e1 = e1.Next()
}
if e2 != nil {
sum += e2.Value.(int)
e2 = e2.Next()
}
l4.PushBack(sum % 10)
carry = sum / 10
if e1 == nil && e2 == nil && carry == 0 {
break
}
}
return l4
}

huangapple
  • 本文由 发表于 2015年1月15日 10:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/27955787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定