golang用slice、list当queue
背景
golang没有queue这种类型,不过可以用slice、list模拟
slice当queue
声明队列
var queue []int 入队一个元素:
queue = append(queue, ) 出队一个元素:
if len(queue) > {
queue = queue[:]
}
问题:当不断入队列时,需不停的扩容
list当queue
初始化一个队里:
queue := list.New() 入队一个元素:
queue.PushBack() 出队一个元素:
if queue.Len() > {
queue.Remove(queue.Front())
}
实例:层次遍历二叉树

list题解
func levelOrderBottom(root *TreeNode) [][]int {
var result [][]int
if root == nil {
return result
}
queue := list.New()
queue.PushBack(root)
for queue.Len() > {
curlen := queue.Len()
var curList []int
for i:= ; i < curlen; i++ {
curTree := queue.Remove(queue.Front()).(*TreeNode)
curList = append(curList, curTree.Val)
if curTree.Left != nil {
queue.PushBack(curTree.Left)
}
if curTree.Right != nil {
queue.PushBack(curTree.Right)
}
}
result = append([][]int{curList}, result...)
}
return result
}
list用法
type Element
func (e *Element) Next() *Element
func (e *Element) Prev() *Element
type List
func New() *List
func (l *List) Back() *Element // 最后一个元素
func (l *List) Front() *Element // 第一个元素
func (l *List) Init() *List // 链表初始化
func (l *List) InsertAfter(v interface{}, mark *Element) *Element // 在某个元素后插入
func (l *List) InsertBefore(v interface{}, mark *Element) *Element // 在某个元素前插入
func (l *List) Len() int // 在链表长度
func (l *List) MoveAfter(e, mark *Element) // 把e元素移动到mark之后
func (l *List) MoveBefore(e, mark *Element) // 把e元素移动到mark之前
func (l *List) MoveToBack(e *Element) // 把e元素移动到队列最后
func (l *List) MoveToFront(e *Element) // 把e元素移动到队列最头部
func (l *List) PushBack(v interface{}) *Element // 在队列最后插入元素
func (l *List) PushBackList(other *List) // 在队列最后插入接上新队列
func (l *List) PushFront(v interface{}) *Element // 在队列头部插入元素
func (l *List) PushFrontList(other *List) // 在队列头部插入接上新队列
func (l *List) Remove(e *Element) interface{} // 删除某个元素
举例
func listTest() {
queue := list.New()
queue.PushBack()
queue.PushBack()
fmt.Println(queue.Front())
fmt.Println(queue.Front().Value)
fmt.Println(queue.Front().Next().Value)
fmt.Println(queue.Back().Value)
fmt.Println(queue.Len())
queue.PushFront()
fmt.Println(queue)
queue2 := list.New()
queue2.PushBack()
queue2.PushBack()
queue3 := list.New()
queue2.PushBack(-)
queue.PushBackList(queue2)
queue.PushFrontList(queue3)
fmt.Println(queue.Len())
queue.Remove(queue.Front())
fmt.Println(queue.Len())
queue.InsertBefore(, queue.Front())
queue.MoveAfter(queue.Front(), queue.Front().Next())
}
golang用slice、list当queue的更多相关文章
- golang 对slice的深拷贝 copy
测试 slice的地址 copy的时候 发现有问题: package main import "fmt" func main() { nums:=[]int{1,2,3,4,5} ...
- golang:slice切片
一直对slice切片这个概念理解的不是太透彻,之前学习python的就没搞清楚,不过平时就用python写个工具啥的,也没把这个当回事去花时间解决. 最近使用go开发又遇到这个问题,于是打算彻底把这个 ...
- golang array, slice, string笔记
本来想写一篇关于golang io的笔记,但是在学习io之前必须了解array, slice, string概念,因此将在下篇写golang io. array: 数组的长度是该数组类型的一部分, ...
- golang error (slice of unaddressable value)
使用 Golang 将生成的 md5 转化为 string 的过程出现如下编译错误: 错误解析: 值得注意的一点是 func Sum(data []byte) [Size]byte 这个函数返回的 ...
- [Golang]-1 Slice与数组的区别
目录 数组 1.创建数组: 2.数组是值拷贝传递: 切片(slice) 1.首先看看slice的源码结构: 2.slice的创建: 3.slice使用make创建 4.切片作为参数传递 5.Golan ...
- Golang Clearing slice
//first method : slice = nil // second method : slice = slice[0:0] Source page : https://www.socketl ...
- golang切片slice
切片slice是引用类型 len()函数获取元素的个数 cap()获取数组的容量 1.申明方式 (1)var a []int 与数组不同的是他不申明长度(2)s2 := make([]int, 3, ...
- 使用golang的slice来模拟栈
slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进 ...
- golang的slice作为函数参数传值的坑
直接贴代码 func sliceModify(slice []int) { // slice[0] = 88 slice = append(slice, ) } func main() { slice ...
随机推荐
- 1-1SpringBoot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...
- javaScript中this的指向?
javaScript中this对象是在运行时基于函数的执行环境绑定的,在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象. 但在实际中,代码环境复杂,th ...
- 第1节 kafka消息队列:3、4、kafka的安装以及命令行的管理使用
6.kafka的安装 5.1三台机器安装zookeeper 注意:安装zookeeper之前一定要确保三台机器时钟同步 */1 * * * * /usr/sbin/ntpdate us.pool.nt ...
- Maven项目- get请求的乱码问题,使用tamcat7出现乱码的解决方法
get请求的乱码问题: 解决方法: 手动处理编码
- java表单基础
一.表单 基本语法: <form method="表单提交方式(post/get)" action="表单提交地址"> </ ...
- MAC 终端编辑完成后如何保存:
如果是vi,则:Esc 退出编辑模式,输入以下命令: :wq 保存后退出vi,若为 :wq! 则为强制储存后退出(常用):w 保存但不退出(常用):w! 若文件属性为『只读』时,强制写入该档案:q 离 ...
- Redis Cluster 获取主从关系
redis-cli -h 192.168.11.111 -p 6380 -c cluster slots | xargs -n8 | awk '{print $3":"$4&qu ...
- 题解:luogu P1247
大概没你们说得复杂吧...... \(Part\;1\) \(Nim\)游戏 大家都对异或和感到懵逼吧(排除大佬),其实很简单,用\(SG\)函数打表计算即可解决: 抛个板子: void get_sg ...
- R 《回归分析与线性统计模型》page141,5.2
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data141.xlsx") ...
- R 再也不用愁变量太多跑回归太麻烦!R语言循环常用方法总结
在高维数据分析过程中,为了筛选出与目标结局相关的变量,通常会用到回归分析,但是因为自变量较多,往往要进行多次回归.这就是统计编程语言发挥作用的时候了 有些大神们认为超过3次的复制粘贴就可以考虑使用循环 ...