-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoubleLinkedList.rb
94 lines (84 loc) · 2.32 KB
/
DoubleLinkedList.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
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
# Single Linked List Example
class Node
attr_accessor :value, :next_node, :previous_node
def initialize(value, previous_node=nil, next_node=nil)
@value = value
@next_node = next_node
@previous_node = previous_node
end
end
class DoubleLinkedList
attr_accessor :head, :size
def initialize
@head = nil
@size = 0
end
def addNode(value)
if @size == 0
@head = Node.new(value, nil, nil)
@size += 1
else
current_node = @head
while current_node.next_node != nil
current_node = current_node.next_node
end
current_node.next_node = Node.new(value, current_node, nil)
@size += 1
end
return displayNodes
end
def displayNodes
if @size == 0
return "head: --> nil"
end
fullList = "nil <== head: ( #{@head.value.to_s} )"
current_node = @head.next_node
while current_node != nil
fullList += " <==> ( #{current_node.value.to_s} )"
current_node = current_node.next_node
end
fullList += " ==> nil"
puts fullList
end
def emptyList # I GUESS ALSO #5
initialize
end
def addNodeToBeginning(value) # THIS WAS #1
return addNode(value) if size == 0
the_next_node = @head
@head = Node.new(value, nil, the_next_node)
the_next_node.previous_node = @head
@size += 1
return displayNodes
end
def removeNodeFromBeginning # THIS WAS #2
return if @size == 0
if @size == 1
@head = nil
@size -= 1
else
new_head = @head.next_node
new_head.previous_node = nil
@head.next_node = nil
@head = new_head
end
return displayNodes
end
def addNodeAtSecondPosition(value) # THIS WAS #3
return if @size == 0
new_third = @head.next_node
@head.next_node = Node.new(value, @head, new_third)
return displayNodes
end
end
dll = DoubleLinkedList.new()
# puts dll.displayNodes
dll.addNode(1)
# dll.addNode(2)
# dll.addNode(3)
# dll.addNodeToBeginning(0.5)
dll.addNodeAtSecondPosition(1.5)
# dll.emptyList
# dll.removeNodeFromBeginning
# dll.displayNodes
# puts dll.displayNodes()