Implement circular linked list in Golang


In this article we will learn how to write a go language program to implement circular linked list using struct and slice method. circular linked lists are created in such a way that each node of the linked list points to the next node and the last node points again to the starting node.

Example 1

In this Example we will write a go language program to implement the circular linked lists by using a struct to store each node of the list.

package main

import "fmt"

type Node struct {
   data int
   next *Node
}

type CircularLinkedList struct {
   head *Node
   tail *Node
}

func NewCircularLinkedList() *CircularLinkedList {
   return &CircularLinkedList{head: nil, tail: nil}
}

func (cll *CircularLinkedList) IsEmpty() bool {
   return cll.head == nil
}

func (cll *CircularLinkedList) AddNode(data int) {
   newNode := &Node{data: data, next: nil}
   
   if cll.IsEmpty() {
      cll.head = newNode
      cll.tail = newNode
      newNode.next = cll.head
   } else {
      cll.tail.next = newNode
      cll.tail = newNode
      newNode.next = cll.head
   }
}

func (cll *CircularLinkedList) Traverse() {
   if cll.IsEmpty() {
      fmt.Println("Circular Linked List is empty")
   } else {
      current := cll.head
      for {
         fmt.Printf("%d -> ", current.data)
         current = current.next
         if current == cll.head {
            break
         }
      }
      fmt.Printf("%d\n", cll.head.data)
   }
}

func main() {
   cll := NewCircularLinkedList()
   fmt.Println("The nodes of circular linked list are:")
   cll.AddNode(1)
   cll.AddNode(2)
   cll.AddNode(3)
   cll.AddNode(4)
   cll.AddNode(5)
   cll.Traverse()
}

Output

The nodes of circular linked list are:
1 -> 2 -> 3 -> 4 -> 5 -> 1

Example 2

In this Example we will write a go language program to implement the circular linked list by using a slice to store the values to the list.

package main

import (
   "fmt"
)

type CircularLinkedList struct {
   values []int
   size   int
}

func (cll *CircularLinkedList) AddNode(value int) {
   cll.values = append(cll.values, value)
   cll.size++
}

func (cll *CircularLinkedList) RemoveNode(value int) bool {
   for i, v := range cll.values {
      if v == value {
         cll.values = append(cll.values[:i], cll.values[i+1:]...)
         cll.size--
         return true
      }
   }
   return false
}

func (cll *CircularLinkedList) PrintList() {
   for i := 0; i < cll.size; i++ {
      fmt.Printf("%d -> ", cll.values[i%cll.size])
   }
   fmt.Printf("%d", cll.values[0])
   fmt.Println()
}

func main() {
   cll := &CircularLinkedList{[]int{}, 0}

   // add nodes
   cll.AddNode(1)
   cll.AddNode(2)
   cll.AddNode(3)
   cll.AddNode(4)
   fmt.Println("The obtained circular linked list is:")
   cll.PrintList()

   cll.RemoveNode(3)
   fmt.Println()
   fmt.Println("The circular linked list obtained after removing node 3 are:")
   cll.PrintList()
}

Output

The obtained circular linked list is:
1 -> 2 -> 3 -> 4 -> 1

The circular linked list obtained after removing node 3 are:
1 -> 2 -> 4 -> 1

Conclusion

We have successfully compiled and executed a go language program to implement a circular linked list along with the Examples. Here we have used two programs. In the first program we are using a struct to store the values and position of the nodes in the list while in the second one we have used a slice to store the data.

Updated on: 05-Apr-2023

446 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements