golang 队列

Posted 学无止境,每天进步一小步

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
1N1001≤N≤100
1x1001≤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 实现简单的 Web 服务器

代码片段 - Golang 实现集合操作

# Java 常用代码片段

# Java 常用代码片段

json [Golang] golang #golang #snippets中有用的片段

java golang oop 2文章片段