-
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathstack.cr
More file actions
44 lines (35 loc) · 704 Bytes
/
stack.cr
File metadata and controls
44 lines (35 loc) · 704 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
class Stack(T)
# The items in the stack.
@stack : Array(T)
# Creates a new empty stack.
def initialize
@stack = Array(T).new
end
# Pushes the given *item* onto the stack and returns the size of the stack.
def push(item : T)
@stack << item
self.size
end
# Remove the last item from the stack.
def pop() : T
@stack.pop
end
# Returns the number of items in the stack.
def size : Int32
@stack.size
end
# Returns the last item push onto the stack.
def top : T
@stack[-1]
end
end
def stack_example
stack = Stack(Int32).new
stack.push(4)
stack.push(5)
stack.push(9)
puts stack.pop
puts stack.size
puts stack.top
end
stack_example