-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.rb
More file actions
82 lines (62 loc) · 994 Bytes
/
Copy pathsolution.rb
File metadata and controls
82 lines (62 loc) · 994 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module Solution1
class Stack
def initialize
@array = []
end
def add(e)
@array << e
end
def remove
@array.pop
end
def peek
@array.last
end
def empty?
@array.empty?
end
def size
@array.length
end
end
end
module Solution2
class Stack
attr_reader :size
def initialize
@first_node = nil
@size = 0
end
def add(e)
node = Node.new(e)
if @first_node.nil?
@first_node = node
else
node.next = @first_node
@first_node = node
end
@size += 1
end
def remove
if size > 0
tmp = @first_node.data
@first_node = @first_node&.next
@size -= 1
tmp
end
end
def peek
@first_node&.data
end
def empty?
size == 0
end
end
class Node
attr_accessor :data, :next
def initialize(data)
@data = data
@next = nil
end
end
end