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.159 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.158 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.157 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.156 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.155 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.154 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.153 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.152 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.151 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.150 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.149 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.148 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.147 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.146 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.145 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.144 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.143 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.142 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.141 tracks/scala/exercises/linked-list/example.scala
trackler-2.2.1.140 tracks/scala/exercises/linked-list/example.scala