forked from makersquare/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.rb
50 lines (46 loc) · 962 Bytes
/
linkedList.rb
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
class LinkedList
attr_reader :front, :back
def initialize
@front = nil
@back = nil
end
def add(node, position)
if @front == nil && @back == nil
@front = node
@back = node
elsif position == :front
node.in_back = @front
@front.in_front = node
@front = node
else
node.in_front = @back
@back.in_back = node
@back = node
end
end
def remove(position)
if @front == nil && @back == nil
nil
elsif @back == @front
@back = nil
@front = nil
elsif position == :front
@front = @front.in_back
@front.in_front.in_back = nil
@front.in_front = nil
elsif position == :back
@back = @back.in_front
@back.in_back.in_front = nil
@back.in_back = nil
end
end
end
class Node
attr_accessor :in_front, :in_back
attr_reader :value
def initialize(value)
@value = value
@in_front = nil
@in_back = nil
end
end