Golang program to implement linked list


In Go programming language, a linked list is a linear data structure made up of a series of nodes which are linked to each other via next pointer, which points to the next address. We will implement linked list in this program using two methods. In the first method struct will be used and in the second example list struct will be used.

Method 1: Using Struct

In this method, there are three nodes in this linked list, and each one has a value of 1, 2, or 3. Each node's next pointer points to the node after it in the list, and the head variable points to the first node. Following each node's next pointer, the for-loop loops through the linked list until it reaches a node with a nil next pointer, which denotes the list's end.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a node structure with the fields next and num_val. The node's value is stored in value, and next is a pointer to the node after it in the list.

  • Step 3 − Create a head node in the main function and set the list's first value as its num_value.

  • Step 4 − A second node should be created and its num_value set to the following value in the list.

  • Step 5 − By setting the head node's next pointer to the second node, you can connect the head node and the second node.

  • Step 6 − To add more nodes and connect them, repeat steps 3 and 4 to complete the connected list.

  • Step 7 − In the next step to the head node, create a current pointer and set it.

  • Step 8 − Follow the next pointers of each node as you go over the linked list using a for loop.

  • Step 9 − Print the value of the current node inside the for loop using fmt.Println() function were ln means new line.

  • Step 10 − By updating it to the value of the next pointer, the current pointer is changed to the next node in the list.

  • Step 11 − Repeat steps 7-9 up until the current node's next pointer is nil, indicating the list's end.

Example

In this example we will use struct to implement linked list.

package main
import "fmt"

type node struct { //create a struct
   num_val int
   next    *node
}

func main() {
   head := &node{num_val: 1}
   head.next = &node{num_val: 2}
   head.next.next = &node{num_val: 3}
   fmt.Println("The implementation of linked list is given as following:")

   current := head
   for current != nil {               //run a for loop to print values of current node
      fmt.Println(current.num_val)
      current = current.next
   }
}

Output

The implementation of linked list is given as following:
1
2
3

Method 2: Using a List Struct

The linked list is implemented in this implementation as a List struct with a head field that points to the root node. By constructing a new node, setting its next pointer to the existing head node, and modifying the head field of the List struct to point to the new node, the Insert method adds a new node with a specified value at the start of the list. Following each node's next pointer, the Print method prints the values of every node in the list.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a List struct and give its head field a type of *Node.

  • Step 3 − Create a Node struct with value and next as its two fields. The node's value is stored in value, and next is a pointer to the node after it in the list.

  • Step 4 − Create an Insert method that accepts a value as an argument for the List struct.

  • Step 5 − Create a new node n with the specified value within the Insert method.

  • Step 6 − Set the new node's next pointer to the List struct's current head.

  • Step 7 − To point to the new node, update the List struct's head field.

  • Step 8 − Create a method called Print for the List struct that iterates across the linked list and outputs each node's value.

  • Step 9 − Create a List struct in the main function and use the Insert method to add a number of nodes to it.

  • Step 10 − To print the values of each node in the list, call the Print method.

Example

In this example we will use list struct to implement linked list.

package main
import "fmt"

type List struct { //create a list struct
   head *Node
}

type Node struct {
   value_num int
   next      *Node
}

func (l *List) Insert(value_num int) {
   n := &Node{value_num: value_num}
   n.next = l.head
   l.head = n
}

func (l *List) Print() {
   current := l.head
   for current != nil {
      fmt.Println(current.value_num)
      current = current.next
   }
}

func main() {
   list := &List{}  //create a list struct
   fmt.Println("The implementation of linked list is given as:")
   list.Insert(3)
   list.Insert(2)
   list.Insert(1)
   list.Print()
}

Output

The implementation of linked list is given as:
1
2
3

Conclusion

We executed the program of implementing linked list using two examples. In the first example we used struct and in the second example we used list struct to implement linked list.

Updated on: 22-Feb-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements