Skip to content

Commit e5d9c12

Browse files
committed
Small tweaks to stack and queue playgrounds
1 parent d7d41e8 commit e5d9c12

File tree

2 files changed

+90
-84
lines changed

2 files changed

+90
-84
lines changed

Queue/Queue.playground/Contents.swift

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
11
/*
2+
Queue
23

3-
Queue
4-
5-
A queue is a list where you can only insert new items at the back and remove items from the front. This ensures that the first item you enqueue is also the first item you dequeue. First come, first serve!
6-
7-
A queue gives you a FIFO or first-in, first-out order. The element you inserted first is also the first one to come out again. It's only fair! (A very similar data structure, the stack, is LIFO or last-in first-out.)
8-
9-
Insertion - O(1)
10-
Deletion - O(n)
4+
A queue is a list where you can only insert new items at the back and
5+
remove items from the front. This ensures that the first item you enqueue
6+
is also the first item you dequeue. First come, first serve!
117

8+
A queue gives you a FIFO or first-in, first-out order. The element you
9+
inserted first is also the first one to come out again. It's only fair!
1210

11+
In this implementation, enqueuing is an O(1) operation, dequeuing is O(n).
1312
*/
1413

15-
16-
import UIKit
17-
18-
1914
public struct Queue<T> {
20-
private var array = [T]()
21-
22-
public var count: Int {
23-
return array.count
24-
}
25-
26-
public var isEmpty: Bool {
27-
return array.isEmpty
28-
}
29-
30-
public mutating func enqueue(element: T) {
31-
array.append(element)
32-
}
33-
34-
public mutating func dequeue() -> T? {
35-
if count == 0 {
36-
return nil
37-
} else {
38-
return array.removeFirst()
39-
}
40-
}
41-
42-
public func peek() -> T? {
43-
return array.first
15+
private var array = [T]()
16+
17+
public var count: Int {
18+
return array.count
19+
}
20+
21+
public var isEmpty: Bool {
22+
return array.isEmpty
23+
}
24+
25+
public mutating func enqueue(element: T) {
26+
array.append(element)
27+
}
28+
29+
public mutating func dequeue() -> T? {
30+
if count == 0 {
31+
return nil
32+
} else {
33+
return array.removeFirst()
4434
}
35+
}
36+
37+
public func peek() -> T? {
38+
return array.first
39+
}
4540
}
4641

42+
// Create a queue and put some elements on it already.
43+
var queueOfNames = Queue(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"])
4744

48-
var queueOfNames = Queue(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"]) // Initialized queue
49-
50-
queueOfNames.enqueue("Mike") // Adds element to the end of the queue. Queue is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"]
45+
// Adds an element to the end of the queue.
46+
queueOfNames.enqueue("Mike")
5147

52-
queueOfNames.dequeue() // Removes and returns the first element in the queue. Returns "Carl" and then removes it from the queue
48+
// Queue is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"]
49+
print(queueOfNames.array)
5350

54-
queueOfNames.peek() // Returns the first element in the queue. Returns "Lisa" since "Carl" was dequeued on the previous line
51+
// Remove and return the first element in the queue. This returns "Carl".
52+
queueOfNames.dequeue()
5553

56-
queueOfNames.isEmpty // Checks to see if the queue is empty. Returns "False" since the queue still has elements in it.
54+
// Return the first element in the queue.
55+
// Returns "Lisa" since "Carl" was dequeued on the previous line.
56+
queueOfNames.peek()
5757

58+
// Check to see if the queue is empty.
59+
// Returns "false" since the queue still has elements in it.
60+
queueOfNames.isEmpty

Stack/Stack.playground/Contents.swift

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
11
/*
2+
Stack
23

3-
Stack
4-
5-
A stack is like an array but with limited functionality. You can only push to add a new element to the top of the stack, pop to remove the element from the top, and peek at the top element without popping it off.
6-
7-
A stack gives you a LIFO or last-in first-out order. The element you pushed last is the first one to come off with the next pop. (A very similar data structure, the queue, is FIFO or first-in first-out.)
8-
9-
Insertion - O(1)
10-
Deletion - O(1)
4+
A stack is like an array but with limited functionality. You can only push
5+
to add a new element to the top of the stack, pop to remove the element from
6+
the top, and peek at the top element without popping it off.
117

8+
A stack gives you a LIFO or last-in first-out order. The element you pushed
9+
last is the first one to come off with the next pop.
1210

11+
Push and pop are O(1) operations.
1312
*/
1413

15-
import UIKit
16-
1714
public struct Stack<T> {
18-
private var array = [T]()
19-
20-
public var count: Int {
21-
return array.count
22-
}
23-
24-
public var isEmpty: Bool {
25-
return array.isEmpty
15+
private var array = [T]()
16+
17+
public var count: Int {
18+
return array.count
19+
}
20+
21+
public var isEmpty: Bool {
22+
return array.isEmpty
23+
}
24+
25+
public mutating func push(element: T) {
26+
array.append(element)
27+
}
28+
29+
public mutating func pop() -> T? {
30+
if count == 0 {
31+
return nil
32+
} else {
33+
return array.removeLast()
2634
}
27-
28-
public mutating func push(element: T) {
29-
array.append(element)
30-
}
31-
32-
public mutating func pop() -> T? {
33-
if count == 0 {
34-
return nil
35-
} else {
36-
return array.removeLast()
37-
}
38-
}
39-
40-
public func peek() -> T? {
41-
return array.last
42-
}
43-
35+
}
36+
37+
public func peek() -> T? {
38+
return array.last
39+
}
4440
}
4541

42+
// Create a stack and put some elements on it already.
43+
var stackOfNames = Stack(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"])
4644

47-
var stackOfNames = Stack(array: ["Carl", "Lisa", "Stephanie", "Jeff", "Wade"]) // Initialized queue
48-
49-
stackOfNames.push("Mike") // Adds element to the end of the stack. Stack is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"]
50-
51-
stackOfNames.pop() // Removes and returns the last element in the stack. Returns "Mike" and then removes it from the stack
45+
// Add an element to the top of the stack.
46+
stackOfNames.push("Mike")
5247

53-
stackOfNames.peek() // Returns the first element in the stack. Returns "Wade" since "Mike" was popped on the previous line
48+
// The stack is now ["Carl", "Lisa", "Stephanie", "Jeff", "Wade", "Mike"]
49+
print(stackOfNames.array)
5450

55-
stackOfNames.isEmpty // Checks to see if the stack is empty. Returns "False" since the stack still has elements in it
51+
// Remove and return the first element from the stack. This returns "Mike".
52+
stackOfNames.pop()
5653

54+
// Look at the first element from the stack.
55+
// Returns "Wade" since "Mike" was popped on the previous line.
56+
stackOfNames.peek()
5757

58+
// Check to see if the stack is empty.
59+
// Returns "false" since the stack still has elements in it.
60+
stackOfNames.isEmpty

0 commit comments

Comments
 (0)