Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to create a queue, then enqueue and dequeue elements in Go

2 Answers

0 votes
package main

import (
	"errors"
	"fmt"
)

// Queue represents a queue data structure
type Queue struct {
	elements []int // Slice to hold the elements of the queue
}

// Enqueue adds an element to the end of the queue
func (q *Queue) Enqueue(value int) {
	q.elements = append(q.elements, value)
}

// Dequeue removes and returns the element at the first of the queue
func (q *Queue) Dequeue() (int, error) {
	if q.IsEmpty() {
		return 0, errors.New("queue is empty")
	}
	// Get the first element
	first := q.elements[0]
	// Remove the first element by slicing
	q.elements = q.elements[1:]
	
	return first, nil
}

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

// Peek returns the first element without removing it
func (q *Queue) Peek() (int, error) {
	if q.IsEmpty() {
		return 0, errors.New("queue is empty")
	}
	return q.elements[0], nil
}

func main() {
	// Create a new queue
	queue := &Queue{}

	// Enqueue elements
	queue.Enqueue(10)
	queue.Enqueue(20)
	queue.Enqueue(30)
    queue.Enqueue(40)

	fmt.Println("Queue after enqueuing:", queue.elements)

	// Peek at the first element
	first, err := queue.Peek()
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("first element:", first)
	}

	// Dequeue elements
	for !queue.IsEmpty() {
		dequeued, err := queue.Dequeue()
		if err != nil {
			fmt.Println("Error:", err)
		} else {
			fmt.Println("Dequeued:", dequeued)
		}
	}

	if !queue.IsEmpty() {
        _, err = queue.Dequeue()
        if err != nil {
            fmt.Println("Error:", err)
        }
    } else {
        fmt.Println("queue is empty")
    }
}



/*
run:

Queue after enqueuing: [10 20 30 40]
first element: 10
Dequeued: 10
Dequeued: 20
Dequeued: 30
Dequeued: 40
queue is empty

*/



answered Aug 23, 2025 by avibootz
edited Aug 23, 2025 by avibootz
0 votes
package main

import (
	"container/list"
	"fmt"
)

// Queue structure using container/list
type Queue struct {
	items *list.List
}

// Create a new queue
func NewQueue() *Queue {
	return &Queue{items: list.New()}
}

// Enqueue: Add an element to the back of the queue
func (q *Queue) Enqueue(value any) {
	q.items.PushBack(value)
}

// Dequeue: Remove and return the front element of the queue
func (q *Queue) Dequeue() (any, error) {
	front := q.items.Front()
	if front == nil {
		return nil, fmt.Errorf("queue is empty")
	}
	value := q.items.Remove(front)
	return value, nil
}

// IsEmpty: Check if the queue is empty
func (q *Queue) IsEmpty() bool {
	return q.items.Len() == 0
}

// Size: Get the number of elements in the queue
func (q *Queue) Size() int {
	return q.items.Len()
}

// Print: Display all elements in the queue
func (q *Queue) Print() {
    if q.IsEmpty() {
        fmt.Println("Queue is empty.")
        return
    }
    fmt.Print("Queue contents: ")
    for e := q.items.Front(); e != nil; e = e.Next() {
        fmt.Printf("%v ", e.Value)
    }
    fmt.Println()
}

func main() {
    queue := NewQueue()

    queue.Enqueue(10)
    queue.Enqueue(20)
    queue.Enqueue(30)
    queue.Enqueue(40)
    queue.Print()

    var err error 
    
    for !queue.IsEmpty() {
        value, err := queue.Dequeue()
        if err != nil {
            fmt.Println("Error:", err)
            break
        }
        fmt.Println("Dequeued:", value)
    }

    if !queue.IsEmpty() {
        _, err = queue.Dequeue()
        if err != nil {
            fmt.Println("Error:", err)
        }
    } else {
        fmt.Println("queue is empty")
    }
}




/*
run:

Queue contents: 10 20 30 40 
Dequeued: 10
Dequeued: 20
Dequeued: 30
Dequeued: 40
queue is empty

*/

 



answered Aug 23, 2025 by avibootz
edited Aug 23, 2025 by avibootz
...