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

Go - Creating Stacks

时间:2023-10-08 16:26:06浏览次数:34  
标签:el elements return Creating Stacks func Go Stack stack

Problem: You want to create a stack data structure.


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

 

A stack is a last - in - first - out (LIFO) ordered list. You add elements at the top of the stack and get elements from the top of the stack as well.

Stacks are very important in programming, especially in memory management. They’re also used for creating recursive functions, expression, evaluation, and backtracking.

Here are the functions associated with a stack:
Push
• Add an element to the top of the stack.
Pop
• Remove an element at the top of the stack.
Peek
• Get the element at the top of the stack without removing it.
Size
• Get the size of the stack.
IsEmpty
• Check if the stack is empty.

There are a few ways to implement a stack but here you’ll simply be using a single slice:

type   Stack   struct   { 
      elements   [] any 
}

 

func   ( s   * Stack )   Push ( el   any )   { 
      s . elements   =   append ( s . elements ,   el ) 
}

func   ( s   * Stack )   Pop ()   ( el   any ,   err   error )   { 
      if   s . IsEmpty ()   { 
          err   =   errors . New ( "empty  stack" ) 
          return 
      } 
      el   =   s . elements [ len ( s . elements ) - 1 ] 
      s . elements   =   s . elements [: len ( s . elements ) - 1 ] 
      return 
}

func   ( s   * Stack )   Peek ()   ( el   any ,   err   error )   { 
      if   s . IsEmpty ()   { 
          err   =   errors . New ( "empty  queue" ) 
          return 
      } 
      el   =   s . elements [ len ( s . elements ) - 1 ] 
      return 
}

func   ( s   * Stack )   IsEmpty ()   bool   { 
      return   s . Size ()   ==   0 
} 

func   ( s   * Stack )   Size ()   int   { 
      return   len ( s . elements ) 
}

 

标签:el,elements,return,Creating,Stacks,func,Go,Stack,stack
From: https://www.cnblogs.com/zhangzhihui/p/17749481.html

相关文章

  • Go - Creating Queues
    Problem: Youwanttocreateaqueuedatastructure.Solution: Wrapastructaroundaslice.Createqueuefunctionsonthestruct. Aqueueisafirst-in-first-out(FIFO)orderedlist.Youaddelementsatthebackofthequeueandgetelementsatt......
  • 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......