"Querying With LINQ" was retired on July 14, 2025.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Queue Problem solving! ASAP what goes in the line?

struct Queue {
    var queueArray:[String] = []

    mutating func enqueue (element:String) {
        // ...
    }
    mutating func dequeue() -> String {
       // ...
        return returnValue
    }
}

var newQueue = Queue()
newQueue.enqueue("Leonard")
newQueue.enqueue("Sheldon")

newQueue.enqueue("Penny")
newQueue.enqueue("Howard")
newQueue.enqueue("Rajesh")
let firstOne = newQueue.dequeue()

Could you please explain your question/problem in more detail? I have no clue :|

1 Answer

Hey Hee Eun,

If the question is to fill in the lines commented out, here's your solution.

struct Queue {
    var queueArray:[String] = []

    mutating func enqueue(element:String) {
        queueArray.append(element)
    }
    mutating func dequeue() -> String? {
        return queueArray.count > 0 ? queueArray.removeFirst() : nil
    }
}

var newQueue = Queue()
newQueue.enqueue("Leonard")
newQueue.enqueue("Sheldon")

newQueue.enqueue("Penny")
newQueue.enqueue("Howard")
newQueue.enqueue("Rajesh")
let firstOne = newQueue.dequeue()

Enjoy! :)

I see! dequeue might have to be sthg. like

mutating func dequeue() -> String? {
    return !test.isEmpty ? test.removeFirst() : nil
}

in order to remove that item from the queue.

Thanks for pointing that out Martin Wildfeuer, I really appreciate it :). The full solution is located here:

Solution

Sure thing, Richard Lu! I appreciate you taking the time to write the Queue/Stack thing. To be honest, not sure if I had done this in this case ;)