Golang program to count number of nodes in a circular linked list


In golang, circular linked lists are an important data structure used in computer science to efficiently manage memory and data. A circular linked list is a linked list in which the last node of the list points to the first node of the list, creating a loop.

Using Traversal Method

In this method, we will write a go language program to count the number of nodes in a circular linked list by traversing through it.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, initialize a node struct and assign two variables in it. The first variable stores the integer data while the second pointer variable stores the address to the next node.

  • Step 3 − Now, create a different function called countNodes(). This function accepts the list to be traversed as argument.

  • Step 4 − Inside the function store 0 to the count variable and store the head node of the list to the current variable.

  • Step 5 − Use a for loop to iterate over the linked list and increment the count variable every time a new node is reached. Further returns this variable.

  • Step 6 − Start the main() function. Inside the main() function initialize different nodes called head, node2, node3 etc and link them together by using storing the address of next node to the next element of the current node.

  • Step 7 − Now call the countNodes() function and pass the head node of the linked list as argument to the function.

  • Step 8 − Further, Store the count obtained by the function to a different variable and print it on the screen by using the fmt.Println() function.

Example

The following Example demonstrates how to develop a go language program to count the number of nodes in a circular linked list by using traversal method

package main

import "fmt"

type Node struct {
   data int
   next *Node
}

func countNodes(head *Node) int {
   count := 0
   current := head

   for current != nil && current.next != head {
      count++
      current = current.next
   }

   if current == head {
      count++
   }
   return count
}

func main() {
   head := &Node{data: 1}
   node2 := &Node{data: 2}
   node3 := &Node{data: 3}
   node4 := &Node{data: 4}
   head.next = node2
   node2.next = node3
   node3.next = node4
   node4.next = head

   count := countNodes(head)
   fmt.Println("The number of nodes in the given circular linked list are:", count)
}

Output

The number of nodes in the given circular linked list are: 3

Using Floyd’s Cycle-Finding Algorithm

In this method, we will write a go language program to count the number of nodes present in a circular linked list by using the floyd’s cycle finding Algorithm. This Algorithm uses two pointer variables, one variable moves one node at a time while the other variable moves two nodes at a time.

Algorithm

  • Step 1 − First, we need to import the fmt package. Then create a node called struct and store in it a variable to store data and a pointer to store the address of the next node.

  • Step 2 − Create a function called countNodes() to count the number of nodes present in the given list. create two pointer variables called slow and fast and store the address to the head node to them.

  • Step 3 − Traverse the linked list by using a loop until either the fast pointer reaches the end of the list or it reaches the slow pointer. If this happens then there is no loop, so return zero as the number of nodes in the list.

  • Step 4 − In order to count the number of nodes. Traverse again through the linked list and increment the count variable and move slow pointer one step forward.

  • Step 5 − Once the slow pointer reaches the fast pointer again, we count all the nodes in the loop. Return the count variable as the number of nodes in the linked list.

  • Step 6 − Now, start the main() function. Inside the main() initialize the head node by using the node struct and assign value to it.

  • Step 7 − In this manner create different nodes and link them together with the head node. Call the countNodes() function by passing the head node as an argument to the function.

  • Step 8 − Store the result obtained by the function in a variable and print it on the screen.

Example

The following Example explains how to create go language program to count the number of nodes in a circular linked lists by using floyd’s cycle-finding Algorithm

package main

import "fmt"

type Node struct {
   data int
   next *Node
}

func countNodes(head *Node) int {
   slow := head
   fast := head

   // Detect the presence of a loop
   for fast != nil && fast.next != nil {
      slow = slow.next
      fast = fast.next.next
      if slow == fast {
         break
      }
   }

   count := 0
   if fast != nil && fast.next != nil {
      slow = slow.next
      count++
      for slow != fast {
         count++
         slow = slow.next
      }
   }
   return count
}

func main() {
   head := &Node{data: 1}
   head.next = &Node{data: 2}
   head.next.next = &Node{data: 3}
   head.next.next.next = head

   fmt.Println("Number of nodes present in the circular linked list are:", countNodes(head))
}

Output

Number of nodes present in the circular linked list are: 3

Conclusion

We have successfully compiled and executed a go language program to count the number of nodes in a circular linked list along with Examples. Here we have used two programs. In the first program we are using method of traversal while in the second one we are using Floyd-cycle method to implement the result.

Updated on: 05-Apr-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements