Compare commits

...
This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.

1 Commits

Author SHA1 Message Date
6223ffb3ea Grow the queue if needed 2018-01-02 22:36:24 -08:00

View File

@ -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
}