- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试使用链表实现多项式的加法。该程序成功地添加了幂 0 系数,但在第一次遍历后它出现了困惑。这是我到目前为止编写的代码。在初始化 temp1!= nil 之后,循环遍历 else 但当权力不同时不进入 if 循环并进入 panic 状态
package main
import (
"fmt"
)
type Node struct {
coefficient int
power int
next *Node
}
type list struct {
head *Node
count int
}
func main() {
list1 := &list{}
list1.addVariable(5, 2)
list1.addVariable(4, 1)
list1.addVariable(3, 0)
list1.print()
list2 := &list{}
list2.addVariable(4, 1)
list2.addVariable(2, 0)
list2.print()
list4 := addLists(list1, list2)
list4.print()
}
func (lst *list) print() {
temp := lst.head
for temp != nil {
fmt.Print(temp.coefficient, "x", "^", temp.power, "+")
temp = temp.next
}
fmt.Println("\n")
}
func (lst *list) addVariable(coefficient int, power int) {
lst.head = &Node{coefficient, power, lst.head}
lst.count++
}
func addLists(list1 *list, list2 *list) *list {
list3 := &list{}
temp1 := list1.head
temp2 := list2.head
fmt.Println("reached") // for debugging purposes
if list1.count > list2.count {
fmt.Println("\nreached 2") // for debugging purposes
for temp1 != nil {
fmt.Println("reached3") // for debugging purposes
if temp1.power != temp2.power {
fmt.Println("reached4") // for debugging purposes
list3.normalAdd(temp1, temp2)
temp1 = temp1.next
} else {
fmt.Println("reached5") // for debugging purposes
node4 := add(temp1, temp2)
list3.exlusiveAdd(node4)
temp1 = temp1.next
temp2 = temp2.next
}
}
}
return list3
}
func (lst *list) normalAdd(node6 *Node, node7 *Node) {
node6.next = lst.head
lst.head = node6
node7.next = lst.head
lst.head = node7
lst.count += 2
}
func (lst *list) exlusiveAdd(node5 *Node) {
node5.next = lst.head
lst.head = node5
lst.count++
}
func add(node1, node2 *Node) *Node {
node3 := &Node{}
node3.coefficient = node1.coefficient + node2.coefficient
node3.power = node1.power
return node3
}
程序运行时的输出:
3x^0+4x^1+5x^2+
2x^0+4x^1+
reached
reached 2
reached3
reached5
reached3
reached5
reached3
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xd48e6]
goroutine 1 [running]:
main.addLists(0x41c7a8, 0x41c7a0, 0x40c120, 0x1d)
/tmp/sandbox520212269/main.go:56 +0x186
main.main()
/tmp/sandbox520212269/main.go:28 +0x220
Playground here
更新:感谢您的解决方案,但我已经成功编写了上述问题的代码。这里是。 包主
import (
"fmt"
)
type Node struct {
coefficient int
power int
next *Node
}
type list struct {
head *Node
count int
}
func main() {
list1 := &list{}
list1.addVariable(5, 2)
list1.addVariable(4, 1)
list1.addVariable(3, 0)
list1.print()
list2 := &list{}
list2.addVariable(4, 1)
list2.addVariable(2, 0)
list2.print()
poly := addPoly(list1, list2)
poly.print()
}
func (lst *list) print() {
temp := lst.head
for temp != nil {
fmt.Print(temp.coefficient, "x", "^", temp.power, "+")
temp = temp.next
}
fmt.Println("\n")
}
func (lst *list) addVariable(coefficient int, power int) {
lst.head = &Node{coefficient, power, lst.head}
lst.count++
}
func addPoly(list_1 *list, list_2 *list) *list {
list_3 := &list{}
temp_1 := list_1.head
temp_2 := list_2.head
if list_1.count > list_2.count {
for temp_1 != nil && temp_2 != nil {
if temp_1.power == temp_2.power {
new_coefficient := temp_1.coefficient + temp_2.coefficient
new_power := temp_1.power
list_3.addVariable(new_coefficient, new_power)
temp_1 = temp_1.next
temp_2 = temp_2.next
} else if temp_1.power != temp_2.power {
list_3.addVariable(temp_1.coefficient, temp_1.power)
list_3.addVariable(temp_2.coefficient, temp_2.power)
temp_1 = temp_1.next
temp_2 = temp_2.next
}
}
}
for temp_1 != nil {
list_3.addVariable(temp_1.coefficient, temp_1.power)
temp_1 = temp_1.next
}
return list_3
}
最佳答案
错误发生是因为您试图在下面的代码中从 nil
对象访问 .power
属性。 temp1
和 temp2
都是 nil
。
if temp1.power != temp2.power {
// ...
}
list
结构上的.head
属性具有指针数据类型。指针数据类型的零值是nil
。在您的代码中,list1
和 list2
的 .head
属性都具有 nil
值。
您需要做的:在创建 list1
和 list2
期间明确初始化 .head
属性的值。然后 panic 错误将消失,您的代码将正常工作。
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground :https://play.golang.org/p/vPK3pYKht3E
关于go - panic : runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x8 pc=0x48be5c] goroutine 1 [running]:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53424273/
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
有谁知道区别吗? 最佳答案 如果您想玩可移植的可执行文件,就没有办法绕过 the specs 的副本。 . 已经有一段时间了,但如果内存正确地为我服务:IT 和 IAT 是相同的,除了 IAT 在解析
我正在尝试在 ec2-instance 上安装 Hadoop-2.6.0。 我下载并安装了 Hadoop。我还设置了环境变量。尝试启动 hdfs 服务时出现以下错误。 [ec2-user@ip-10-
我写了一个示例程序。如果我打印 pa 和 pb 的地址都是不同的。你能告诉我为什么会这样吗? #include using namespace std; class A { int x; }; cla
*&x 是否总是等于 x?如果不是,什么时候不是? &*x 是否总是等于 x?如果不是,什么时候不是? 我的猜测是 (1) 总是正确的,但 (2) 并不总是正确的,因为 x 可能并不总是一个指针,所以
我有一个 Address 类,它是一个非常简单的元数据容器。 (在底部。) 我有一个 Address 对象数组,它不是可选的 - 它肯定存在。我还有一个类维护这些引用的可选列表,如下所示: publi
我在理解 wsdl 中 soap:address 和 http:address 标签之间的区别时遇到了一些问题。它可以互换吗?我可以使用 soap:address 代替 http:address 吗?
关于AT (...) ld 的指令, this source状态: AT ( ldadr ) The expression ldadr that follows the AT keyword spec
我正在使用 Tensorflow 的 C API 在并行模拟中进行推理。因为我想要 AVX 支持,所以我从源代码编译了 Tensorflow。我链接它并使用 wmake 编译所有内容。 现在,如果我开
就像标题一样,我是cxf的新手。只是想知道 soap:address 和发布时在应用程序上下文中的 jaxws:endpoint 中指定的地址有什么区别? 此外,在jaxws:endpoint中,地址
#include #include using namespace std; class myexception: public exception { virtual const char*
C/C++ 应用程序抛出该错误,如何开始调试(比添加打印语句更好的主意)? 最佳答案 第二个地址是不是一个非常小的数字,比如 0x00000001 或 0x00000000?如果是这样,您可能只是忘记
如果我没记错的话,几天前它曾经显示“localhost”。我不确定是什么改变了 server.address().address 返回双冒号 (::) 。我在这里读到,如果它可用,它会返回一个 IPv
我现在正在使用 MPI 练习简单的并行编程。该代码旨在通过随机生成 N*N 矩阵并使用简单的邻域加权平均滤波器来模拟图像处理,而不处理第一行和最后一行和列。我在编译时没有出错,但在运行时出现了一些我无
这个问题在这里已经有了答案: How to retrieve range.address which is longer than 255 character? (2 个回答) 5年前关闭。 觉得很奇
当我尝试启动 Apache2 时收到以下消息: * Restarting web server apache2
我正在阅读一些有关指针和结构的内容,但我就是不明白:微 Controller 的头文件中有这样的内容: #define NVIC_BASE (SCS_BASE + 0x0100) /*ICER[0]
我有 alertmanager 作为 docker 容器在两台不同的主机上运行,并且两者都应该作为集群运行。两台机器都在同一个 vpc 内,并与私有(private) IP 地址通信。 我需要知道
在 Organization schema两者都有属性 address和 location . 什么时候应该使用每一个的真实世界例子? 地点 事件、组织或行动的地点。 地址 项目的物理地址。 最佳答案
我想要具有 FIFO 的服务器-客户端模型和客户端获取目录路径,但我收到错误“读:错误地址”和“写:错误地址”。 客户端 服务器错误:“读取:地址错误” 客户端错误:“写入:地址错误” 最佳答案 您可
我是一名优秀的程序员,十分优秀!