Golang Program to Implementation of Linked List


March 8, 2022, Learn eTutorial
1653

What is a linked list?

A linked list (LL) is one of the basic and linear data structures like arrays. It has a bunch of nodes and each node contains the data and a pointer to access the next node. All the elements in the linked list are linked using pointers.

Linked List

How to Implement Linked List in Golang?

In this Go program, the Head is the starting node of the linked list and it points to the next node. It will continue to generate a linked list of nodes, as the previous node would store the address of the next node. Finally, the pointer of the last node points to a NULL value.

Let's see this with an example, Consider 4 numbers 100, 20, 40,70 which we are going to make nodes of a Linked List

  • Insert the first node which is the HEAD of LL will hold 100 as data and a pointer that holds the address of the next node
  • Insert 2nd node with data 20 and pointer to next node.
  • Insert 3rd node with data 40 and pointer to next node.
  • Insert 4th node with data 70 and a pointer to NULL because no more nodes are there to insert.
Linked List Example

A struct datatype can be used to create a node of the linked list, it will contain an integer value and a pointer to the next node. Next, we need a function to generate a new node, assign values to its variables, and attach the node to that LL. Another function is used to traverse from head to last and print the values with the help of a loop.

The library functions in Go will help us to implement this program, for that we need to import the fmt package at the start of the program. In the main function, we can call our user-defined functions to create nodes and traverse the linked list.

ALGORITHM

STEP 1: Start
STEP 2: Define the structure of the node
STEP 3: Assign head as the start node by calling the function NewNode(value)
STEP 4: Create the linked list such that the previous node would store the address of the next node by calling the function NewNode(value) 
STEP 5: Call the function TraverseLinkedList(head)
STEP 6: Exit

Steps to Implement function NewNode(value int):

STEP 1: Declare the variable n as Node.
STEP 2: Create node n by assigning data to its node value and changing the pointer of the node as nil. 
STEP 3: Return node n

Steps to Implement TraverseLinkedList(head *Node):

STEP 1: Display all the nodes from the linked list by traversing a list that starts with the head until we find the reference value is NULL.


The linked list implementation program uses the below GO programming topics, please refer to these topics to get a better understanding.

Golang Source Code

                                          package main
import "fmt"
type Node struct {
   value int
   next *Node
}
func NewNode(value int) *Node{
   var n Node
   n.value = value
   n.next = nil
   return &n
}
func TraverseLinkedList(head *Node){
   fmt.Printf("Linked List: ")
   temp := head
   for temp != nil {
      fmt.Printf("%d -> ", temp.value)
      temp = temp.next
   }
   fmt.Printf("NULL")
}
func main(){
   head := NewNode(100)
   head.next = NewNode(20)
   head.next.next = NewNode(40)
   head.next.next.next = NewNode(70)
   TraverseLinkedList(head)
}

                                      

OUTPUT

Linked List: 100 -> 20 -> 40 -> 70 -> NULL