Group Group Group Group Group Group Group Group Group

Possible Mistake in QueueStack description

#1

I’ve implemented all of the variations of queue that the books has to offer. When I got to the QueueStack variant of a Queue data structure, It appeared that the print out was “backwards”.

The current code is this:

extension QueueStack: CustomStringConvertible {
  public var description: String {
    let printList = leftStack + rightStack.reversed()
    return printList.description
  }
}

When referencing the code snippet to test the output:

var queue = QueueStack<String>()
queue.enqueue("Ray")
queue.enqueue("Brian")
queue.enqueue("Eric")
queue.dequeue()
queue
queue.peek

I took the liberty of printing it directly to the console via this update:

print(queue)
queue.dequeue()
print(queue)
print(queue.peek!)

The response ended up being:

["Eric", "Brian", "Ray"]
["Eric", "Brian"]
"Brian"

I modified the above to add a few more elements after the dequeue call:

var queue = QueueStack<String>()
queue.enqueue("Ray")
queue.enqueue("Brian")
queue.enqueue("Eric")
print(queue)
queue.dequeue()
queue.enqueue("Asa")
queue.enqueue("Brandon")
print(queue)
print(queue.peek)

The output of this produced:

["Eric", "Brian", "Ray"]
["Eric", "Brian", "Brandon", "Asa"]
Optional("Brian")

If we update the description computed property in CustomStringConvertible protocol on QueueStack to be:

extension QueueStack: CustomStringConvertible {
  public var description: String {
    let printList = leftStack.reversed() + rightStack
    return printList.description
  }
}

We get the same output as the rest of the Queue variants:

["Ray", "Brian", "Eric"]
["Brian", "Eric", "Asa", "Brandon"]
"Brian"

I want to think that this was the original intention. If not, then please ignore my post.

#2

@psyaryu good catch! Thanks a lot, we will fix it for the up-coming release!

1 Like