Is there anyway to override "invalid memory address or nil pointer dereference" to construct doubly linked list?

193 views
Skip to first unread message

Gyu-Ho Lee

unread,
Nov 10, 2013, 4:45:33 AM11/10/13
to golan...@googlegroups.com
My question starts from this code. http://play.golang.org/p/WKcQBWnEG9

As you see, when adding new elements to the list, either to the back or to the front, this code is not updating both of the two pointers in Node struct. One of which points to the next and the other to the previous node. 

A.tail= NewNode(input_value, A.tail, nil) 
or
A.head = NewNode(input_value, nil, A.head) 
works perfect for a singly linked list that only points to next
, so I did the same thing for the doubly linked list and get this error.



Another try can be found here: http://play.golang.org/p/bYg6MM1k7T
In which I try to avoid the nil pointer dereference, creating a temporary variable but I am still getting the same error.

Third try can be found here: http://play.golang.org/p/AEDAqn8hQU
In which I try to condition-test the empty list and do the similar thing and get the same error. And I am stuck at this point.

The main problem is for the structure like doubly linked list
, how do I initialize the nil pointer of the head ?
1. when I want to add new elements to the front
2. thereby when I need to update the second-, third-, fourth-front elements with a newly-inserted head nodes
3. and when I can't update the nil pointer of head node.

I am stuck on this all day.
I would greatly appreciate it!

Ingo Oeser

unread,
Nov 10, 2013, 2:15:48 PM11/10/13
to golan...@googlegroups.com

Kevin Gillette

unread,
Nov 10, 2013, 3:05:42 PM11/10/13
to golan...@googlegroups.com
You were forgetting to change the previous head's prev pointer (or previous tail's next pointer) to the new node, thus producing a detached head/tail.

A few notes about general code style:
  1. use camel-case instead of underscores (such as targetValue instead of target_value)
  2. keep names short (such as val instead targetValue), FindVal or Find instead of FindValue.
  3. 'Get' should be implicit, 'Set' can be explicit. Instead of GetPrevNode, call it Prev.
  4. don't provide redundant information; for example, with FindValue, you return (bool, *Node), though it's clear that if a nil *Node is returned, no matched *Node was found, thus the bool is pointless.
  5. use method names based on types when appropriate. Instead of InsertBeforeTarget, call it InsBeforeNode.
Additional note: TraverseFromTailDoublyLinkedList doesn't have anything to do with doubly linked list, since it's a unidirectional traverse. The only distinction between it and TraverseFromTail is that one iterates vals and one iterates nodes. I'd call them Iter (for values) and IterRev (for reverse iteration of values), while not providing any convenience method for *Node iteration, since you're not exposing anything that an application can usefully do with a *Node besides accessing a val if the application were just doing a linear traversal.

Luther

unread,
Nov 11, 2013, 12:43:13 PM11/11/13
to golan...@googlegroups.com
I'm not sure I understand Kevin's solution, but there's a simple solution to your first example. When adding the very first node, head and tail *both* need to point to that same node. To ensure this, AddHead and AddTail need to check if the list is empty.
Reply all
Reply to author
Forward
0 new messages