-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.swift
48 lines (39 loc) · 907 Bytes
/
Stack.swift
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
//
// Stack.swift
// TriAngle
//
// Created by Mate Hegedus on 13/12/2014.
// Copyright (c) 2014 Mate Hegedus. All rights reserved.
//
import Foundation
public class Stack<T> {
private var top:StackItem<T>? = nil
private var size = 0
public init(top:T) {
self.top = StackItem(item: top, child: nil)
size = 1
}
public init() {}
public func push(item:T) {
var stackItem = StackItem(item: item, child: top)
top = stackItem
size++
}
public func pop() -> T? {
var result = top?.value()
top = top?.getChild()
if (size > 0) {
size--
}
return result
}
public func peek() -> T? {
return top?.value()
}
public func getSize() -> Int {
return size
}
public func isEmpty() -> Bool {
return size == 0
}
}