为了账号安全,请及时绑定邮箱和手机立即绑定

go语言数据结构 线性表之顺序表

标签:
Go 数据结构

data_struct/squeue_list.go

package data_struct

import (
    "fmt"
    "testing"
)

func TestMakeSqueueList(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    if q.GetLength() != 0 {
        t.Error("初始长度应为0,但现在是%d", q.GetLength())
    }
    if !q.IsEmpty() {
        t.Error("初始状态应该为空")
    }
    if q.IsFull() {
        t.Error("初始状态不应该为满")
    }
}
func TestSqueueList_GetIndex(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    _, ok := q.GetIndex("nil")
    if ok {
        t.Error("初始状态不应该找到任何内容")
    }
}
func TestSqueueList_Insert(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 10)
    q.Insert(0, 12)
    if q.GetLength() != 2 {
        t.Error("此时表长度应为2,但确是%d", q.GetLength())
    }
    if v, _ := q.GetElement(int64(0)); v != 12 {
        t.Errorf("此时表第一个元素应为12,但确是%s", v)
    }
    if v, _ := q.GetElement(int64(1)); v != 10 {
        t.Errorf("此时表第二个元素应为10,但确是%s", v)
    }
    if v, ok := q.Delete(0); ok {
        if q.GetLength() != 1 {
            t.Error("此时表长度应为1,但确是%d", q.GetLength())
        }
        if v != 12 {
            t.Errorf("刚刚删除的元素应为12,但确是%s", v)
        }
    }
}
func TestSqueueList_GetElement(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 3)
    q.Insert(1, 5)
    q.Insert(2, 7)
    q.Insert(3, 8)
    fmt.Print("顺序表的内容:")
    q.Traverse(func(node interface{}) {
        fmt.Printf("%v", node)
    })

    if v, _ := q.GetElement(1); v != 5 {
        t.Errorf("表中1号位置的值应为5,得到的确是%s", v)
    }
    if v, _ := q.GetIndex(3); v != 0 {
        t.Errorf("3在表中的位置为0,GetIndex得到的确是%s", v)
    }
    if v, _ := q.ProviElem(5); v != 3 {
        t.Errorf("5在表中的前驱为3,ProviElem 得到 %s", v)
    }
    if v, _ := q.NextElem(5); v != 7 {
        t.Errorf("5在表中的后驱为7,NextElem 得到 %s", v)
    }
    if v, ok := q.NextElem(8); ok {
        t.Errorf("8在表中没有后驱,NextElem得到%s", v)
    }

    if v, ok := q.ProviElem(3); ok {
        t.Errorf("3在表中没有后驱,ProviElem 得到 %s", v)
    }
}
func BenchmarkSqueueList_Insert(b *testing.B) {
    s := int64(1000)
    q := MakeSqueueList(s)
    for v := 0; v < b.N; v++ {
        for i := int64(0); i < s; i++ {
            q.Insert(i, i)
        }
    }
}

func ExampleMakeSqueueList() {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 3)
    q.Insert(1, 5)
    q.Insert(2, 7)  // 3 5 7
    //q.Insert(3, 8)
    q.Insert(1, 2) //插入后,变成 3257
    q.Insert(4, 9) //无法插入
    fmt.Println(q.GetLength())
    var each = func(q SqueueList) {
        q.Traverse(func(node interface{}) {
            fmt.Print(node)
        })
        fmt.Println()
    }
    each(q)
    q.Delete(2)
    each(q)
    q.Clear()
    each(q)
    fmt.Print(q.IsEmpty())
    //output:
    // 4
    // 3257
    // 327
    //
    // true
}

squeue_list_test.go

package data_struct

import (
    "fmt"
    "testing"
)

func TestMakeSqueueList(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    if q.GetLength() != 0 {
        t.Error("初始长度应为0,但现在是%d", q.GetLength())
    }
    if !q.IsEmpty() {
        t.Error("初始状态应该为空")
    }
    if q.IsFull() {
        t.Error("初始状态不应该为满")
    }
}
func TestSqueueList_GetIndex(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    _, ok := q.GetIndex("nil")
    if ok {
        t.Error("初始状态不应该找到任何内容")
    }
}
func TestSqueueList_Insert(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 10)
    q.Insert(0, 12)
    if q.GetLength() != 2 {
        t.Error("此时表长度应为2,但确是%d", q.GetLength())
    }
    if v, _ := q.GetElement(int64(0)); v != 12 {
        t.Errorf("此时表第一个元素应为12,但确是%s", v)
    }
    if v, _ := q.GetElement(int64(1)); v != 10 {
        t.Errorf("此时表第二个元素应为10,但确是%s", v)
    }
    if v, ok := q.Delete(0); ok {
        if q.GetLength() != 1 {
            t.Error("此时表长度应为1,但确是%d", q.GetLength())
        }
        if v != 12 {
            t.Errorf("刚刚删除的元素应为12,但确是%s", v)
        }
    }
}
func TestSqueueList_GetElement(t *testing.T) {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 3)
    q.Insert(1, 5)
    q.Insert(2, 7)
    q.Insert(3, 8)
    fmt.Print("顺序表的内容:")
    q.Traverse(func(node interface{}) {
        fmt.Printf("%v", node)
    })

    if v, _ := q.GetElement(1); v != 5 {
        t.Errorf("表中1号位置的值应为5,得到的确是%s", v)
    }
    if v, _ := q.GetIndex(3); v != 0 {
        t.Errorf("3在表中的位置为0,GetIndex得到的确是%s", v)
    }
    if v, _ := q.ProviElem(5); v != 3 {
        t.Errorf("5在表中的前驱为3,ProviElem 得到 %s", v)
    }
    if v, _ := q.NextElem(5); v != 7 {
        t.Errorf("5在表中的后驱为7,NextElem 得到 %s", v)
    }
    if v, ok := q.NextElem(8); ok {
        t.Errorf("8在表中没有后驱,NextElem得到%s", v)
    }

    if v, ok := q.ProviElem(3); ok {
        t.Errorf("3在表中没有后驱,ProviElem 得到 %s", v)
    }
}
func BenchmarkSqueueList_Insert(b *testing.B) {
    s := int64(1000)
    q := MakeSqueueList(s)
    for v := 0; v < b.N; v++ {
        for i := int64(0); i < s; i++ {
            q.Insert(i, i)
        }
    }
}

func ExampleMakeSqueueList() {
    s := int64(4)
    q := MakeSqueueList(s)
    q.Insert(0, 3)
    q.Insert(1, 5)
    q.Insert(2, 7)
    q.Insert(3, 8)
    q.Insert(1, 2) //插入后,变成 3258
    q.Insert(4, 9) //无法插入
    fmt.Println(q.GetLength())
    var each = func(q SqueueList) {
        q.Traverse(func(node interface{}) {
            fmt.Print(node)
        })
        fmt.Println()
    }
    each(q)
    q.Delete(2)
    each(q)
    q.Clear()
    each(q)
    fmt.Print(q.IsEmpty())
    //output:
    // 4
    // 3578
    // 358
    //
    // true
}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消