Grow the queue if needed

This commit is contained in:
Kevin Cotugno 2018-01-02 22:36:24 -08:00
parent 4291bfb210
commit 6223ffb3ea

View File

@ -1,10 +1,5 @@
package main package main
import (
"fmt"
"os"
)
type Queue struct { type Queue struct {
data []interface{} data []interface{}
begin, end int begin, end int
@ -25,9 +20,8 @@ func (q *Queue) Length() int {
} }
func (q *Queue) Enqueue(v interface{}) { func (q *Queue) Enqueue(v interface{}) {
if q.length == 256 { if q.length == q.size {
fmt.Println("Queue Full") q.grow()
os.Exit(1)
} }
q.end++ q.end++
@ -78,3 +72,17 @@ func (q *Queue) Element(i int) interface{} {
return v return v
} }
func (q *Queue) grow() {
size := q.size * 2
tmp := make([]interface{}, size)
for i := 0; i < q.length; i++ {
tmp[i] = q.Element(i)
}
q.data = tmp
q.size = size
q.begin = 0
q.end = q.length - 1
}