Go数据结构:队列

A queue is a an ordered collection of items following the First-In-First-Out (FIFO) principle. We add items from one end of the queue, and we retrieve items from the other end.

队列是遵循先进先出(FIFO)原则的项目的有序集合。 我们从队列的一端添加项目,而从另一端检索项目。

Queue applications have all sorts of uses, which can be easily inferred from real-world queues. It’s not hard to imagine a queue representation of a supermarket line or the queue to get into a concert hall.

队列应用程序具有各种用途,可以从实际队列中轻松推断出这些用途。 不难想象,超市的排队队列或进入音乐厅的队列。

实作 (Implementation)

Internally the queue will be represented with a slice type, and I’ll expose the

在内部,队列将以slice类型表示,而我将公开

  • New()

    New()

  • Enqueue()

    Enqueue()

  • Dequeue()

    Dequeue()

  • Front()

    Front()

  • IsEmpty()

    IsEmpty()

  • Size()

    Size()

methods.

方法。

New() serves as the constructor, which initializes the internal slice when we start using it.

New()用作构造函数,当我们开始使用它时会初始化内部切片。

I’ll create an ItemQueue generic type, concurrency safe, that can generate queues containing any type by using genny, to create a type-specific queue implementation, encapsulating the actual value-specific data structure containing the data.

我将创建一个ItemQueue泛型类型,并发安全的,可以生成包含任何类型的使用队列genny ,创建一个特定类型的队列实现,封装包含数据的实际值特定的数据结构。

// Package queue creates a ItemQueue data structure for the Item type
package queue

import (
    "sync"

    "github.com/cheekybits/genny/generic"
)

// Item the type of the queue
type Item generic.Type

// ItemQueue the queue of Items
type ItemQueue struct {
    items []Item
    lock  sync.RWMutex
}

// New creates a new ItemQueue
func (s *ItemQueue) New() *ItemQueue {
    s.items = []Item{}
    return s
}

// Enqueue adds an Item to the end of the queue
func (s *ItemQueue) Enqueue(t Item) {
    s.lock.Lock()
    s.items = append(s.items, t)
    s.lock.Unlock()
}

// Dequeue removes an Item from the start of the queue
func (s *ItemQueue) Dequeue() *Item {
    s.lock.Lock()
    item := s.items[0]
    s.items = s.items[1:len(s.items)]
    s.lock.Unlock()
    return &item
}

// Front returns the item next in the queue, without removing it
func (s *ItemQueue) Front() *Item {
    s.lock.RLock()
    item := s.items[0]
    s.lock.RUnlock()
    return &item
}

// IsEmpty returns true if the queue is empty
func (s *ItemQueue) IsEmpty() bool {
    return len(s.items) == 0
}

// Size returns the number of Items in the queue
func (s *ItemQueue) Size() int {
    return len(s.items)
}

测验 (Tests)

The tests describe the usage of the above implementation.

这些测试描述了上述实现的用法。

package queue

import (
    "testing"
)

var s ItemQueue

func initQueue() *ItemQueue {
    if s.items == nil {
        s = ItemQueue{}
        s.New()
    }
    return &s
}

func TestEnqueue(t *testing.T) {
    s := initQueue()
    s.Enqueue(1)
    s.Enqueue(2)
    s.Enqueue(3)

    if size := s.Size(); size != 3 {
        t.Errorf("wrong count, expected 3 and got %d", size)
    }
}

func TestDequeue(t *testing.T) {
    s.Enqueue(1)
	s.Enqueue(1)
	s.Enqueue(1)
    s.Dequeue()
    if size := len(s.items); size != 2 {
        t.Errorf("wrong count, expected 2 and got %d", size)
    }

    s.Dequeue()
    s.Dequeue()
    if size := len(s.items); size != 0 {
        t.Errorf("wrong count, expected 0 and got %d", size)
    }

    if !s.IsEmpty() {
        t.Errorf("IsEmpty should return true")
    }
}

创建具体的队列数据结构 (Creating a concrete queue data structure)

You can use this generic implemenation to generate type-specific queues, using

您可以使用以下通用实现来生成特定于类型的队列,方法是使用

//generate a `IntQueue` queue of `int` values
genny -in queue.go -out queue-int.go gen "Item=int"

//generate a `StringQueue` queue of `string` values
genny -in queue.go -out queue-string.go gen "Item=string"

翻译自: https://flaviocopes.com/golang-data-structure-queue/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值