forked from gl-lei/algorithm
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayStack.swift
More file actions
Latest commit
49 lines (40 loc) · 961 Bytes
/
Copy pathArrayStack.swift
File metadata and controls
49 lines (40 loc) · 961 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
49
//
// ArrayStack.swift
// Stack
//
// Created by ggl on 2019/4/8.
// Copyright © 2019年 ggl. All rights reserved.
// 数组顺序栈
import Foundation
classArrayStack<Element>{
/// 顺序栈底层结构
vararray:[Element]
/// 元素的个数
varcount: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:"")
foritemin array.reversed(){
Swift.print(item, terminator:"")
}
Swift.print("")
}
}