generic queue

This commit is contained in:
Gregory 2021-12-15 23:35:58 +02:00
parent 6fe973096e
commit cd3878d698
3 changed files with 19 additions and 24 deletions

View file

@ -1,37 +1,32 @@
package queue
// "Generic" item.
// When type parameters will land this type became
// type Item[T any] T
type Item interface{}
type Queue interface {
Enqueue(Item)
Dequeue() Item
type Queue[T any] interface {
Enqueue(T)
Dequeue() T
Size() int
IsEmpty() bool
}
// We use linked list as internal data structure
// to get O(1) speed for push and pop opeartions
type node struct {
type node[Item any] struct {
item Item
next *node
next *node[Item]
}
type queue struct {
type queue[OfType any] struct {
size int
head *node
tail *node
head *node[OfType]
tail *node[OfType]
}
func NewQueue() Queue {
return &queue{}
func NewQueue[OfType any]() Queue[OfType] {
return &queue[OfType]{}
}
func (q *queue) Enqueue(item Item) {
func (q *queue[T]) Enqueue(item T) {
oldTail := q.tail
q.tail = &node{item: item, next: nil}
q.tail = &node[T]{item: item, next: nil}
if q.IsEmpty() {
q.head = q.tail
} else {
@ -40,7 +35,7 @@ func (q *queue) Enqueue(item Item) {
q.size++
}
func (q *queue) Dequeue() Item {
func (q *queue[T]) Dequeue() T {
first := q.head
q.head = q.head.next
if q.IsEmpty() {
@ -50,10 +45,10 @@ func (q *queue) Dequeue() Item {
return first.item
}
func (q *queue) Size() int {
func (q *queue[T]) Size() int {
return q.size
}
func (q *queue) IsEmpty() bool {
func (q *queue[T]) IsEmpty() bool {
return q.head == nil
}

View file

@ -6,13 +6,13 @@ import (
)
func TestSimple(t *testing.T) {
queue := NewQueue()
queue := NewQueue[int]()
queue.Enqueue(10)
queue.Enqueue(20)
queue.Enqueue(30)
first, second := queue.Dequeue().(int), queue.Dequeue().(int)
first, second := queue.Dequeue(), queue.Dequeue()
if first != 10 && second != 20 {
log.Fatal("wrong order")
@ -20,7 +20,7 @@ func TestSimple(t *testing.T) {
}
func TestSize(t *testing.T) {
queue := NewQueue()
queue := NewQueue[int]()
if queue.Size() != 0 {
log.Fatal("empty queue should have size 0")

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/fotonmoton/algorithms
go 1.16
go 1.18