golang 队列
You have to perform NN operations on the queue. The operations are of following type:
E xE x : Enqueue xx in the queue and print the new size of the queue.
DD : Dequeue from the queue and print the element that is deleted and the new size of the queue separated by space. If there is no element in the queue then print −1−1 in place of deleted element.
Constraints:
1≤N≤1001≤N≤100
1≤x≤1001≤x≤100
Format of the input file:
First line : N.
Next N lines : One of the above operations
Format of the output file:
For each enqueue operation print the new size of the queue. And for each dequeue operation print two integers, deleted element (−1, if queue is empty) and the new size of the queue.
该功能就是先进先出,和栈唯一不相同的就是 queue = queue[1:] 把前面的一个元素去掉
package main import "fmt"
var queue []int func main() {
//fmt.Println("Hello World!")
queue = make([]int,0,0)
var inputCount int
fmt.Scanln(&inputCount) var flag string
var value int
var queueLength int
for i:=0;i<inputCount;i++{
fmt.Scanln(&flag,&value)
queueLength = len(queue)
if flag == "E" {
queue = append(queue,value)
queueLength = len(queue)
fmt.Println(queueLength)
}else if flag == "D"{
if queueLength ==0 {
fmt.Println("-1 0")
}else{
exitvalue:=queue[0]
queue = queue[1:]
queueLength = len(queue)
fmt.Println(exitvalue,queueLength)
}
}
}
}
golang 队列的更多相关文章
- 数据结构和算法(Golang实现)(14)常见数据结构-栈和队列
栈和队列 一.栈 Stack 和队列 Queue 我们日常生活中,都需要将物品排列,或者安排事情的先后顺序.更通俗地讲,我们买东西时,人太多的情况下,我们要排队,排队也有先后顺序,有些人早了点来,排完 ...
- golang实现并发爬虫三(用队列调度器实现)
欲看此文,必先可先看: golang实现并发爬虫一(单任务版本爬虫功能) gollang实现并发爬虫二(简单调度器) 上文中的用简单的调度器实现了并发爬虫. 并且,也提到了这种并发爬虫的实现可以提高爬 ...
- 用栈来实现队列的golang实现
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...
- golang 中操作nsq队列数据库
首先先在本地将服务跑起来,我用的是docker-compose ,一句话6666 先新建一个docker-compose.yml version: '2' services: nsqlookupd: ...
- golang实现rabbitmq消息队列失败尝试
在工作中发现,有些时候消息因为某些原因在消费一次后,如果消息失败,这时候不ack,消息就回一直重回队列首部,造成消息拥堵. 如是有了如下思路: 消息进入队列前,header默认有参数 retry_nu ...
- golang数据结构之队列
队列可以用数组或链表实现,遵从先入先出. 目录结构: 在main中调用queue包中的属性和方法,如何调用参考另一篇文章: https://www.cnblogs.com/xiximayou/p/12 ...
- golang数据结构之环形队列
目录结构: circlequeue.go package queue import ( "errors" "fmt" ) //CircleQueue 环型队列 ...
- golang数据结构和算法之QueueLinkedList链表队列
队列和堆栈不一样的地方在于进出顺序: 堆栈是后进先出, 队列是先进先出. QueueLinkedList.go package QueueLinkedList type Node struct { d ...
- golang数据结构和算法之CircularBuffer环形缓冲队列
慢慢练语法和思路, 想说的都在代码及注释里. CircularBuffer package CircularBuffer const arraySize = 10 type CircularBuffe ...
随机推荐
- JAVA9模块化详解(一)——模块化的定义
JAVA9模块化详解 前言 java9已经出来有一段时间了,今天向大家介绍一下java9的一个重要特性--模块化.模块化系统的主要目的如下: 更可靠的配置,通过制定明确的类的依赖关系代替以前那种易错的 ...
- loadrunner 参数存储在data.ws、paralist、globals.h 中区别(参数与变量额区别于使用)
1.如果变量数据只有一个值,可以直接放在data.ws 中 2.如果变量要根据循环取随机值.序列值等(参数存在一组值),放在paralist中 3.如果是申明全局变量,且要在代码中用到参 ...
- C++中指向类的指针
事情缘起是因为上班途中刷到了有个微博,那人说答对这个问题的请发简历. 看了就是关于指向C++类的指针的知识,原代码类似下面这样: class NullPointCall { public: void ...
- opencv批处理提取图像的特征
____________________________________________________________________________________________________ ...
- JAVA入门[5]-初步搭建SpringMVC站点
一.新建Module 1.新建Module,类型如下图所示: 2.为项目添加Spring框架支持,操作步骤如下: 首先在Module右键->Add Framework Support: 2.Sp ...
- System.out.println()的解释
上周面试的时候,面试官让我解释一下输出语句每一个单词是什么意思.当是有点蒙,后来想想这不就是考察对java中常用的的包,类和其中的方法的熟悉程度吗? 我们首先来看看System:这是一个类名,存在于j ...
- weka 通过普通文本转化成arff文件
这个问题来源于我要用weka这个数据挖掘工具,测试时发现我们新建txt文件,输入内容,然后直接改后缀. 这样生成的arff文件不能打开. 究其原因是编码的问题,正确处理方法如下: 新建文本,然后用no ...
- Java方法的概念及使用
方法 将一段逻辑或者功能提取出来,这种提取的形式就是函数 格式 修饰符 返回值类型 函数名(参数列表){ 方法体: return 返回值; } //明确返回值类型---求两个整数的和,确定结果一定是整 ...
- centos6.5 yum update 报错Couldn't resolve host 'centos.ustc.edu.cn'
异常信息 [root@localhost ~]# yum -y update Loaded plugins: fastestmirror, refresh-packagekit, security S ...
- Visual Studio中将打开的代码与类文件进行关联