Golang program to traverse a circular linked list and print its elements


In this article we are going to understand how to creat a golang program to traverse a circular linked list and print it’s element using simple for and while loop. Circular linked lists are a type of data structure where the last element of the list is connected to the first element, forming a loop.

Algorithm

  • Step 1 − First, we need to import the fmt package. This struct contains a data variable to store data along with a pointer variable to store the address of the next node.

  • Step 2 − Then create a function called Traverse() to traverse over the elements of the list. This function uses a for loop to print the respective elements.

  • Step 3 − Now, create the main() function. Inside the main() create a node to the struct called head and assign values to it.

  • Step 4 − In this manner create a number of nodes by placing the address of the next node to the next pointer of the head node and assign different values to the data variables of all these nodes.

  • Step 5 − To traverse over this list call the traverse() function by passing the head node as an argument to the function and print the elements of the list in circular manner on the screen.

Example 1

In this Example we will write a go language program to traverse a circular linked list and print its elements by using a for loop. It is the simplest way to traverse a circular linked list. Here we will start from the head of the list and iterate through a loop until the head is reached again.

package main

import "fmt"

type Node struct {
   data int
   next *Node
}

func traverseCircularList(head *Node) {
   current := head
   elem := current.data
   for {
      fmt.Printf("%d ", current.data)
      current = current.next
      if current == head {
         break
      }
   }
   fmt.Println(elem)
}

func main() {
   head := &Node{data: 10}
   head.next = &Node{data: 20}
   head.next.next = &Node{data: 30}
   head.next.next.next = head
   fmt.Println("The elements obtained by traversing over the circular linked list are:")
   traverseCircularList(head)
}

Output

The elements obtained by traversing over the circular linked list are:
10 20 30 10

Example 2

In this Example we will write a go language program to traverse over a circular linked list through a while loop.

package main

import "fmt"

type Node struct {
   data int
   next *Node
}

func traverseCircularList(head *Node) {
   if head == nil {
      return
   }
   fmt.Println("The elements of the circular linked list are:")
   current := head
   for current.next != head {
      fmt.Printf("%d ", current.data)
      current = current.next
   }
   fmt.Printf("%d ", current.data)
}

func main() {
   head := &Node{data: 11}
   head.next = &Node{data: 12}
   head.next.next = &Node{data: 13}
   head.next.next.next = head
   traverseCircularList(head)
}

Output

The elements of the circular linked list are:
11 12 13 

Conclusion

We have successfully compiled and executed a go language program to traverse over a circular linked list and print its elements on the screen along with Examples. We have used two programs in this article. In the first program we are using a for loop while in the second program we are using a while loop to implement the result. All three methods are effective and easy to implement, and the choice of method depends on personal preference and coding style.

Updated on: 05-Apr-2023

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements