scala学习笔记(十一):Queue、stack

阅读更多

一、Queue队列是FIFO(先进先出),Queue分可变和不可变(immutable queue and mutable queue)

 

import scala.collection.mutable.Queue
var ints = Queue[Int]()
var fruits = Queue[String]()
var q = Queue[Person]()
scala> val q = Queue(1, 2, 3)
q: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3)

 使用+=、++=、enqueue来添加元素

 

scala> import scala.collection.mutable.Queue
import scala.collection.mutable.Queue

// create an empty queue
scala> var q = new Queue[String]
q: scala.collection.mutable.Queue[String] = Queue()

// add elements to the queue in the usual ways
scala> var q = new Queue[String]
q: scala.collection.mutable.Queue[String] = Queue()

scala> q += "apple"
res0: scala.collection.mutable.Queue[String] = Queue(apple)

scala> q += ("kiwi", "banana")
res1: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana)

scala> q ++= List("cherry", "coconut")
res2: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana, cherry, coconut)

// can also use enqueue
scala> q.enqueue("pineapple")

scala> q
res3: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana, cherry, coconut, pineapple)

 使用dequeue来取出数据,因为是FIFO所以取出的数据是从队列的头部开始,  dequeueFirst, dequeueAll可以根据条件取出数据

 

// take an element from the head of the queue
scala> val next = q.dequeue
next: String = apple

// 'apple' is removed from the queue
scala> q
res0: scala.collection.mutable.Queue[String] = Queue(kiwi, banana, cherry, coconut, pineapple)

// take the next element
scala> val next = q.dequeue
next: String = kiwi

// 'kiwi' is removed from the queue
scala> q
res1: scala.collection.mutable.Queue[String] = Queue(banana, cherry, coconut, pineapple)

  

scala> q.dequeueFirst(_.startsWith("b"))
res2: Option[String] = Some(banana)

scala> q
res3: scala.collection.mutable.Queue[String] = Queue(cherry, coconut, pineapple)

scala> q.dequeueAll(_.length > 6)
res4: scala.collection.mutable.Seq[String] = ArrayBuffer(coconut, pineapple)
11.29. Using a Queue | 373

scala> q
res5: scala.collection.mutable.Queue[String] = Queue(cherry)

 二、stack 栈LIFO(后进先出),这个和大多数语言一样添加数据用push、取出数据pop,stack 也分可变和不可变(immutable stack and mutable stack )

 

import scala.collection.mutable.Stack

var ints = Stack[Int]()
var fruits = Stack[String]()
case class Person(var name: String)
var people = Stack[Person]()

val ints = Stack(1, 2, 3)

 

// create a stack
scala> var fruits = Stack[String]()
fruits: scala.collection.mutable.Stack[String] = Stack()

// add one element at a time
scala> fruits.push("apple")
res0: scala.collection.mutable.Stack[String] = Stack(apple)

scala> fruits.push("banana")
res1: scala.collection.mutable.Stack[String] = Stack(banana, apple)

// add multiple elements
scala> fruits.push("coconut", "orange", "pineapple")
res2: scala.collection.mutable.Stack[String] = Stack(pineapple, orange, coconut, banana, apple)

 

scala> val next = fruits.pop
next: String = pineapple

scala> fruits
res3: scala.collection.mutable.Stack[String] = Stack(orange, coconut, banana, apple)

   因为pop取数据会删除掉stack中的数据,所以我们可以使用top来获取数据

  

scala> fruits.top
res4: String = orange
// 'orange' is still on the top

scala> fruits
res5: scala.collection.mutable.Stack[String] = Stack(orange, coconut, banana, apple)

   因为stack继承Seq,所以可以使用其中的方法

 

scala> fruits.size
res6: Int = 4

scala> fruits.isEmpty
res7: Boolean = false

//清空栈数据
scala> fruits.clear

scala> fruits
res8: scala.collection.mutable.Stack[String] = Stack()

 

 

 

 

 

 

 

 

你可能感兴趣的:(scala学习笔记(十一):Queue、stack)