File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Expand file tree Collapse file tree 1 file changed +26
-2
lines changed Original file line number Diff line number Diff line change 1
- /*
1
+ /**
2
2
Stack
3
3
4
4
A stack is like an array but with limited functionality. You can only push
9
9
last is the first one to come off with the next pop.
10
10
11
11
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
+ ```
12
22
*/
13
-
14
23
public struct Stack < T> {
24
+
25
+ /// Datastructure consisting of a generic item.
15
26
fileprivate var array = [ T] ( )
16
27
28
+ /// The number of items in the stack.
17
29
public var count : Int {
18
30
return array. count
19
31
}
20
32
33
+ /// Verifies if the stack is empty.
21
34
public var isEmpty : Bool {
22
35
return array. isEmpty
23
36
}
24
37
38
+ /**
39
+ Pushes an item to the top of the stack.
40
+
41
+ - Parameter element: The item being pushed.
42
+ */
25
43
public mutating func push( _ element: T ) {
26
44
array. append ( element)
27
45
}
28
46
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
+ */
29
52
public mutating func pop( ) -> T ? {
30
53
return array. popLast ( )
31
54
}
32
55
56
+ /// Returns the item at the top of the stack.
33
57
public var top : T ? {
34
58
return array. last
35
59
}
You can’t perform that action at this time.
0 commit comments