File tree Expand file tree Collapse file tree 1 file changed +26
-26
lines changed
Expand file tree Collapse file tree 1 file changed +26
-26
lines changed Original file line number Diff line number Diff line change 33 Push and pop are O(1) operations.
44 */
55public struct Stack < T> {
6- fileprivate var array = [ T] ( )
7-
8- public var isEmpty : Bool {
9- return array. isEmpty
10- }
11-
12- public var count : Int {
13- return array. count
14- }
15-
16- public mutating func push( _ element: T ) {
17- array. append ( element)
18- }
19-
20- public mutating func pop( ) -> T ? {
21- return array. popLast ( )
22- }
23-
24- public var top : T ? {
25- return array. last
26- }
6+ fileprivate var array = [ T] ( )
7+
8+ public var isEmpty : Bool {
9+ return array. isEmpty
10+ }
11+
12+ public var count : Int {
13+ return array. count
14+ }
15+
16+ public mutating func push( _ element: T ) {
17+ array. append ( element)
18+ }
19+
20+ public mutating func pop( ) -> T ? {
21+ return array. popLast ( )
22+ }
23+
24+ public var top : T ? {
25+ return array. last
26+ }
2727}
2828
2929extension Stack : Sequence {
30- public func makeIterator( ) -> AnyIterator < T > {
31- var curr = self
32- return AnyIterator {
33- return curr. pop ( )
34- }
30+ public func makeIterator( ) -> AnyIterator < T > {
31+ var curr = self
32+ return AnyIterator {
33+ return curr. pop ( )
3534 }
35+ }
3636}
You can’t perform that action at this time.
0 commit comments