Open In App
Related Articles

Queue in Go Language

Improve Article
Improve
Save Article
Save
Like Article
Like

A queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO)

Now if you are familiar with other programming languages like C++, Java, and Python then there are inbuilt queue libraries that can be used for the implementation of queues, but such is not the case in the case of Golang. Even if you are not familiar with those then just know that Golang does not provide an inbuilt queue structure.

How to implement Queue in Go Language?

There are many ways to implement queues in Golang using other Data structures as:

  1. Using Slices 
  2. Using Structures
  3. Using LinkList

1. Implement Queue Using Slices in Go Language:

Implementing queue using a simple slice in which enqueueing and dequeuing operations are done using functions. and Underflow(queue is empty) is checked during dequeuing operation.

Go




package main
  
import "fmt"
  
func enqueue(queue []int, element int) []int {
queue = append(queue, element) // Simply append to enqueue.
fmt.Println("Enqueued:", element)
return queue
}
  
func dequeue(queue []int) (int, []int) {
element := queue[0] // The first element is the one to be dequeued.
if len(queue) == 1 {
 var tmp = []int{}
 return element, tmp
  
}
  
return element, queue[1:] // Slice off the element once it is dequeued.
}
  
func main() {
var queue = make([]int, 0)
  
queue = enqueue(queue, 10)
  
fmt.Println("After pushing 10 ", queue)
queue = enqueue(queue, 20)
  
fmt.Println("After pushing 20 ", queue)
queue = enqueue(queue, 30)
  
fmt.Println("After pushing 30 ", queue)
  
ele, queue := dequeue(queue)
fmt.Println("Queue After removing", ele, " :", queue)
  
queue = enqueue(queue, 40)
fmt.Println("After pushing 40 ", queue)
}


Output:
Enqueued: 10
After pushing 10  [10]
Enqueued: 20
After pushing 20  [10 20]
Enqueued: 30
After pushing 30  [10 20 30]
Queue After removing 10  : [20 30]
Enqueued: 40
After pushing 40  [20 30 40]

Note: In this, the problem is we can not define the size or capacity of the queue. However, it can be done by defining the queue as make([]int, 0, 10) where the third parameter determines capacity but the problem arises when capacity dynamically increases in an overflow condition.

2. Using Structures:

To overcome the problem in the earlier one, use Structures instead which consist of 

  • Elements i.e. queue Elements
  • Size i.e. Capacity of 

Use Pointers to directly change the queue without returning it every time and, check for both overflow and underflow conditions:

Go




package main
  
import (
    "errors"
    "fmt"
)
  
type Queue struct {
    Elements []int
    Size     int
}
  
func (q *Queue) Enqueue(elem int) {
    if q.GetLength() == q.Size {
        fmt.Println("Overflow")
        return
    }
    q.Elements = append(q.Elements, elem)
}
  
func (q *Queue) Dequeue() int {
    if q.IsEmpty() {
        fmt.Println("UnderFlow")
        return 0
    }
    element := q.Elements[0]
    if q.GetLength() == 1 {
        q.Elements = nil
        return element
    }
    q.Elements = q.Elements[1:]
    return element // Slice off the element once it is dequeued.
}
  
func (q *Queue) GetLength() int {
    return len(q.Elements)
}
  
func (q *Queue) IsEmpty() bool {
    return len(q.Elements) == 0
}
  
func (q *Queue) Peek() (int, error) {
    if q.IsEmpty() {
        return 0, errors.New("empty queue")
    }
    return q.Elements[0], nil
}
  
func main() {
    queue := Queue{Size: 3}
    fmt.Println(queue.Elements)
    queue.Enqueue(1)
    fmt.Println(queue.Elements)
    queue.Enqueue(2)
    fmt.Println(queue.Elements)
    queue.Enqueue(3)
    fmt.Println(queue.Elements)
    queue.Enqueue(5)
    fmt.Println(queue.Elements)
    elem := queue.Dequeue()
    fmt.Println(elem)
    fmt.Println(queue.Elements)
    queue.Enqueue(9)
    fmt.Println(queue.Elements)
    elem = queue.Dequeue()
    fmt.Println(elem)
    fmt.Println(queue.Elements)
  
}


Output:
[]
[1]
[1 2]
[1 2 3]
Overflow
[1 2 3]
1
[2 3]
[2 3 9]
2
[3 9]

Note: We used to compare the length of elements to the size(Capacity defined) of queue structure which is more good to use.

3. Using LinkList:

Go




package main
import "container/list"
import "fmt"
  
func main() {
    // new linked list
    queue := list.New()
  
    // Simply append to enqueue.
    queue.PushBack(10)
    queue.PushBack(20)
    queue.PushBack(30)
  
    // Dequeue
    front:=queue.Front()
    fmt.Println(front.Value)
    queue.Remove(front)
}


Output:
10

Note: In this also the capacity problem arises and to overcome that, there is a need to initialize a different variable and compare the length of the LinkList before every pushback.


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 29 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials