golang基础-结构体、结构体链表前后插入、节点添加删除

前端之家收集整理的这篇文章主要介绍了golang基础-结构体、结构体链表前后插入、节点添加删除前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

结构体定义

struct
用来自定义复杂数据结构
struct里面可以包含多个字段(属性
struct类型可以定义方法,注意和函数的区分
struct类型是值类型
struct类型可以嵌套
Go语言没有class类型,只有struct类型

package main

import (
    "fmt"
)

type Student struct{
    Name string
    Age int 
    score float32
}


func main() {
    var stu Student
    stu.Age = 18
    stu.Name = "aaa"
    stu.score = 100
    //{aaa 18 100}格式
    fmt.Println(stu)
    fmt.Println(stu.Age)
    fmt.Println(&stu.Age)
    fmt.Println(&stu)
    fmt.Println("---------------")

    var stu1 *Student = &Student{
        Age : 20,Name:"hh",}

    fmt.Println(stu1)
    fmt.Println(stu1.Age)
    fmt.Println(*stu1)
    //如下的形式是不行
    // fmt.Println(*stu1.Age)
    fmt.Println(&stu1.Age)
    fmt.Println(&stu1)


    fmt.Println("-----------")
    var stu3 = Student{
        Age:18,Name:"iii",}
    fmt.Println(stu3)

    fmt.Println(&stu3)
    fmt.Println(stu3.Age)
    fmt.Println(&stu3.Age)
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{aaa 18 100}
18
0xc042002750
&{aaa 18 100}
---------------
&{hh 20 0}
20
{hh 20 0}
0xc042002850
0xc042004030
-----------
{iii 18 0}
&{iii 18 0}
18
0xc042002950
PS E:\golang\go_pro\src\safly>

结构体链表定义

链表定义

type Student struct { Name string Next* Student }

每个节点包含下一个节点的地址,这样把所有的节点串起来了,通常把
链表中的第一个节点叫做链表头
我们来看一个例子,就是在结构体,末尾插入一个节点

尾部插入
package main

import (
    "fmt"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头结点
    var head Student
    head.Name = "safly"
    head.Age = 1
    head.score = 100

    //创建一个节点
    var stu1 Student
    stu1.Name = "safly1"
    stu1.Age = 2
    stu1.score = 101
    head.next = &stu1

    //创建一个临时遍历
    var temp *Student = &head
    for temp!= nil{
        fmt.Println(temp)

        temp = temp.next
    }
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
&{safly 1 100 0xc04203df80}
&{safly1 2 101 <nil>}
PS E:\golang\go_pro\src\safly>

以上是在末尾插入 ,我们接下来看在头部插入节点的代码

头部插入

头部插入的第一种方法

package main

import (
    "fmt"
    "math/rand"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头 
    var head *Student = &Student{}
    //或者如下的方式
    // var head1 *Student = new(Student)
    head.Name = "safly"
    head.Age = 1
    head.score = 100

    for i:=0;i<5;i++{
        stu:= Student{
            Name:fmt.Sprint(rand.Intn(100)),Age:rand.Intn(100),score:rand.Float32()*100,}

        stu.next = head
        head = &stu
    }

    //循环输出
    for head!=nil{
        fmt.Println(*head)
        head = head.next
    }
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{62 89 31.805817 0xc042062060}
{0 94 81.36399 0xc042062030}
{25 40 9.696952 0xc042062000}
{59 81 68.682304 0xc04203dfb0}
{81 87 66.45601 0xc04203df80}
{safly 1 100 <nil>}
PS E:\golang\go_pro\src\safly>

我们将上面的代码进行优化:

package main

import (
    "fmt"
    "math/rand"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头结点
    var head *Student = new(Student) 
    //或者如下的方式
    // var head1 *Student = new(Student)
    head.Name = "safly"
    head.Age = 1
    head.score = 100

    inserHead(&head)
    trans(head)
}

/**/
func inserHead(p **Student){
    // var tail = p
    for i:=0;i<5;i++{
        stu:= Student{
            Name:fmt.Sprint(rand.Intn(100)),}

        stu.next = *p
        *p = &stu
    }
}

func trans(p *Student) {
    for p != nil {
        fmt.Println(*p)
        p = p.next
    }
    fmt.Println()
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{62 89 31.805817 0xc04204a2d0}
{0 94 81.36399 0xc04204a2a0}
{25 40 9.696952 0xc04204a270}
{59 81 68.682304 0xc04204a240}
{81 87 66.45601 0xc04204a210}
{safly 1 100 <nil>}

PS E:\golang\go_pro\src\safly>

或者如下的方法也可以

package main

import (
    "fmt"
    "math/rand"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头结点
    var head *Student = new(Student) 
    //或者如下的方式
    // var head1 *Student = new(Student)
    head.Name = "safly"
    head.Age = 1
    head.score = 100



    for i:=0;i<5;i++{
        stu:= Student{
            Name:fmt.Sprint(rand.Intn(100)),}
        stu.next = *(&head)
        *(&head) = &stu
    }
    trans(head)
}


func trans(p *Student) {
    for p != nil {
        fmt.Println(*p)
        p = p.next
    }
    fmt.Println()
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{62 89 31.805817 0xc042062060}
{0 94 81.36399 0xc042062030}
{25 40 9.696952 0xc042062000}
{59 81 68.682304 0xc04203dfb0}
{81 87 66.45601 0xc04203df80}
{safly 1 100 <nil>}

PS E:\golang\go_pro\src\safly>

删除节点

package main

import (
    "fmt"
    "math/rand"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头结点
    var head *Student = new(Student) 
    //或者如下的方式
    // var head1 *Student = new(Student)
    head.Name = "safly"
    head.Age = 1
    head.score = 100


    //添加节点
    for i:=0;i<5;i++{
        stu:= Student{
            Name:fmt.Sprintf("stu%d",i),}
        stu.next = *(&head)
        *(&head) = &stu
    }
    trans(head)

    //删除节点
    delNode(head)
    trans(head)
}
/* 删除节点 */
func delNode(p *Student) {

        var prev *Student = p
        for p != nil {
            if (*p).Name == "stu3" {
                prev.next = p.next
                break
            }
            //如果没有遍历到,往下延迟一个
            prev = p
            p = p.next
        }
}


func trans(p *Student) {
    for p != nil {
        fmt.Println(*p)
        p = p.next
    }
    fmt.Println()
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{stu4 56 30.091187 0xc042062060}
{stu3 25 15.651925 0xc042062030}
{stu2 81 68.682304 0xc042062000}
{stu1 47 43.77142 0xc04203dfb0}
{stu0 81 94.05091 0xc04203df80}
{safly 1 100 <nil>}

{stu4 56 30.091187 0xc042062030}
{stu2 81 68.682304 0xc042062000}
{stu1 47 43.77142 0xc04203dfb0}
{stu0 81 94.05091 0xc04203df80}
{safly 1 100 <nil>}

PS E:\golang\go_pro\src\safly>

添加节点

package main

import (
    "fmt"
    "math/rand"
)

type Student struct{
    Name string
    Age int 
    score float32
    next *Student
}


func main() {
    //创建一个头结点
    var head *Student = new(Student) 
    //或者如下的方式
    // var head1 *Student = new(Student)
    head.Name = "safly"
    head.Age = 1
    head.score = 100


    //添加节点
    for i:=0;i<5;i++{
        stu:= Student{
            Name:fmt.Sprintf("stu%d",Age:rand.Intn(100),score:rand.Float32()*100,}
        stu.next = *(&head)
        *(&head) = &stu
    }
    trans(head)

    var newNode *Student = new(Student)

        newNode.Name = "stu1000"
        newNode.Age = 18
        newNode.score = 100
        addNode(head,newNode)
        trans(head)

}

func addNode(p *Student,newNode *Student) {

        for p != nil {
            if p.Name == "stu2" {
                newNode.next = p.next
                p.next = newNode
                break
            }

            p = p.next
        }
    }

func trans(p *Student) {
    for p != nil {
        fmt.Println(*p)
        p = p.next
    }
    fmt.Println()
}

输出如下:

PS E:\golang\go_pro\src\safly> go run demo.go
{stu4 56 30.091187 0xc04204a2d0}
{stu3 25 15.651925 0xc04204a2a0}
{stu2 81 68.682304 0xc04204a270}
{stu1 47 43.77142 0xc04204a240}
{stu0 81 94.05091 0xc04204a210}
{safly 1 100 <nil>}

{stu4 56 30.091187 0xc04204a2d0}
{stu3 25 15.651925 0xc04204a2a0}
{stu2 81 68.682304 0xc04204a480}
{stu1000 18 100 0xc04204a270}
{stu1 47 43.77142 0xc04204a240}
{stu0 81 94.05091 0xc04204a210}
{safly 1 100 <nil>}

PS E:\golang\go_pro\src\safly>
原文链接:https://www.f2er.com/go/187789.html

猜你在找的Go相关文章