Sha256: ad6f06d9b13c11d2d1cb7eadf1fe37bf98a007594c32b53a23c0e138f73a6dca

Contents?: true

Size: 1.61 KB

Versions: 396

Compression:

Stored size: 1.61 KB

Contents

class Node[+T]
case object Empty extends Node[Nothing]
case class NonEmpty[T](value: T, var previous: Node[T], var next: Node[T]) extends Node[T]

class Deque[T] {
  private var first: Node[T] = Empty
  private var last: Node[T] = Empty

  def push(item: T): Unit = synchronized {
    last match {
      case Empty =>
        val newNode = NonEmpty[T](item, Empty, Empty)
        first = newNode
        last = newNode
      case node: NonEmpty[T] =>
        val newLast = NonEmpty(item, node, Empty)
        node.next = newLast
        last = newLast
    }
  }

  def pop: Option[T] = synchronized {
    last match {
      case Empty => None
      case node: NonEmpty[T] =>
        val prev = node.previous
        last = prev
        last match {
          case Empty =>
            first = Empty
          case node: NonEmpty[T] =>
            node.next = Empty
        }
        Some(node.value)
    }
  }

  def unshift(item: T): Unit = synchronized {
    first match {
      case Empty =>
        val newNode = NonEmpty[T](item, Empty, Empty)
        first = newNode
        last = newNode
      case node: NonEmpty[T] =>
        val newFirst = NonEmpty[T](item, Empty, node)
        node.previous = newFirst
        first = newFirst
    }
  }

  def shift: Option[T] = synchronized {
    first match {
      case Empty => None
      case node: NonEmpty[T] =>
        val nxt = node.next
        first = nxt
        first match {
          case Empty =>
            last = Empty
          case node: NonEmpty[T] =>
            node.previous = Empty
        }
        Some(node.value)
    }
  }
}

object Deque {
  def apply[T]() = new Deque[T]
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.179 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.178 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.177 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.176 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.175 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.174 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.173 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.172 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.171 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.170 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.169 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.167 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.166 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.165 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.164 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.163 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.162 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.161 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.160 tracks/scala/exercises/linked-list/example.scala