Skip to content

Instantly share code, notes, and snippets.

@maxclav
Last active July 24, 2023 16:42
Show Gist options
  • Save maxclav/d00a2b1b101c1f4345944da76b792ce0 to your computer and use it in GitHub Desktop.
Save maxclav/d00a2b1b101c1f4345944da76b792ce0 to your computer and use it in GitHub Desktop.
Queue Data Structure in Go (GoLang)
package queue
// Queue represents a basic queue data structure.
type Queue struct {
items []any
}
// Enqueue adds an item to the end of the queue.
func (q *Queue) Enqueue(item any) {
q.items = append(q.items, item)
}
// Dequeue removes and returns the item from the front of the queue.
// It also returns a boolean value indicating whether the queue is empty.
func (q *Queue) Dequeue() (any, bool) {
if len(q.items) == 0 {
return nil, false
}
item := q.items[0]
q.items = q.items[1:]
return item, true
}
// IsEmpty checks if the queue is empty.
func (q *Queue) IsEmpty() bool {
return len(q.items) == 0
}
// Size returns the number of elements in the queue.
func (q *Queue) Size() int {
return len(q.items)
}
// Front returns the item at the front of the queue without removing it.
func (q *Queue) Front() (any, bool) {
if len(q.items) == 0 {
return nil, false
}
return q.items[0], true
}
@maxclav
Copy link
Author

maxclav commented Jul 24, 2023

template-queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment