In Introduction to Algorithms, the stack data structure's enqueue operation has the following pseudocode

Q[Q.tail] = x
if Q.tail == Q.length
    Q.tail = 1
else Q.tail = Q.tail + 1

I started writing it in Swift and figured the queue should be modeled as a structure

struct IntQueue {
    var items = [Int]()
    var head = 0
    var tail = 0

func enqueue(queue: IntQueue, item: Int) {


to be able to access the head and tail properties. However, I'm not sure why the pseudocode subscripts Q directly. Is this shorthand for subscripting a queue's nested array property rather than the queue type itself?

  • 1
    $\begingroup$ You may have not realized that the answer to your question is in your question. That is, pseudocode. $\endgroup$ – John L. Nov 1 '18 at 4:54

In pseudocode we can use whatever convention we want, as long as the reader understands what we mean.

That said, in languages like python and C++ you can actually define the subscript operator to access the underlying array, and for such languages the pseudocode could work literally.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.