Quick actions

cmd+k|ctrl+k

Navigation

Languages

Queue LinkedListImpl

Snippet info

Language

Scala

Visibility

public

Author

devernul

Created

2018-12-08T18:33:05Z

Updated

2018-12-08T18:33:05Z

object Main extends App {
    case class NodeMut(var item: String, var next: NodeMut)
    
    class QueueOfString {
        var first, last: NodeMut = null
        def isEmpty: Boolean = first == null
    
        def enqueue(item: String): Unit = {
          var oldLast = last
          last = NodeMut(item, null)
          if (isEmpty) first = last
          else oldLast.next = last
        }
    
        def dequeue(): String = {
          val res = first.item
          first = first.next
          if (isEmpty) last = null
          res
        }
      }
      
  val l = new QueueOfString
  for (n <- 0 until 100) {
    l.enqueue(n.toString)
  }

  for (n <- 0 until 100) {
    println(l.dequeue())
  }
}
INFO