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
Joyce Ryu
1,124 PointsQueue 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()
1 Answer
Richard Lu
20,185 PointsHey 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! :)
Martin Wildfeuer
Courses Plus Student 11,071 PointsI 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.
Richard Lu
20,185 PointsThanks for pointing that out Martin Wildfeuer, I really appreciate it :). The full solution is located here:
Martin Wildfeuer
Courses Plus Student 11,071 PointsSure 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 ;)
Martin Wildfeuer
Courses Plus Student 11,071 PointsMartin Wildfeuer
Courses Plus Student 11,071 PointsCould you please explain your question/problem in more detail? I have no clue :|