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

The AddToEnd method

The AddToEnd method adds the node at the end of the list. In the following code, the current lastNode is found and its nextNode property is set as the added node:

//AddToEnd method adds the node with property to the end

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

In the following screenshot, the AddToEnd method is invoked when the node with a property value of 5 is added to the end. Adding the property through this method creates a node with a value of 5. The last node of the list has a property value of 5. The nextNode property of lastNode is nil. The nextNode of lastNode is set to the node with a value of 5:

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