Last active
July 24, 2023 16:42
-
-
Save maxclav/d00a2b1b101c1f4345944da76b792ce0 to your computer and use it in GitHub Desktop.
Queue Data Structure in Go (GoLang)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Author
maxclav
commented
Jul 24, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment