Skip to content

Commit b9d245b

Browse files
Update ruby-implementation.md
1 parent 0f6fd5b commit b9d245b

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

stacks/ruby-implementation.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
```ruby
2+
3+
# Stack using array as underlying data collector
4+
# we'll manipulate the element at the last index, since it supports O(1) operations
5+
6+
class ArrayStack
7+
def initialize
8+
@list = []
9+
end
10+
11+
def push(value)
12+
@list.append(Node.new(value))
13+
end
14+
15+
def pop
16+
@list.pop()
17+
end
18+
19+
def top
20+
@list.last
21+
end
22+
23+
def isEmpty
24+
@list.empty?
25+
end
26+
end
27+
28+
# Stack using LinkedList as underlying data collector
29+
# we'll manipulate HEAD, since it supports O(1) operations
30+
31+
class LLStack
32+
def initialize
33+
@list = LinkedList.new()
34+
end
35+
36+
def push(value)
37+
node = Node.new(value)
38+
node.next = @list.head
39+
@list.head = node
40+
end
41+
42+
def pop
43+
newHead = @list.head.next
44+
value = @list.head.value
45+
@list.head = newHead
46+
value
47+
end
48+
49+
def top
50+
value = @list.head.value
51+
value
52+
end
53+
54+
def is_empty?
55+
@list.head.nil?
56+
end
57+
end
58+
59+
```

0 commit comments

Comments
 (0)