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

The AddAfter method

The AddAfter method adds the node after a specific node. The AddAfter method of LinkedList has nodeProperty and property parameters. A node with the nodeProperty value is retrieved using the NodeWithValue method. A node with property is created and added after the NodeWith node, as follows:

//AddAfter method adds a node with nodeProperty after node with property

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 {
node.nextNode = nodeWith.nextNode
nodeWith.nextNode = node
}
}

You then get the following output when the AddAfter method is invoked when the node with a property value of 7 is added after the node with a value of 1. The nextNode property of the node with a property value of 1 is nil. The nextNode property of the node with a property value of 1 is set to the node with a value of 5

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