Implementing a Circular Queue in Go

 

 

This program demonstrates the implementation of a circular queue using the Go programming language. Circular queues are particularly useful in scenarios where continuous or cyclic usage of the queue is necessary, such as buffering data streams.

Program Code

package main

import (
    "fmt"
    "errors"
)

// CircularQueue structure
type CircularQueue struct {
    items  []int
    head   int
    tail   int
    size   int
    length int
}

// NewCircularQueue creates a new Circular Queue of a given size
func NewCircularQueue(size int) *CircularQueue {
    return &CircularQueue{
        items:  make([]int, size),
        head:   0,
        tail:   0,
        size:   size,
        length: 0,
    }
}

// Enqueue adds an item to the queue
func (q *CircularQueue) Enqueue(item int) error {
    if q.IsFull() {
        return errors.New("queue is full")
    }
    q.items[q.tail] = item
    q.tail = (q.tail + 1) % q.size
    q.length++
    return nil
}

// Dequeue removes and returns the front item of the queue
func (q *CircularQueue) Dequeue() (int, error) {
    if q.IsEmpty() {
        return 0, errors.New("queue is empty")
    }
    item := q.items[q.head]
    q.head = (q.head + 1) % q.size
    q.length--
    return item, nil
}

// IsFull checks if the queue is full
func (q *CircularQueue) IsFull() bool {
    return q.length == q.size
}

// IsEmpty checks if the queue is empty
func (q *CircularQueue) IsEmpty() bool {
    return q.length == 0
}

func main() {
    cq := NewCircularQueue(5)
    fmt.Println("Enqueue: 1, 2, 3, 4, 5")
    for i := 1; i <= 5; i++ {
        err := cq.Enqueue(i)
        if err != nil {
            fmt.Println(err)
        }
    }

    fmt.Println("Dequeue:", cq.Dequeue())
    fmt.Println("Enqueue: 6")
    cq.Enqueue(6)

    fmt.Println("Current Queue:")
    for !cq.IsEmpty() {
        front, _ := cq.Dequeue()
        fmt.Println(front)
    }
}

Explanation

  • Structure: The CircularQueue struct holds the array items which stores the queue elements, pointers head and tail for tracking the front and rear of the queue, and an integer length to keep track of the number of items.
  • Queue Operations:
    • Enqueue adds an item at the tail of the queue and adjusts the tail pointer in a circular manner using modulo arithmetic.
    • Dequeue removes an item from the head of the queue and adjusts the head pointer similarly.
    • Utility functions like IsFull and IsEmpty are used to check the state of the queue.

 

Running the Program

To run this Go program:

  1. Save the code in a file named circularqueue.go.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the file is stored.
  4. Run the program by typing go run circularqueue.go.

The output will demonstrate enqueue and dequeue operations showing how items are managed in a circular fashion within the queue. Adjust the queue operations in the main function to test different scenarios and see how the circular queue handles edge cases, such as wrapping around when the queue is full.

Leave a Reply

Your email address will not be published. Required fields are marked *