1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16interface MyStack<Item> {
17    getTop() : Item|null;
18    pop() : Item|null;
19    push(item : Item) : void;
20    isEmpty() : boolean;
21    size() : int;
22}
23
24class MyArrayStack<Item> implements MyStack<Item> {
25
26    private container : Item[];
27    private top : int = -1;
28    private readonly DEFAULT_SIZE : int = 100;
29
30    constructor() {
31        this.container = new Item[this.DEFAULT_SIZE];
32        this.top = -1;
33    }
34
35    constructor(size: int) {
36        this.container = new Item[size];
37    }
38
39    override getTop() : Item|null {
40        if (this.top == -1) {
41            return null;
42        }
43        return this.container[this.top];
44    }
45
46    override pop() : Item|null {
47        if (this.top == -1) {
48            return null;
49        }
50        return this.container[this.top--];
51    }
52
53    override push(item : Item) : void {
54        this.container[++this.top] = item;
55    }
56
57    override isEmpty() : boolean {
58        return (this.top == -1);
59    }
60
61    override size() : int {
62        return (this.top + 1);
63    }
64}
65
66function main() : void {
67  let stack1 : MyArrayStack<Int>;
68  stack1 = new MyArrayStack<Int>();
69  assert(stack1.isEmpty() == true);
70  assert(stack1.size() == 0);
71
72  stack1.push(new Int(10));
73  assert(stack1.isEmpty() == false);
74  assert(stack1.size() == 1);
75  assert((stack1.getTop() as Int) == 10);
76
77  stack1.push(new Int(20));
78  stack1.push(new Int(30));
79  assert(stack1.isEmpty() == false);
80  assert(stack1.size() == 3);
81  assert((stack1.getTop() as Int) == 30);
82
83  assert((stack1.pop() as Int) == 30);
84  assert(stack1.size() == 2);
85  assert((stack1.pop() as Int) == 20);
86  assert(stack1.size() == 1);
87  assert((stack1.pop() as Int) == 10);
88  assert(stack1.size() == 0);
89  assert(stack1.isEmpty() == true);
90
91  assert(stack1.getTop() == null);
92  assert(stack1.pop() == null);
93  assert(stack1.size() == 0);
94  assert(stack1.isEmpty() == true);
95
96}
97