- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 223字
- 2021-06-24 15:37:48
AddAfter method
The AddAfter method adds a node after a specific node to a double linked list. The AddAfter method of the double LinkedList class searches the node whose value is equal to nodeProperty. The found node is set as the previousNode of the node that was added with property. The nextNode of the added node will be the nodeWith property's nextNode. The previousNode of the added node will be the node that was found with value equal to nodeProperty. The nodeWith node will be updated to the current node. In the following code, the AddAfter method is shown:
//AddAfter method of LinkedList
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
node.previousNode = nodeWith
nodeWith.nextNode = node
}
}
The example output after the AddAfter method is invoked with property 7 is as follows. A node with property value 7 is created. The nextNode property of the created node is nil. The nextNode property of the created node is set to headNode with property value 1. The previousNode property of headNode is set to the current node:
Let's take a look at the AddToEnd method in the next section.