Friday, July 29, 2022
HomeWordPress DevelopmentQueue in Go Language - GeeksforGeeks

Queue in Go Language – GeeksforGeeks


A queue is a linear construction that follows a specific order by which the operations are carried out. The order is First In First Out (FIFO)

Now in case you are acquainted with different programming languages like C++, Java, and Python then there are inbuilt queue libraries that can be utilized for the implementation of queues, however such will not be the case within the case of Golang. Even in case you are not acquainted with these then simply know that Golang doesn’t present an inbuilt queue construction.

The best way to implement Queue in Go Language?

There are numerous methods to implement queues in Golang utilizing different Knowledge buildings as:

  1. Utilizing Slices 
  2. Utilizing Constructions
  3. Utilizing LinkList

1. Implement Queue Utilizing Slices in Go Language:

Implementing queue utilizing a easy slice by which enqueueing and dequeuing operations are accomplished utilizing capabilities. and Underflow(queue is empty) is checked throughout dequeuing operation.

Go

package deal important

  

import "fmt"

  

func enqueue(queue []int, component int) []int {

queue = append(queue, component)

fmt.Println("Enqueued:", component)

return queue

}

  

func dequeue(queue []int) (int, []int) {

component := queue[0]

if len(queue) == 1 {

 var tmp = []int{}

 return component, tmp

  

}

  

return component, queue[1:]

}

  

func important() {

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 eradicating", 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 eradicating 10  : [20 30]
Enqueued: 40
After pushing 40  [20 30 40]

Word: On this, the issue is we can’t outline the scale or capability of the queue. Nonetheless, it may be accomplished by defining the queue as make([]int, 0, 10) the place the third parameter determines capability however the issue arises when capability dynamically will increase in an overflow situation.

2. Utilizing Constructions:

To beat the issue within the earlier one, use Constructions as an alternative which encompass 

  • Parts i.e. queue Parts
  • Dimension i.e. Capability of 

Use Tips to instantly change the queue with out returning it each time and, examine for each overflow and underflow situations:

Go

package deal important

  

import (

    "errors"

    "fmt"

)

  

sort Queue struct {

    Parts []int

    Dimension     int

}

  

func (q *Queue) Enqueue(elem int) {

    if q.GetLength() == q.Dimension {

        fmt.Println("Overflow")

        return

    }

    q.Parts = append(q.Parts, elem)

}

  

func (q *Queue) Dequeue() int {

    if q.IsEmpty() {

        fmt.Println("UnderFlow")

        return 0

    }

    component := q.Parts[0]

    if q.GetLength() == 1 {

        q.Parts = nil

        return component

    }

    q.Parts = q.Parts[1:]

    return component

}

  

func (q *Queue) GetLength() int {

    return len(q.Parts)

}

  

func (q *Queue) IsEmpty() bool {

    return len(q.Parts) == 0

}

  

func (q *Queue) Peek() (int, error) {

    if q.IsEmpty() {

        return 0, errors.New("empty queue")

    }

    return q.Parts[0], nil

}

  

func important() {

    queue := Queue{Dimension: 3}

    fmt.Println(queue.Parts)

    queue.Enqueue(1)

    fmt.Println(queue.Parts)

    queue.Enqueue(2)

    fmt.Println(queue.Parts)

    queue.Enqueue(3)

    fmt.Println(queue.Parts)

    queue.Enqueue(5)

    fmt.Println(queue.Parts)

    elem := queue.Dequeue()

    fmt.Println(elem)

    fmt.Println(queue.Parts)

    queue.Enqueue(9)

    fmt.Println(queue.Parts)

    elem = queue.Dequeue()

    fmt.Println(elem)

    fmt.Println(queue.Parts)

  

}

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

Word: We used to check the size of components to the scale(Capability outlined) of queue construction which is extra good to make use of.

3. Utilizing LinkList:

Go

package deal important

import "container/checklist"

import "fmt"

  

func important() {

    

    queue := checklist.New()

  

    

    queue.PushBack(10)

    queue.PushBack(20)

    queue.PushBack(30)

  

    

    entrance:=queue.Entrance()

    fmt.Println(entrance.Worth)

    queue.Take away(entrance)

}

Output:
10

Word: On this additionally the capability drawback arises and to beat that, there’s a have to initialize a distinct variable and evaluate the size of the LinkList earlier than each pushback.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments