1

Having the code:

class DoubleLinkedQueue[T] {
    class DoubleLinkedQueueNode[T](value: T) {
        var next : DoubleLinkedQueueNode[T]
        var prev : DoubleLinkedQueueNode[T]
    }

    var head = new DoubleLinkedQueueNode[T](_)
    var tail = new DoubleLinkedQueueNode[T](_)
    head.next = tail //compillation error here

    def isEmpty() : Boolean = {
        return head.next == tail //and here
    }

    def peekStart() : T = {
        return head.next.value //and here too
    }

    def popStart() : T = {
        val result = peekStart()
        head.next = head.next.next //and everywhere when trying to acces head or tail properties
        return result
    }

    def peekEnd() : T = {
        return tail.prev.value
    }

    def popEnd() : T = {
        val result = peekEnd()
        tail.prev = tail.prev.prev
        return result
    }

    def pushStart(value : T) : DoubleLinkedQueue[T] = {
        val node = new DoubleLinkedQueueNode[T](value)
        node.next = head.next
        node.prev = head
        head.next = node
        return this
    }

    def pushEnd(value : T) : DoubleLinkedQueue[T] = {
        val node = new DoubleLinkedQueueNode[T](value)
        node.prev = tail.prev
        node.next = tail
        tail.prev = node
        return this
    }
}

and having the compillation error:

DoubleLinkedQueue.scala:9: error: value next is not a member of T => DoubleLinkedQueue.this.DoubleLinkedQueueNode[T] head.next = tail

Does anybody know how to fix it?

UPD1 Is it the same issue that described here?

Community
  • 1
  • 1
noorhe
  • 21
  • 3
  • In `var head = new DoubleLinkedQueueNode[T](_)`, head is of the type T=>DoubleLinkedQueueNode. `new DoubleLinkedQueueNode[T](_)`, is a partially applied function. – Johny T Koshy Nov 27 '15 at 15:49
  • so how to make it type of DoubleLinkedQueueNode and leave DoubleLinkedQueueNode still generic? – noorhe Nov 27 '15 at 15:53

3 Answers3

0

You can make value of DoubleLinkedQueueNode mutable:

class DoubleLinkedQueueNode[T] {
  var next : DoubleLinkedQueueNode[T] = _
  var prev : DoubleLinkedQueueNode[T] = _
  var value: T = _
}

So you don't need to initialize it with:

 var head = new DoubleLinkedQueueNode[T](_)

Instead you'll have:

 var head = new DoubleLinkedQueueNode[T]
Nyavro
  • 8,479
  • 2
  • 21
  • 30
0

It seems to be, that a good solution is to use an Option as type of 'val' in DoubleLinkedQueueNode.

noorhe
  • 21
  • 3
0

If you decide to create an instance of the DoubleLinkedQueueNode, you must give it a proper value of type T and not _.

When writing the following:

var head = new DoubleLinkedQueueNode[T](_)

You in fact create the following function:

T => DoubleLinkedQueueNode[T] = <function1>

So basically, its the same as writing:

var func = new DoubleLinkedQueueNode[T](_)
var head = func(someValueOfTypeT)

By the way, there are other issues i spot in the code, such as:

The second command can be erased:

var head = new DoubleLinkedQueueNode[T](_)
var tail = new DoubleLinkedQueueNode[T](_)
head.next = tail //compillation error here

value should be val (as it is accessed in various methods in DoubleLinkedQueue)

class DoubleLinkedQueueNode[T](val value: T) {

Hope it helps.

Moshe Derri
  • 208
  • 2
  • 11