This is 3rd post on linked list. For previous posts, pls check the links below:
https://sreesindhusruthi.blogspot.com/2021/07/create-single-linked-list-in-golang.html
https://sreesindhusruthi.blogspot.com/2021/07/length-of-linkedlist-implementation-in.html
Writing print function for linked list to print elements.
The function name is Print and elements data is available in Node.data .
Step1: Check if linked list is empty. If empty print "NULL"
Step2: Print Node.data and update head to next Node in the linkedlist.
The function code is :
func (ll *LinkedList) Print() {
if ll.head == nil{
fmt.Print("NULL")
return
}
//No Intermediate variable is used so we are traversing on
//the actual object created and it's references gets updated on updating ll.head.
for ll.head != nil {
fmt.Printf("%+v -->",ll.head.data)
ll.head = ll.head.next
ll.Print()
}
}
Complete code:
No comments:
Post a Comment