首页 > 其他分享 >Go - Creating Queues

Go - Creating Queues

时间:2023-10-08 16:13:46浏览次数:37  
标签:el elements return Creating Queues Queue queue Go any

Problem: You want to create a queue data structure.


Solution: Wrap a struct around a slice. Create queue functions on the struct.

 

A queue is a first - in - first - out (FIFO) ordered list. You add elements at the back of the queue and get elements at the front of the queue.

Queues are one of the simplest data structures. They are commonly used as a buffer or queueing system.

Here are the functions associated with a queue:
Enqueue
• Add an element to the back of the queue.
Dequeue
• Remove an element at the front of the queue.
Peek
• Get the element at the front of the queue without removing it from the queue.
Size
• Get the size of the queue.
IsEmpty
• Check if the queue is empty.

You can easily implement a queue using a slice:

type   Queue   struct   { 
      elements   [] any 
}

In this code you are using any elements so it can be any type.

func   ( q   * Queue )   Enqueue ( el   any )   { 
      q . elements   =   append ( q . elements ,   el ) 
}

func   ( q   * Queue )   Dequeue ()   ( el   any ,   err   error )   { 
      if   q . IsEmpty ()   { 
          err   =   errors . New ( "empty  queue" ) 
          return 
      } 
      el   =   q . elements [ 0 ] 
      q . elements   =   q . elements [ 1 :] 
      return 
}

func   ( q   * Queue )   Peek ()   ( el   any ,   err   error )   { 
      if   q . IsEmpty ()   { 
          err   =   errors . New ( "empty  queue" ) 
          return 
      } 
      el   =   q . elements [ 0 ] 
      return 
}

func   ( q   * Queue )   IsEmpty ()   bool   { 
      return   q . Size ()   ==   0 
} 

func   ( q   * Queue )   Size ()   int   {
     return   len ( q . elements ) 
}

 

标签:el,elements,return,Creating,Queues,Queue,queue,Go,any
From: https://www.cnblogs.com/zhangzhihui/p/17749433.html

相关文章

  • Django模型及Admin
    Django简介什么是DjangoDjango是一个开源的PythonWeb框架,由DjangoSoftwareFoundation开发和维护。Django的目标是让Web开发更加快速、简单和高效,提供了许多内置的功能和库,如ORM、模板引擎、表单处理、认证、管理后台等。1.2Django的优点及特点高效快速:Django提供了许多......
  • Go复合类型之数组类型
    Go复合类型之数组@目录Go复合类型之数组一、数组(Array)介绍1.1基本介绍1.2数组的特点二、数组的声明与初始化2.1数组声明2.2常见的数据类型声明方法2.3数组的初始化方式一:使用初始值列表初始化数组方法二:根据初始值个数自动推断数组长度方法三:通过指定索引值初始化数组三、......
  • Go with Protobuf
    原文在这里。本教程为Go程序员提供了使用Protocolbuffer的基本介绍。本教程使用proto3向Go程序员介绍如何使用protobuf。通过创建一个简单的示例应用程序,它向你展示了如何:在.proto中定义消息格式使用protocolbuffer编译器使用GoprotocolbufferAPI读写消息这并......
  • 02_go语言的变量和常量
    1.内置类型和内置函数1.1内置类型总体上分为四类:其中数字类型主要包括如下,uint8就是byte、int16相当于C语言的short型、int64相当于C语言的long型也可以总体分为值类型、引用类型1.2内置函数1.3值类型和引用类型funcmain(){vara*int*a=100......
  • GO数组解密:从基础到高阶全解
    在本文中,我们深入探讨了Go语言中数组的各个方面。从基础概念、常规操作,到高级技巧和特殊操作,我们通过清晰的解释和具体的Go代码示例为读者提供了全面的指南。无论您是初学者还是经验丰富的开发者,这篇文章都将助您更深入地理解和掌握Go数组的实际应用。关注公众号【TechLeadClou......
  • 04_go语言io流
    1.基本IO接口1.1Reader接口//Reader接口定义typeReaderinterface{Read(p[]byte)(nint,errerror)}Read将len(p)个字节读取到p中。它返回读取的字节数n(0<=n<=len(p))以及遇到的任何错误。即使Read返回的n<len(p),它也会在调用过程中占用le......
  • GO数组解密:从基础到高阶全解
    在本文中,我们深入探讨了Go语言中数组的各个方面。从基础概念、常规操作,到高级技巧和特殊操作,我们通过清晰的解释和具体的Go代码示例为读者提供了全面的指南。无论您是初学者还是经验丰富的开发者,这篇文章都将助您更深入地理解和掌握Go数组的实际应用。关注公众号【TechLeadClo......
  • Go - Sorting Arrays or Slices
    Problem: Youwanttosortelementsinanarrayorslice.Solution: Forint,float64,andstringarraysorslicesyoucanusesort.Ints,sort.Float64s,andsort.Strings.Youcanalsouseacustomcomparatorbyusingsort.Slice.Forstructs,youcan......
  • Go - Making Arrays and Slices Safe for Concurrent Use
    Problem: Youwanttomakearraysandslicessafeforconcurrentusebymultiplegoroutines.Solution: Useamutexfromthesynclibrarytosafeguardthearrayorslice.Lockthearrayorslicebeforemodifyingit,andunlockitaftermodificationsarema......
  • 使用hugo+github搭建免费个人博客
    使用hugo+github搭建免费个人博客前提条件win11电脑一台电脑安装了git电脑安装了hugogithub账号一个个人博客本地搭建初始化一个博客打开cmd窗口,使用hugo新建一个博客工程hugonewsiteblogtest 1下载主题主题官网:themes.gohugo.io在上面找一个主题,我这里找......