with a single test instead of two per iteration. Or even simpler:
stack2 = stack1.reversed()
stack1.removeAll()
(If your queues become really large then you might want to check which of the above methods is faster.)
If a function body consists of a single expression then the return
keyword can be omitted.
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
while let elemstack2 = stack1.popLastreversed() {
stack2stack1.appendremoveAll(elem)
}
}
return stack2.popLast()
}
func peek() -> Element? {
stack2.last ?? stack1.first
}
func empty() -> Bool {
stack1.isEmpty && stack2.isEmpty
}
}
with a single test instead of two per iteration.
If a function body consists of a single expression then the return
keyword can be omitted.
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
while let elem = stack1.popLast() {
stack2.append(elem)
}
}
return stack2.popLast()
}
func peek() -> Element? {
stack2.last ?? stack1.first
}
func empty() -> Bool {
stack1.isEmpty && stack2.isEmpty
}
}
with a single test instead of two per iteration. Or even simpler:
stack2 = stack1.reversed()
stack1.removeAll()
(If your queues become really large then you might want to check which of the above methods is faster.)
If a function body consists of a single expression then the return
keyword can be omitted.
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
stack2 = stack1.reversed()
stack1.removeAll()
}
return stack2.popLast()
}
func peek() -> Element? {
stack2.last ?? stack1.first
}
func empty() -> Bool {
stack1.isEmpty && stack2.isEmpty
}
}
If a function body consists of a single expression then the return
keyword can be omitted.
Putting it all together, we get
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
while let elem = stack1.popLast() {
stack2.append(elem)
}
}
return stack2.popLast()
}
func peek() -> Element? {
return stack2.last ?? stack1.first
}
func empty() -> Bool {
return stack1.isEmpty && stack2.isEmpty
}
}
I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as
FIFO
orFIFOQueue
.Typical names for adding something to a FIFO and retrieving it are
enqueue
anddequeue
.Swift collection types have an
isEmpty
typically use "isEmpty" as the name of a property to indicatewhich indicates whether itthe collection contains elements or not:var isEmpty: Bool { stack1.isEmpty && stack2.isEmpty }
Putting it all together, we get
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
while let elem = stack1.popLast() {
stack2.append(elem)
}
}
return stack2.popLast()
}
func peek() -> Element? {
return stack2.last ?? stack1.first
}
func empty() -> Bool {
return stack1.isEmpty && stack2.isEmpty
}
}
I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as
FIFO
orFIFOQueue
.Typical names for adding something to a FIFO and retrieving it are
enqueue
anddequeue
.Swift collection types have an
isEmpty
property to indicate whether it contains elements or not:var isEmpty: Bool { stack1.isEmpty && stack2.isEmpty }
If a function body consists of a single expression then the return
keyword can be omitted.
Putting it all together, we get
class MyQueue<Element> {
private var stack1: [Element]
private var stack2: [Element]
init() {
stack1 = []
stack2 = []
}
func push(_ x: Element) {
stack1.append(x)
}
func pop() -> Element? {
if stack2.isEmpty {
while let elem = stack1.popLast() {
stack2.append(elem)
}
}
return stack2.popLast()
}
func peek() -> Element? {
stack2.last ?? stack1.first
}
func empty() -> Bool {
stack1.isEmpty && stack2.isEmpty
}
}
I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as
FIFO
orFIFOQueue
.Typical names for adding something to a FIFO and retrieving it are
enqueue
anddequeue
.Swift collection types typically use "isEmpty" as the name of a property which indicates whether the collection contains elements or not:
var isEmpty: Bool { stack1.isEmpty && stack2.isEmpty }
Finally, naming: I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as FIFO
or FIFOQueue
. Typical names for adding something to a FIFO and retrieving it are enqueue
and dequeue
.
I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as
FIFO
orFIFOQueue
.Typical names for adding something to a FIFO and retrieving it are
enqueue
anddequeue
.Swift collection types have an
isEmpty
property to indicate whether it contains elements or not:var isEmpty: Bool { stack1.isEmpty && stack2.isEmpty }
Finally, naming: I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as FIFO
or FIFOQueue
. Typical names for adding something to a FIFO and retrieving it are enqueue
and dequeue
.
Finally, naming:
I had to inspect the code in order to understand that what you have implemented is a "first in, first out" queue. This would be more obvious to a user of your code (or to you in one year) with a more specific class name, such as
FIFO
orFIFOQueue
.Typical names for adding something to a FIFO and retrieving it are
enqueue
anddequeue
.Swift collection types have an
isEmpty
property to indicate whether it contains elements or not:var isEmpty: Bool { stack1.isEmpty && stack2.isEmpty }