In a doubly-linked list, all nodes have a pointer to the node they are connected to, on either side of them, in the list. This means that each node is connected to two nodes, and we can traverse forward through to the next node or backward through to the previous node.
Doubly linked lists allow insertion, deletion, and, obviously, traversing operations.
Golang Doubly Linked List Example
Let's create file named " doubly_linked_list.go" and add the following code to it:
package main
// importing fmt package
import (
"fmt"
)
// Node class
type Node struct {
property int
nextNode *Node
previousNode *Node
}
// LinkedList class
type LinkedList struct {
headNode *Node
}
//AddToHead method of LinkedList
func (linkedList *LinkedList) AddToHead(property int) {
var node = &Node{}
node.property = property
node.nextNode = nil
if linkedList.headNode != nil {
//fmt.Println(node.property)
node.nextNode = linkedList.headNode
linkedList.headNode.previousNode = node
}
linkedList.headNode = node
}
//NodeWithValue method of LinkedList
func (linkedList *LinkedList) NodeWithValue(property int) *Node {
var node *Node
var nodeWith *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
if node.property == property {
nodeWith = node
break
}
}
return nodeWith
}
//AddAfter method of LinkedList
func (linkedList *LinkedList) AddAfter(nodeProperty int, property int) {
var node = &Node{}
node.property = property
node.nextNode = nil
var nodeWith *Node
nodeWith = linkedList.NodeWithValue(nodeProperty)
if nodeWith != nil {
//fmt.Println(node.property)
node.nextNode = nodeWith.nextNode
node.previousNode = nodeWith
nodeWith.nextNode = node
}
}
//LastNode method of LinkedList
func (linkedList *LinkedList) LastNode() *Node {
var node *Node
var lastNode *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
if node.nextNode == nil {
lastNode = node
}
}
return lastNode
}
//AddToEnd method of LinkedList
func (linkedList *LinkedList) AddToEnd(property int) {
var node = &Node{}
node.property = property
node.nextNode = nil
var lastNode *Node
lastNode = linkedList.LastNode()
if lastNode != nil {
lastNode.nextNode = node
node.previousNode = lastNode
}
}
//IterateList method of LinkedList
func (linkedList *LinkedList) IterateList() {
var node *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
fmt.Println(node.property)
}
}
//NodeBetweenValues method of LinkedList
func (linkedList *LinkedList) NodeBetweenValues(firstProperty int, secondProperty int) *Node {
var node *Node
var nodeWith *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
if node.previousNode != nil && node.nextNode != nil {
if node.previousNode.property == firstProperty && node.nextNode.property == secondProperty {
nodeWith = node
break
}
}
}
return nodeWith
}
// main method
func main() {
var linkedList LinkedList
linkedList = LinkedList{}
linkedList.AddToHead(1)
linkedList.AddToHead(2)
linkedList.AddToHead(3)
linkedList.AddToHead(4)
linkedList.AddToEnd(5)
linkedList.AddAfter(1, 8)
fmt.Println(linkedList.headNode.property)
var node *Node
node = linkedList.NodeBetweenValues(1, 5)
fmt.Println(node.property)
}
The main() method creates a linked list. The nodes are added to the head and end. The node between values 1 and 5 is searched and its property is printed.
Run the following command to execute the doubly_linked_list.go file:
G:\GoLang\examples>go run doubly_linked_list.go 4 8