数据结构--队列(链表实现)

Yuan_sr

队列的一种链表简单实现

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)
}
阅读 135
7 声望
4 粉丝
0 条评论
你知道吗?

7 声望
4 粉丝
宣传栏