golang使用数组实现 数据结构队列

代码如下:

/**
 * @Author: lena
 * @Date: 2021/9/23 16:32
 * @Description: 用数组实现队列
 * @Version: 1.0.0
 */

package data_structure

import (
    "errors"
)

type queue struct {
    start int
    end int
    size int    // 数组大小
    arr []int   // 用数组模拟队列
}

// 创建一个队列
func NewQueue(size int) *queue {
    return &queue{
        start: 0,
        end: 0,
        size: size+1,   // 由于判断队满队空,需要牺牲掉一个空间,所以为了能够存储size个元素,数组大小应该是size+1
        arr: make([]int,size+1),
    }
}

// 判断队列是否满:(end+1)%size == start
func (this *queue) isFull() bool {
    return (this.end+1)%this.size == this.start
}

// 判断队列是否为空
func (this *queue) isEmpty() bool {
    return this.end == this.start
}

// 出队
func (this *queue) pop() (int,error) {
    if this.isEmpty() {
        return 0,errors.New("queue is empty")
    }
    data := this.arr[this.start]
    if this.start == this.size {
        this.start = 0
    } else {
        this.start++
    }
    return data,nil
}

// 入队
func (this *queue) put(data int) (bool,error) {
    if this.isFull() {
        return false,errors.New("queue is full")
    }
    // 元素入队
    this.arr[this.end]=data
    // end后移
    if this.end == this.size-1 {
        this.end=0
    } else {
        this.end++
    }
    return true,nil
}

// 判断当前队列中有几个元素
func (this *queue) num() int {
    if this.isEmpty() {
        return 0
    }
    if this.isFull() {
        return this.size-1
    }
    if this.start > this.end {
        return this.size-(this.start-this.end)
    } else {
        return this.end-this.start
    }
}

// 遍历队列
/*func (this *queue) list() {
    total := this.num()
    //fmt.Println("total =",total)
    index := this.start
    for total>0 {
        if index > this.size-1 {
            index=0
        }
        fmt.Printf("%d,",this.arr[index])
        index++
        total--
    }
    fmt.Println()
}*/

// 遍历队列:以数组形式返回
func (this *queue) list() []int {
    total := this.num()
    res:=make([]int,total)
    index := this.start
    i:=0
    for total>0 {
        if index > this.size-1 {
            index=0
        }
        res[i]=this.arr[index]
        index++
        total--
        i++
    }
    return res
}

测试代码如下:

/**
 * @Author: lena
 * @Date: 2021/9/23 20:55
 * @Description: queue_test.go
 * @Version: 1.0.0
 */

package data_structure

import (
    "fmt"
    "reflect"
    "testing"
)

func TestIsEmpty(t *testing.T) {
    queue := NewQueue(5)
    if !reflect.DeepEqual(queue.isEmpty(),true) {
        t.Errorf("false")
    }
    queue.put(1)
    if !reflect.DeepEqual(queue.isEmpty(),false) {
        t.Errorf("false")
    }
}

func TestIsFull(t *testing.T) {
    queue := NewQueue(5)
    queue.put(1)
    queue.put(2)
    queue.put(3)
    queue.put(4)
    queue.put(5)
    if !reflect.DeepEqual(queue.isFull(),true) {
        t.Errorf("false")
    }
}

func TestList(t *testing.T) {
    queue := NewQueue(5)
    queue.put(1)
    queue.put(2)
    queue.put(3)
    queue.put(4)
    queue.put(5)
    expert := []int{1,2,3,4,5}
    res := queue.list()
    if !reflect.DeepEqual(res,expert) {
        t.Errorf("result is %d\n",res)
    }
}

func TestPop(t *testing.T) {
    queue := NewQueue(5)
    queue.put(1)
    queue.put(2)
    queue.put(3)
    queue.put(4)
    queue.put(5)
    tests:=[]int{1,2,3,4,5}
    for _, ts := range tests {
        t.Run("pop", func(t *testing.T) {
            res,_ := queue.pop()
            if !reflect.DeepEqual(res,ts) {
                t.Errorf("result is %d,not %d\n",res,ts)
            }
        })
    }
    if !reflect.DeepEqual(queue.isEmpty(),true) {
        t.Errorf("false")
    }
    if !reflect.DeepEqual(queue.isFull(),false) {
        t.Errorf("false")
    }
}

func TestQueue(t *testing.T) {
    queue := NewQueue(5)
    queue.put(1)    //[1]
    queue.put(2)    //[1,2]
    queue.put(3)    //[1,2,3]
    //queue.list()    // [1,2,3]
    queue.pop()          //[0,2,3]
    queue.put(4)    //[0,2,3,4,0]
    queue.put(5)    //[0,2,3,4,5]
    //queue.list()    // [2,3,4,5]
    queue.pop()          //[0,0,3,4,5]
    queue.put(7)    //[0,0,3,4,5,7]
    queue.put(8)    //[8,0,3,4,5,7]
    expert := []int{3,4,5,7,8}
    res := queue.list()
    if !reflect.DeepEqual(res,expert) {
        t.Errorf("result is %d\n",res)
    }
    flag, err := queue.put(9)
    fmt.Println("err:",err) // err: queue is full
    if !reflect.DeepEqual(flag,false) {
        t.Errorf("error")
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值