-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathArrayStack.swift
More file actions
48 lines (40 loc) · 956 Bytes
/
ArrayStack.swift
File metadata and controls
48 lines (40 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// ArrayStack.swift
// Stack
//
// Created by ggl on 2019/4/8.
// Copyright © 2019年 ggl. All rights reserved.
// 数组顺序栈
import Foundation
class ArrayStack<Element> {
/// 顺序栈底层结构
var array: [Element]
/// 元素的个数
var count: Int {
return array.count
}
init() {
array = []
}
/// 元素入栈
///
/// - Parameter item: 需要入栈的元素
func push(_ item: Element) {
array.append(item)
}
/// 元素出栈
///
/// - Returns: 需要出栈的元素
@discardableResult
func pop() -> Element? {
return array.last
}
/// 打印元素
func print() {
Swift.print("ArrayStack元素(靠前的元素表示栈顶元素):", terminator: "")
for item in array.reversed() {
Swift.print(item, terminator: " ")
}
Swift.print("")
}
}