Golang program to check if a priority queue is empty


A priority queue is a queue in which elements are stored along with their priority values. The functions supported by priority queue are − enqueue and dequeue where enqueue implies adding elements to the queue along with their priorities and dequeue implies removing elements from the queue along with their priorities. In this article, we will write a Go language program to check if a priority queue is empty.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func len(v Type) int

The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • Step 1 − Create an Item struct with three fields for the value of type string, priority, and index of type int

  • Step 2 − Then, create PriorityQueue type as a slice of *Item pointers and Create the Len, Less, Swap, Push, and Pop methods for the PriorityQueue type

  • Step 3 − Then, implement the IsEmpty method for the PriorityQueue type to check if the length of the priority queue is zero

  • Step 4 − In the main, create an empty PriorityQueueusing make as built-in function and assign it as pq. Then, check if pq is empty using the IsEmpty method and print the output on the console

  • Step 5 − Create and push an item with a value of "chocolate" and priority of 2 to pq. In this step, create and push next item with a value of "milk-shake" and priority of 1 to pq

  • Step 6 − check whether pq is empty and print the output on the console. Then, use heap. Pop to remove an item from pq.

  • Step 7 − Finally, check if pq is empty and print the output.

Example 1

In this example, we will write a Go language program to check if a priority queue is empty using internal methods of the container/heap package.

package main

import (
   "container/heap"
   "fmt"
)
type Item struct {
   value    string 
   priority int    
   index    int    
}
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
   return pq[i].priority < pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
   pq[i], pq[j] = pq[j], pq[i]
   pq[i].index = i
   pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
   n := len(*pq)
   item := x.(*Item)
   item.index = n
   *pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
   old := *pq
   n := len(old)
   item := old[n-1]
   item.index = -1 
   *pq = old[0 : n-1]
   return item
}
func (pq *PriorityQueue) IsEmpty() bool {
   return len(*pq) == 0
}
func main() {
   pq := make(PriorityQueue, 0)

   heap.Init(&pq)

   fmt.Println("Is priority queue empty?", pq.IsEmpty())

   item1 := &Item{
      value:    "chocolate",
      priority: 2,
   }
   heap.Push(&pq, item1)

   fmt.Println("Is priority queue empty?", pq.IsEmpty())

   item2 := &Item{
      value:    "milk shake",
      priority: 1,
   }
   heap.Push(&pq, item2)

   fmt.Println("Is priority queue empty?", pq.IsEmpty())

   _ = heap.Pop(&pq)

   fmt.Println("Is priority queue empty?", pq.IsEmpty())

   _ = heap.Pop(&pq)

   fmt.Println("Is priority queue empty?", pq.IsEmpty())
}

Output

Is priority queue empty? true
Is priority queue empty? false
Is priority queue empty? false
Is priority queue empty? false
Is priority queue empty? true

Conclusion

In this article we have discussed how we can check if a priority queue is empty in our language. We have executed the program for this operation which uses a container/ heap package. An empty priority queue may look useless but it serves as the building block of large algorithms, offers flexibility, and may be used in a variety of other ways.

Updated on: 05-Jul-2023

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements