package main
import "fmt"
func main() {
data := []int{6, 3, 1, 7, 5, 8, 9, 2, 4}
head := 0
tail := len(data) - 1
for i := 0; i <= tail; i++ {
fmt.Println(data[head])
if head >= tail {
break
}
data = append(data, data[head+1])
head += 2
tail += 1
}
}
如果使用结构体代替其中内容, 则是如下的代码
package main
import "fmt"
type queue struct {
data []int
head, tail int
}
func main() {
q := queue{
[]int{6, 3, 1, 7, 5, 8, 9, 2, 4},
0,
8,
}
for i := q.head; i <= q.tail; i++ {
fmt.Println(q.data[q.head])
if q.head >= q.tail {
break
}
q.data = append(q.data, q.data[q.head+1])
q.head += 2
q.tail += 1
}
}