From 6223ffb3ea41addad3eb062db34a1f1f37bfd532 Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Tue, 2 Jan 2018 22:36:24 -0800 Subject: [PATCH] Grow the queue if needed --- queue.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/queue.go b/queue.go index f8d4b48..88c7b3a 100644 --- a/queue.go +++ b/queue.go @@ -1,10 +1,5 @@ package main -import ( - "fmt" - "os" -) - type Queue struct { data []interface{} begin, end int @@ -25,9 +20,8 @@ func (q *Queue) Length() int { } func (q *Queue) Enqueue(v interface{}) { - if q.length == 256 { - fmt.Println("Queue Full") - os.Exit(1) + if q.length == q.size { + q.grow() } q.end++ @@ -78,3 +72,17 @@ func (q *Queue) Element(i int) interface{} { 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 +}