-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiy_adts.rb
More file actions
95 lines (78 loc) · 1.42 KB
/
diy_adts.rb
File metadata and controls
95 lines (78 loc) · 1.42 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
class Stack
attr_accessor :stack
def initialize
# create ivar to store stack here!
@stack = []
end
def push(el)
# adds an element to the stack
@stack.push(el)
end
def pop
# removes one element from the stack
@stack.pop
end
def peek
# returns, but doesn't remove, the top element in the stack
@stack.last
end
end
# s = Stack.new
# p s.push(6)
# p s.pop
# p s.peek
class Queue
attr_accessor :queue
def initialize
# create ivar to store queue here!
@queue= []
end
def enqueue(el)
@queue.push(el)
end
def dequeue
@queue.shift
end
def peek
@queue.first
end
end
# q = Queue.new
# p q.enqueue(2)
# p q.enqueue(3)
# p q.enqueue(4)
# p q.dequeue
# p q.dequeue
# p q.peek
class Map
attr_accessor :map
def initialize
@map = []
end
def set(key, value)
@map.each do |pair|
if pair[0] == key
pair[1] = value
return @map
end
end
@map << [key,value]
end
def get(key)
res = @map.detect {|pair| pair[0] == key}
res && res[1]
end
def delete(key)
@map.reject! {|pair| pair[0] == key}
end
def show
p @map
end
end
# m = Map.new
# p m.set('a', 0)
# p m.set('b', 1)
# p m.set('a', 2)
# p m.get('b')
# p m.delete('a')
# m.show