Golang implements queue

WBOY
Release: 2023-05-16 17:01:08
Original
1585 people have browsed it

Queue is a first-in-first-out (FIFO) data structure that is often used to solve various problems in computer programs. In the Go language, you can use the container package in the standard library to implement the queue data structure.

Creating a queue

To create a queue, we need to use the list library in the container package to create a list and add elements to it:

package main

import (
    "container/list"
    "fmt"
)

func main() {
    // 创建一个新的列表
    q := list.New()

    // 向列表中添加元素
    q.PushBack("one")
    q.PushBack("two")
    q.PushBack("three")

    // 遍历列表并打印元素
    for e := q.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
}
Copy after login

In the above code , we first imported the container/list package, and then used the list.New() function to create a new list. Next, three elements were added to the queue using the q.PushBack() method, and a for loop was used to iterate over the list and print the elements.

Queue operations

In addition to adding elements, queues can also perform other operations. Here are some common examples of queue operations:

package main

import (
    "container/list"
    "fmt"
)

func main() {
    // 创建一个新的列表
    q := list.New()

    // 向列表中添加元素
    q.PushBack("one")
    q.PushBack("two")
    q.PushBack("three")

    // 遍历列表并打印元素
    for e := q.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }

    // 弹出最前面的元素
    q.Remove(q.Front())

    // 在最前面添加一个元素
    q.PushFront("zero")

    // 遍历列表并打印元素
    for e := q.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
}
Copy after login

In the above example, we have added three elements and used a for loop to iterate over the list and print the elements. Next, we remove the front element using q.Remove(q.Front()) and add a new element to the front using q.PushFront("zero") . Finally, we used the for loop again to iterate through the list and print the elements.

Implementing a Queue structure

If you want to encapsulate the queue into a structure, you can use the following code to create a Queue structure:

package main

import (
    "container/list"
)

// 队列结构体
type Queue struct {
    items *list.List
}

// 初始化队列
func NewQueue() *Queue {
    return &Queue{list.New()}
}

// 将元素插入队列尾部
func (q *Queue) Enqueue(value interface{}) {
    q.items.PushBack(value)
}

// 从队列头部移除元素
func (q *Queue) Dequeue() interface{} {
    if q.items.Len() == 0 {
        return nil  // 空队列
    }
    front := q.items.Front()
    q.items.Remove(front)
    return front.Value
}

// 返回队列的长度
func (q *Queue) Len() int {
    return q.items.Len()
}

// 判断队列是否为空
func (q *Queue) IsEmpty() bool {
    return q.Len() == 0
}
Copy after login

In the above In the code, we created a Queue structure and defined four methods: Enqueue, Dequeue, Len and IsEmpty. Enqueue inserts elements at the end of the queue, Dequeue removes elements from the head of the queue, Len returns the length of the queue, and IsEmpty determines whether the queue is empty.

Using the Queue structure

The following is a sample code for using the Queue structure:

package main

import (
    "fmt"
)

func main() {
    q := NewQueue()

    q.Enqueue("one")
    q.Enqueue("two")
    q.Enqueue("three")

    fmt.Println(q.Len(), q.IsEmpty())

    fmt.Println(q.Dequeue())
    fmt.Println(q.Dequeue())

    fmt.Println(q.Len(), q.IsEmpty())

    q.Enqueue("four")

    fmt.Println(q.Len(), q.IsEmpty())

    fmt.Println(q.Dequeue())
    fmt.Println(q.Dequeue())

    fmt.Println(q.Len(), q.IsEmpty())
}
Copy after login

In the above code, we first create a new Queue object q, And use the Enqueue method to add three elements to the queue. We then use the Len and IsEmpty methods to check the length of the queue and whether it is empty, and remove the first two elements using the Dequeue method respectively. Next, we use the Len and IsEmpty methods again to check the length of the queue and whether it is empty, and add a new element "four" to the queue. Finally, we use the Dequeue method again to remove the first two elements and use Len and IsEmpty to check if the queue is empty.

We can see that with the help of the Queue structure, we can implement the queue data structure clearly and concisely.

The above is the detailed content of Golang implements queue. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template