zoukankan      html  css  js  c++  java
  • Go语言(Golang)单链表

    package main

    import (
        "fmt"
    )

    type LinkNode struct {
        no int //编号
        name string //姓名
        next *LinkNode
    }

    //直接在队尾插入
    func InsertNode(head *LinkNode, newNode *LinkNode)  {
        
        temp := head
        for{
            if temp.next == nil {
                break
            }
            temp = temp.next
        }

        temp.next = newNode
    }

    //按编号no从小到大顺序插入
    func InsertNode2(head *LinkNode, newNode *LinkNode)  {
        temp := head
        for{
            if temp.next == nil {
                break
            }else if temp.next.no >= newNode.no {
                break
            }
            temp = temp.next
        }
        newNode.next = temp.next
        temp.next = newNode
    }

    //删除队列
    func DelNode(head *LinkNode, id int) {
        temp := head

        if temp.next == nil {
            fmt.Println("队列为空!")
            return
        }

        for {
            if temp.next == nil {
                break
            } else if temp.next.no == id {
                temp.next = temp.next.next
                break
            }
            temp = temp.next
        }
    }

    //显示队列
    func ShowNode(head *LinkNode) {
        temp := head
        
        if temp.next == nil {
            return
        }
        for {
            fmt.Printf("%d:%s\t",temp.next.no,temp.next.name)
            temp = temp.next
            if temp.next == nil {
                break
            }
        }
    }

    func main() {

        head := &LinkNode{}

        newNode1 := &LinkNode {
            no : 1,
            name : "a",
        }
        newNode2 := &LinkNode {
            no : 2,
            name : "b",
        }
        newNode3 := &LinkNode {
            no : 3,
            name : "c",
        }
        InsertNode2(head, newNode2)
        InsertNode2(head, newNode1)
        InsertNode2(head, newNode3)
        ShowNode(head)
    }
  • 相关阅读:
    join
    runlevel 运行级别
    腾讯一shell试题.
    awk grep sed 的一些问题
    while read line do done < file
    awk 中 RS,ORS,FS,OFS 区别与联系
    节选
    rpm -qa -qc 查询安装过的软件
    css实现两端对齐
    JS表单验证
  • 原文地址:https://www.cnblogs.com/HouZhenglan/p/10065784.html
Copyright © 2011-2022 走看看