This is 4th post on linked list, for previous post on linkedlist, please check posts from :
https://sreesindhusruthi.blogspot.com/2021/07/create-single-linked-list-in-golang.html
Insert is the function name to insert nodes in linked list:
func (ll *LinkedList) Insert(data int, position int){ }
Step1: We need to have a Node with the data given.
temp := new(Node)
temp.data = data
Step2: If linked list is empty then insert the temp Node as head of the linked list.
//This inserts new element as linked list is empty
if ll.head == nil{
ll.head = temp
return
}
Step3: If position is 0 then we need to update the linkedlist head to temp Node and update the current linked list reference.
//This inserts at beginning of the linked list.
if position == 0{
temp.next = ll.head
ll.head = temp
}
Step4: Loop till we reach the position we want to Insert the Node, we need a variable currentNode to traverse till the desired position, update the currentNode for each iteration of the loop, we will compare the position with currentposition.
var currPosition int
currentNode := ll.head
for currPosition < position-1 && currentNode != nil {
currentNode = currentNode.next
currPosition ++
//fmt.Println(currentNode.data, currPosition)
}
Step5: Once we are at the postion where we have to Insert the node update the linked list references.
Example linked list : 3-->7-->10-->NULL
ll.Insert(4, 2)
Expected output is : 3--> 7--> 4-->10--> NULL
To acheive expected output store Node with data 10 into existingNextNode.
existingNextNode := currentNode.next
Update currentNode links 7--> 4 .
currentNode.next = temp
then traverse currentNode from 7 to 4.
currentNode = currentNode.next
update currentNode next to existingNextNode 4-->10
currentNode.next = existingNextNode
//Update the links
existingNextNode := currentNode.next
currentNode.next = temp
currentNode = currentNode.next
currentNode.next = existingNextNode
Full code:
package main | |
import "fmt" | |
type Node struct{ | |
data int | |
next *Node | |
} | |
func (n *Node) NewNode(data int, next *Node){ | |
n.data = data | |
n.next = next | |
} | |
type LinkedList struct{ | |
head *Node | |
} | |
func (ll *LinkedList) Len() int{ | |
count := 0 | |
var temp *Node | |
if ll.head == nil{ | |
return 0 | |
} | |
temp = ll.head | |
//fmt.Println(temp) | |
for temp != nil { | |
count ++ | |
temp = temp.next | |
//fmt.Println(temp) | |
} | |
return count | |
} | |
func (ll *LinkedList) Print() { | |
if ll.head == nil{ | |
fmt.Print("NULL") | |
return | |
} | |
for ll.head != nil { | |
fmt.Printf("%+v -->",ll.head.data) | |
ll.head = ll.head.next | |
ll.Print() | |
} | |
} | |
func (ll *LinkedList) Insert(data int, position int){ | |
var currPosition int | |
currentNode := ll.head | |
temp := new(Node) | |
temp.data = data | |
//This inserts new element as linked list is empty | |
if ll.head == nil{ | |
ll.head = temp | |
return | |
} | |
//This inserts at beginning of the linked list. | |
if position == 0{ | |
temp.next = ll.head | |
ll.head = temp | |
} | |
for currPosition < position-1 && currentNode != nil { | |
currentNode = currentNode.next | |
currPosition ++ | |
//fmt.Println(currentNode.data, currPosition) | |
} | |
//Update the links | |
existingNextNode := currentNode.next | |
currentNode.next = temp | |
currentNode = currentNode.next | |
currentNode.next = existingNextNode | |
} | |
func main(){ | |
llist := new(LinkedList) | |
llist.Insert(3, 0) | |
llist.Insert(7, 1) | |
llist.Insert(10, 2) | |
llist.Len() | |
llist.Insert(4, 2) | |
llist.Print() | |
llist.Len() | |
} | |
/* | |
Input: | |
3 -->7 -->10 -->NULL | |
Output: | |
3 -->7 -->4 -->10 -->NULL | |
*/ |