Skip to content

Instantly share code, notes, and snippets.

@TheAlchemistKE
Last active June 6, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAlchemistKE/86ebb2de492ed751624ddee12dff00d2 to your computer and use it in GitHub Desktop.
Save TheAlchemistKE/86ebb2de492ed751624ddee12dff00d2 to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Queue struct {
items []interface{}
}
func (q *Queue) Enqueue(item interface{}) {
q.items = append(q.items, item)
}
func (q *Queue) Dequeue() interface{} {
if !q.IsEmpty() {
item := q.items[0]
q.items = q.items[1:]
return item
}
return nil
}
func (q *Queue) IsEmpty() bool {
return len(q.items) == 0
}
func (q *Queue) Size() int {
return len(q.items)
}
func main() {
queue := Queue{}
queue.Enqueue("Alice")
queue.Enqueue("Bob")
queue.Enqueue("Charlie")
fmt.Println(queue.Dequeue()) // Output: Alice
fmt.Println(queue.Dequeue()) // Output: Bob
fmt.Println(queue.Size()) // Output: 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment