panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x8...
I am trying to implement addition of polynomials using linked list. The program adds the power 0 coefficients successfully but after the first traversal it panics. Here is the code I have written so far. after initializing temp1!= nil the loop iterates over else but doesn't enter if loop when powers are different and goes into panic state
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
}
Output when the program is run:
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
Update: Thanks for the solution but I have successfully written the code for above problem. here it is.
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()
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
}
go linked-list
add a comment |
I am trying to implement addition of polynomials using linked list. The program adds the power 0 coefficients successfully but after the first traversal it panics. Here is the code I have written so far. after initializing temp1!= nil the loop iterates over else but doesn't enter if loop when powers are different and goes into panic state
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
}
Output when the program is run:
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
Update: Thanks for the solution but I have successfully written the code for above problem. here it is.
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()
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
}
go linked-list
add a comment |
I am trying to implement addition of polynomials using linked list. The program adds the power 0 coefficients successfully but after the first traversal it panics. Here is the code I have written so far. after initializing temp1!= nil the loop iterates over else but doesn't enter if loop when powers are different and goes into panic state
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
}
Output when the program is run:
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
Update: Thanks for the solution but I have successfully written the code for above problem. here it is.
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()
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
}
go linked-list
I am trying to implement addition of polynomials using linked list. The program adds the power 0 coefficients successfully but after the first traversal it panics. Here is the code I have written so far. after initializing temp1!= nil the loop iterates over else but doesn't enter if loop when powers are different and goes into panic state
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
}
Output when the program is run:
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
Update: Thanks for the solution but I have successfully written the code for above problem. here it is.
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()
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
}
go linked-list
go linked-list
edited Nov 22 '18 at 9:53
Sameer Agarwal
asked Nov 22 '18 at 5:15
Sameer AgarwalSameer Agarwal
123
123
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The error occur because you tried to access .power
property from nil
object in code below. Both temp1
and temp2
are nil
.
if temp1.power != temp2.power {
// ...
}
The .head
property on list
struct has pointer data type. The zero value of pointer data type is nil
. In your code, both list1
and list2
has nil
value on its .head
property.
What you need to do: initialise the value of .head
property explicitly during creation of both list1
and list2
. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
add a comment |
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424273%2fpanic-runtime-error-invalid-memory-address-or-nil-pointer-dereference-signal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error occur because you tried to access .power
property from nil
object in code below. Both temp1
and temp2
are nil
.
if temp1.power != temp2.power {
// ...
}
The .head
property on list
struct has pointer data type. The zero value of pointer data type is nil
. In your code, both list1
and list2
has nil
value on its .head
property.
What you need to do: initialise the value of .head
property explicitly during creation of both list1
and list2
. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
add a comment |
The error occur because you tried to access .power
property from nil
object in code below. Both temp1
and temp2
are nil
.
if temp1.power != temp2.power {
// ...
}
The .head
property on list
struct has pointer data type. The zero value of pointer data type is nil
. In your code, both list1
and list2
has nil
value on its .head
property.
What you need to do: initialise the value of .head
property explicitly during creation of both list1
and list2
. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
add a comment |
The error occur because you tried to access .power
property from nil
object in code below. Both temp1
and temp2
are nil
.
if temp1.power != temp2.power {
// ...
}
The .head
property on list
struct has pointer data type. The zero value of pointer data type is nil
. In your code, both list1
and list2
has nil
value on its .head
property.
What you need to do: initialise the value of .head
property explicitly during creation of both list1
and list2
. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
The error occur because you tried to access .power
property from nil
object in code below. Both temp1
and temp2
are nil
.
if temp1.power != temp2.power {
// ...
}
The .head
property on list
struct has pointer data type. The zero value of pointer data type is nil
. In your code, both list1
and list2
has nil
value on its .head
property.
What you need to do: initialise the value of .head
property explicitly during creation of both list1
and list2
. Then the panic error will disappear and your code will work properly.
list1 := &list{head: new(Node)}
// ...
list2 := &list{head: new(Node)}
// ...
Playground: https://play.golang.org/p/vPK3pYKht3E
answered Nov 22 '18 at 7:00
xparexpare
4,6672248
4,6672248
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
add a comment |
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
but on doing the same, the program goes into infinite loop.
– Sameer Agarwal
Nov 22 '18 at 7:08
1
1
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
Then there could be some logical error you need to fix.
– xpare
Nov 22 '18 at 7:09
add a comment |
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
add a comment |
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
add a comment |
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
You have a lot of mistakes in your code like the first answer posted. Also, why is there a need to keep track of the number of terms? I would suggest you take a simpler approach.
type Node struct {
Coeff int
Power int
next *Node
}
// Add coeff^power to a polynomial
func (n *Node) AddTerm(coeff int, power int) {
for t := n; t != nil; t = t.next {
if t.Power == power {
t.Coeff += coeff
return
}
}
n.next = &Node{coeff, power, n.next}
}
// Add a polynomial m to the polynomial
func (n *Node) AddPolynomial(m *Node) {
for t := m; t != nil; t = t.next {
n.AddTerm(t.Coeff, t.Power)
}
}
// The string representation of the polynomial
func (n *Node) String() string {
buff := strings.Builder{}
for t := n; t != nil; t = t.next {
fmt.Fprintf(&buff, " + %d^%d", t.Coeff, t.Power)
}
return buff.String()[3:]
}
// Add two polynomials together creating a new one
func Add(a *Node, b *Node) *Node {
c := &Node{}
c.AddPolynomial(a)
c.AddPolynomial(b)
return c
}
func main() {
p1 := &Node{}
p1.AddTerm(5, 2)
p1.AddTerm(4, 1)
p1.AddTerm(3, 0)
fmt.Println(p1)
p2 := &Node{}
p2.AddTerm(4, 1)
p2.AddTerm(2, 0)
fmt.Println(p2)
p3 := Add(p1, p2)
fmt.Println(p3)
}
Playground
answered Nov 22 '18 at 9:27
ssemillassemilla
3,077424
3,077424
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424273%2fpanic-runtime-error-invalid-memory-address-or-nil-pointer-dereference-signal%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown