Fix the programming style (make it more idiomatic to Ruby)

This commit is contained in:
Niboucha Redouane 2020-10-12 18:59:48 +02:00
parent a3d895a367
commit 75c72ca7a5
2 changed files with 51 additions and 60 deletions

View file

@ -10,15 +10,16 @@ end
# A Class for double linked lists (each element links to the next one, and to the previous one) # A Class for double linked lists (each element links to the next one, and to the previous one)
class DoubleList class DoubleList
include Enumerable
attr_accessor :head, :tail attr_accessor :head, :tail
def initialize() def initialize
@head = nil @head = nil
@tail = nil @tail = nil
end end
def insert_tail(value) def insert_tail(value)
new_node = Node.new(value) new_node = Node.new(value)
if (@head == nil) if @head.nil?
@head = new_node @head = new_node
@tail = new_node @tail = new_node
else else
@ -30,7 +31,7 @@ class DoubleList
def insert_head(value) def insert_head(value)
new_node = Node.new(value) new_node = Node.new(value)
if (@head == nil) if @head.nil?
@head = new_node @head = new_node
@tail = new_node @tail = new_node
else else
@ -40,41 +41,36 @@ class DoubleList
end end
end end
def delete_tail() def delete_tail
if (@tail != nil) until @tail.nil?
@tail = @tail.prev @tail = @tail.prev
if (@tail != nil) @tail.next = nil unless @tail.nil?
@tail.next = nil
end
end end
end end
def delete_head() def delete_head
if (@head != nil) until @head.nil?
@head = @head.next @head = @head.next
if (@head != nil) @head.prev = nil unless @head.nil?
@head.prev = nil
end
end end
end end
def print_list() def each
print "[" return if @head.nil?
if (@head != nil)
printNode = @head current = @head
while (printNode != nil) until current.nil?
print "#{printNode.value}" yield current.value
if (printNode != @tail) current = current.next
print ", "
end
printNode = printNode.next
end
end end
print "]"
STDOUT.flush
end end
def is_empty() def print_list
return (@head==nil) # the to_a method is from Enumerable, will call each to get the values, and return an array
puts '[' + self.to_a.join(', ') + ']'
end end
end
def empty?
@head.nil?
end
end

View file

@ -10,27 +10,26 @@ end
# A Class for single linked lists (each element links to the next one, but not to the previous one) # A Class for single linked lists (each element links to the next one, but not to the previous one)
class SingleList class SingleList
include Enumerable
attr_accessor :head attr_accessor :head
def initialize() def initialize
@head = nil @head = nil
end end
def insert_tail(value) def insert_tail(value)
newNode = Node.new(value) newNode = Node.new(value)
if (@head == nil) if @head.nil?
@head = newNode @head = newNode
else else
tempNode = @head tempNode = @head
while (tempNode.next != nil) tempNode = tempNode.next until tempNode.next.nil?
tempNode = tempNode.next
end
tempNode.next = newNode tempNode.next = newNode
end end
end end
def insert_head(value) def insert_head(value)
newNode = Node.new(value) newNode = Node.new(value)
if (@head == nil) if @head.nil?
@head = newNode @head = newNode
else else
newNode.next = @head newNode.next = @head
@ -38,42 +37,38 @@ class SingleList
end end
end end
def print_list() def each
print "[" return if @head.nil?
if (@head != nil)
printNode = @head current = @head
while (printNode != nil) until current.nil?
print "#{printNode.value}" yield current.value
if (printNode.next != nil) current = current.next
print ", "
end
printNode = printNode.next
end
end end
print "]" end
STDOUT.flush
def print_list
puts '[' + self.to_a.join(', ') + ']'
end end
def delete_head def delete_head
if (@head != nil) && (@head.next != nil) if !@head.nil? && !@head.next.nil?
newHead = @head.next newHead = @head.next
@head = newHead @head = newHead
elsif (@head != nil) && (@head.next == nil) elsif !@head.nil? && @head.next.nil?
@head = nil @head = nil
end end
end end
def delete_tail def delete_tail
if (@head != nil) return if @head.nil?
tempNode = @head
while (tempNode.next.next != nil) tempNode = @head
tempNode = tempNode.next tempNode = tempNode.next until tempNode.next.next.nil?
end tempNode.next = nil
tempNode.next = nil
end
end end
def isEmpty() def empty?
return (@head==nil) @head.nil?
end end
end end