
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Create a Priority Queue
A priority queue is a type of queue in which priorities are assigned to the elements and the elements with higher priority are popped out first than the elements with lower priority.
In this article, we will write Golang programs to create a priority queue. They can be implemented using heaps, slices, trees etc and are used to perform the operations such as pushing elements in queue and removing elements from queue.
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 named value of the item of type string, priority of the item of type int and index of the item of type int
Step 2 ? Then, create a PriorityQueue type as a slice of Item with a pointer pointing to it
Step 3 ? Implement the Len() method, Less() method, Swap() method, Push() Method, Pop() Method.
Step 4 ? Finally, implement update method to update the priority and the value of the queue elements
Step 5 ? In the main, create an empty priority queue, piq, of the type PriorityQueue
Step 6 ? In this step, push items into the priority queue using the Push() function from the heap package and update the priority of an item using the Update() method of the PriorityQueue.
Step 7 ? Here, pop items from the priority queue from the heap package until the queue is empty. Print the value and priority of each popped item.
Example 1
In this example, we will write a Golang program to create a priority queue using a binary heap. The slice of items will be created as pointers pointing to it.
package main import ( "container/heap" "fmt" ) type Item struct { value string priority int index int } type PriorityQueue []*Item func (piq PriorityQueue) Len() int { return len(piq) } func (piq PriorityQueue) Less(i, j int) bool { return piq[i].priority > piq[j].priority } func (piq PriorityQueue) Swap(i, j int) { piq[i], piq[j] = piq[j], piq[i] piq[i].index = i piq[j].index = j } func (piq *PriorityQueue) Push(x interface{}) { n := len(*piq) item := x.(*Item) item.index = n *piq = append(*piq, item) } func (piq *PriorityQueue) Pop() interface{} { old := *piq n := len(old) item := old[n-1] item.index = -1 *piq = old[0 : n-1] return item } func (piq *PriorityQueue) Update(item *Item, value string, priority int) { item.value = value item.priority = priority heap.Fix(piq, item.index) } func main() { piq := make(PriorityQueue, 0) heap.Push(&piq, &Item{value: "Item 1", priority: 30}) heap.Push(&piq, &Item{value: "Item 2", priority: 10}) heap.Push(&piq, &Item{value: "Item 3", priority: 20}) item := piq[2] piq.Update(item, item.value, 40) for piq.Len() > 0 { item := heap.Pop(&piq).(*Item) fmt.Printf("Value: %s, Priority: %d\n", item.value, item.priority) } }
Output
Value: Item 3, Priority: 40 Value: Item 1, Priority: 30 Value: Item 2, Priority: 10
Example 2
In this example, we will write a Golang program to implement a priority queue using a slice of Item struct.
package main import ( "fmt" ) type Item struct { value string priority int } type PriorityQueue []Item func (piq PriorityQueue) Len() int { return len(piq) } func (piq PriorityQueue) Less(i, j int) bool { return piq[i].priority > piq[j].priority } func (piq PriorityQueue) Swap(i, j int) { piq[i], piq[j] = piq[j], piq[i] } func (piq *PriorityQueue) Push(x interface{}) { item := x.(Item) *piq = append(*piq, item) } func (piq *PriorityQueue) Pop() interface{} { old := *piq n := len(old) item := old[n-1] *piq = old[0 : n-1] return item } func main() { piq := make(PriorityQueue, 0) piq.Push(Item{value: "Item 1", priority: 30}) piq.Push(Item{value: "Item 2", priority: 10}) piq.Push(Item{value: "Item 3", priority: 20}) for piq.Len() > 0 { item := piq.Pop().(Item) fmt.Printf("Value: %s, Priority: %d\n", item.value, item.priority) } }
Output
Value: Item 3, Priority: 20 Value: Item 2, Priority: 10 Value: Item 1, Priority: 3
Conclusion
In this article we compiled and executed the program of creating a priority queue using two examples. In the first example we used a heap to create a "priorityqueue" and in the second example we used slice and custom functions. Priority queue can be used in task scheduling, huffman coding, dijkstra's algorithm and many more.