Stack
public struct Stack<T>
A Stack is a Last-In-First-Out (LIFO) collection, the last element added to the stack will be the first one to be removed.
The push
and pop
operations run in amortized constant time.
Conforms to Sequence
, ExpressibleByArrayLiteral
,
CustomStringConvertible
.
-
Constructs an empty stack.
Declaration
Swift
public init()
-
Constructs a stack from a sequence, such as an array. The elements will be pushed from first to last.
Declaration
Swift
public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T
-
Number of elements stored in the stack.
Declaration
Swift
public var count : Int
-
Returns
true
if and only ifcount == 0
.Declaration
Swift
public var isEmpty: Bool
-
The top element of the stack, or
nil
if the stack is empty.Declaration
Swift
public var top: T?
-
Inserts an element into the top of the stack.
Declaration
Swift
public mutating func push(_ element: T)
-
Retrieves and removes the top element of the stack.
Declaration
Swift
public mutating func pop() -> T
Return Value
The top element.
-
Removes all the elements from the stack, and by default clears the underlying storage buffer.
Declaration
Swift
public mutating func removeAll(keepingCapacity keep: Bool = false)
-
Provides for-in loop functionality. Generates elements in LIFO order.
Declaration
Swift
public func makeIterator() -> AnyIterator<T>
Return Value
A generator over the elements.
-
Constructs a stack using an array literal. The elements will be pushed from first to last.
let stack: Stack<Int> = [1,2,3]
Declaration
Swift
public init(arrayLiteral elements: T...)
-
A string containing a suitable textual representation of the stack.
Declaration
Swift
public var description: String