Skip to content

Commit b4ec5b8

Browse files
Added Markdown Documentation to Stack playground
This might be overkill for such a small file, but I wish I started using Swift Markdown early in my iOS career. Option click is too convenient to ignore!
1 parent 3d8ad39 commit b4ec5b8

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Stack/Stack.playground/Contents.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
Stack
33

44
A stack is like an array but with limited functionality. You can only push
@@ -9,27 +9,51 @@
99
last is the first one to come off with the next pop.
1010

1111
Push and pop are O(1) operations.
12+
13+
## Usage
14+
```
15+
var myStack = Stack(array: [])
16+
myStack.push(10)
17+
myStack.push(3)
18+
myStack.push(57)
19+
myStack.pop() // 57
20+
myStack.pop() // 3
21+
```
1222
*/
13-
1423
public struct Stack<T> {
24+
25+
/// Datastructure consisting of a generic item.
1526
fileprivate var array = [T]()
1627

28+
/// The number of items in the stack.
1729
public var count: Int {
1830
return array.count
1931
}
2032

33+
/// Verifies if the stack is empty.
2134
public var isEmpty: Bool {
2235
return array.isEmpty
2336
}
2437

38+
/**
39+
Pushes an item to the top of the stack.
40+
41+
- Parameter element: The item being pushed.
42+
*/
2543
public mutating func push(_ element: T) {
2644
array.append(element)
2745
}
2846

47+
/**
48+
Removes and returns the item at the top of the sack.
49+
50+
- Returns: The item at the top of the stack.
51+
*/
2952
public mutating func pop() -> T? {
3053
return array.popLast()
3154
}
3255

56+
/// Returns the item at the top of the stack.
3357
public var top: T? {
3458
return array.last
3559
}

0 commit comments

Comments
 (0)