Golang program to implement the Queue data structure


In this Golang program, a queue is a data structure that works on the First-In-First-Out (FIFO) principle where elements are added to the rear and removed from the front. Although Go doesn't come with a built-in queue data structure, slices, linked lists, and other data structures can be used to build one. We will use two methods to implement queue data structure using slices and linked list.

Method 1: Using Slice Method

The fundamental operations for a queue data structure: Enqueue, Dequeue, and IsEmpty are implemented in this implementation, which employs a slice to hold the items in the queue

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 an empty slice called q.item_value in the queue data structure to hold the components.

  • Step 3 − In the enqueue operation just append the element to the q.item_value slice using the append function to add it to the end of the queue.

  • Step 4 − In the dequeue operation extract the first item from the q.item_value slice using indexing and put it in a variable to remove an element from the front of the queue. Then, to eliminate the first item, slice the q.item_value slice starting with the second element. Return the item that was extracted.

  • Step 5 − Implement the function IsEmpty to determine whether the queue is empty, simply determine whether the length of q.item_value is 0.

  • Step 6 − This example shows how to utilize a basic queue data structure in Go to enqueue and dequeue elements as well as verify if the queue is empty.

Example

In this example we will use slice to implement queue data structure. Let’s see how thois can be executed.

package main
import "fmt"

type Queue struct {
   item_value []int
}

func (q *Queue) Enqueue(item int) {
   q.item_value = append(q.item_value, item)  //used to add items
}

func (q *Queue) Dequeue() int {
   item := q.item_value[0]
   q.item_value = q.item_value[1:]  //used to remove items
   return item
}

func (q *Queue) IsEmpty() bool {
   return len(q.item_value) == 0
}

func main() {
   fmt.Println("Enqueue and Dequeue the elements:")
   q := &Queue{}  // create a queue instance which will be used to enqueue and dequeue elements
   q.Enqueue(1)
   q.Enqueue(2)
   q.Enqueue(3)
   for !q.IsEmpty() {
      fmt.Println(q.Dequeue())  //check whether the queue is empty or not
   }
}

Output

Enqueue and Dequeue the elements:
1
2
3

Method 2: Using Linked List

In this method, the elements of the queue are kept on a linked list. A value and a pointer to the following node are contained in each node of the linked list. The front of the line is shown by the head pointer, while the back is indicated by the tail pointer. By modifying the next pointer of the existing tail node and the tail pointer to point to the new node, the Enqueue operation adds a new node to the end of the linked list. By changing the head pointer to go to the subsequent node, the Dequeue operation removes the front node from the linked list. The Size action returns the queue's total number of entries.

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 − Initialize a queue data structure with a variable size to keep track of the queue's element count and pointers to the head and tail of a linked list.

  • Step 3 − Set the next pointer of a new node to nil and create it with the specified value.

  • Step 4 − Update the head and tail pointers to the new node if the queue is empty.

  • Step 5 − Otherwise, update both the tail pointer and the next pointer of the current tail node to point to the new node. Add one to the size variable.

  • Step 6 − Return 0 and false if the queue is empty. If not, subtract 1 from the size variable, extract the value of the front node, update the head pointer to the next node, and return the extracted value and true.

  • Step 7 − Return the value of the size variable using the size operation.

  • Step 8 − The preceding example shows how to use a linked list to create a queue data structure in Go and how to use it to enqueue and dequeue elements as well as obtain the queue's size.

Example

In this example, we will use linked list to implement the queue data structure. Let’s see through the code.

package main
import "fmt"

type Node struct {
   value int
   next  *Node   //use of linked list to implements queue
}

type Queue struct {
   head *Node
   tail *Node
   size int
}

//add the elements in the queue
func (qe *Queue) Enqueue(value int) {
   newNode := &Node{value: value}
   if qe.size == 0 {
      qe.head = newNode
      qe.tail = newNode
   } else {
      qe.tail.next = newNode
      qe.tail = newNode
   }
   qe.size++
}

//remove the elements from queue
func (qe *Queue) Dequeue() (int, bool) {
   if qe.size == 0 {
      return 0, false
   }
   value := qe.head.value
   qe.head = qe.head.next
   qe.size--
   return value, true
}

//return the size of queue
func (qe *Queue) Size() int {
   return qe.size
}

func main() {
   qe := &Queue{}  //create an instance of queue
   qe.Enqueue(1)
   qe.Enqueue(2)
   qe.Enqueue(3)
   fmt.Println("Enqueue and Dequeue the elements:")
   for qe.Size() > 0 {
      value, success := qe.Dequeue()
      if success {
         fmt.Println(value)
      }
   }
}

Output

Enqueue and Dequeue the elements:
1
2
3

Conclusion

We executed the program of implementing the queue data structure using two methods. In the first method we used slice and in the second example we used lined list to execute the program.

Updated on: 20-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements