Skip to content

Commit d7d41e8

Browse files
committed
Merge pull request kodecocodes#3 from JovannyEspinal/master
Added playgrounds with comments
2 parents 5f071fd + ef7a338 commit d7d41e8

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

Queue/Queue.playground/Contents.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
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)
11+
12+
13+
*/
14+
15+
16+
import UIKit
17+
18+
19+
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
44+
}
45+
}
46+
47+
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"]
51+
52+
queueOfNames.dequeue() // Removes and returns the first element in the queue. Returns "Carl" and then removes it from the queue
53+
54+
queueOfNames.peek() // Returns the first element in the queue. Returns "Lisa" since "Carl" was dequeued on the previous line
55+
56+
queueOfNames.isEmpty // Checks to see if the queue is empty. Returns "False" since the queue still has elements in it.
57+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Stack/Stack.playground/Contents.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
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)
11+
12+
13+
*/
14+
15+
import UIKit
16+
17+
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
26+
}
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+
44+
}
45+
46+
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
52+
53+
stackOfNames.peek() // Returns the first element in the stack. Returns "Wade" since "Mike" was popped on the previous line
54+
55+
stackOfNames.isEmpty // Checks to see if the stack is empty. Returns "False" since the stack still has elements in it
56+
57+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)