关于数据结构:数据结构队列链表实现

40次阅读

共计 694 个字符,预计需要花费 2 分钟才能阅读完成。

队列的一种链表简略实现

package main

import "fmt"

type Queue struct {
    id int
    name string
    next *Queue
}

//Push
func Push(head, node *Queue){
    node.next = head.next
    head.next = node
}

//Pop
func Pop(head *Queue) (node *Queue){
    temp := head
    for {
        if temp.next == nil{return}else if temp.next.next == nil{
            node = temp.next
            temp.next = nil
            break
        }
        temp = temp.next
    }
    return
}

//List
func QList(head *Queue){
    temp := head
    if temp.next == nil{fmt.Println("队列为空")
        return
    }
    for {fmt.Printf("| %d  %s |", temp.next.id, temp.next.name)
        temp = temp.next
        if temp.next == nil{break}
    }
}

func main(){head := &Queue{}
    node1 := &Queue{
        id:   0,
        name: "number1",
        next: nil,
    }
    node2 := &Queue{
        id : 1,
        name: "number2",
        next: nil,
    }
    Push(head, node1)
    Push(head, node2)
    QList(head)
    fmt.Println()
    p := Pop(head)
    fmt.Printf("%d %s\n",p.id, p.name)
    QList(head)
}

正文完
 0