Learn Data Structures and Algorithms with Golang
上QQ阅读APP看书,第一时间看更新

The AddToEnd method

The AddToEnd method adds the node to the end of the double linked list. The AddToEnd method of the LinkedList class creates a node whose property is set as the integer parameter property. The method sets the previousNode property of the node that was added with the current lastNode property as follows. The nextNode of the current lastNode property is set to a node added with property at the end as follows:

//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
}
}

The example output after the AddToEnd method was invoked with property 5 is as follows. A node with property value 5 is created. The lastNode of the list has property value 1. The nextNode property of the lastNode is nil. The nextNode of the lastNode is set to the node with property value 5. The previousNode of the created node is set to the node with property value 1:

Let's take a look at the main method in the next section.