- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 196字
- 2021-06-24 15:37:47
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:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/4702e546-c068-42ff-9d31-9b1814410e73.png?sign=1739268480-WYvQLle5LGB5GapcNkdpJBV6W5vLa7LQ-0-2f38366951783b509a0ea3e79771196e)
Let's take a look at the main method in the next section.