![Learn Data Structures and Algorithms with Golang](https://wfqqreader-1252317822.image.myqcloud.com/cover/744/36698744/b_36698744.jpg)
上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:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/4702e546-c068-42ff-9d31-9b1814410e73.png?sign=1738921322-Tf4yAGBY2JY8o3IEsQ0sFasqoD7zaTQh-0-4058b9bfacf9ae8d41d25296cfea73a1)
Let's take a look at the main method in the next section.