mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
Make changes according to review
This commit is contained in:
parent
baa7ca17b4
commit
906ccac28d
3 changed files with 18 additions and 16 deletions
0
data_structures/linked_lists/base_linked_list.rb
Normal file
0
data_structures/linked_lists/base_linked_list.rb
Normal file
|
@ -16,7 +16,7 @@ class CircularList
|
||||||
@head = nil
|
@head = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert_end(value)
|
def insert_tail(value)
|
||||||
newNode = Node.new(value)
|
newNode = Node.new(value)
|
||||||
if @head.nil?
|
if @head.nil?
|
||||||
@head = newNode
|
@head = newNode
|
||||||
|
@ -59,8 +59,10 @@ class CircularList
|
||||||
puts(STDOUT.flush)
|
puts(STDOUT.flush)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_head
|
def delete_tail
|
||||||
if !@head.nil? && (@head.next != @head)
|
return if @head.nil?
|
||||||
|
|
||||||
|
if @head.next != @head
|
||||||
newHead = @head.next
|
newHead = @head.next
|
||||||
tempNode = newHead
|
tempNode = newHead
|
||||||
tempNode = tempNode.next while tempNode.next != @head
|
tempNode = tempNode.next while tempNode.next != @head
|
||||||
|
@ -88,17 +90,17 @@ end
|
||||||
|
|
||||||
obj = CircularList.new
|
obj = CircularList.new
|
||||||
|
|
||||||
obj.insert_end(1)
|
obj.insert_tail(1)
|
||||||
obj.insert_end(2)
|
obj.insert_tail(2)
|
||||||
obj.insert_end(3)
|
obj.insert_tail(3)
|
||||||
obj.insert_end(4)
|
obj.insert_tail(4)
|
||||||
obj.insert_end(5)
|
obj.insert_tail(5)
|
||||||
obj.print_list
|
obj.print_list
|
||||||
|
|
||||||
obj.insert_head(6)
|
obj.insert_head(6)
|
||||||
obj.print_list
|
obj.print_list
|
||||||
|
|
||||||
obj.delete_head
|
obj.delete_tail
|
||||||
obj.print_list
|
obj.print_list
|
||||||
|
|
||||||
obj.delete_tail
|
obj.delete_tail
|
||||||
|
|
|
@ -38,7 +38,7 @@ class DoublyLinkedList
|
||||||
|
|
||||||
# Add a node of value val before the first element of the linked list.
|
# Add a node of value val before the first element of the linked list.
|
||||||
# After the insertion, the new node will be the first node of the linked list.
|
# After the insertion, the new node will be the first node of the linked list.
|
||||||
def add_at_head(val)
|
def insert_head(val)
|
||||||
node = Node.new(val, nil, @head)
|
node = Node.new(val, nil, @head)
|
||||||
@tail = node unless @head
|
@tail = node unless @head
|
||||||
@head.prev = node if @head
|
@head.prev = node if @head
|
||||||
|
@ -47,8 +47,8 @@ class DoublyLinkedList
|
||||||
end
|
end
|
||||||
|
|
||||||
# Append a node of value val to the last element of the linked list.
|
# Append a node of value val to the last element of the linked list.
|
||||||
def add_at_tail(val)
|
def insert_tail(val)
|
||||||
return add_at_head(val) unless @head
|
return insert_head(val) unless @head
|
||||||
|
|
||||||
node = Node.new(val, @tail, nil)
|
node = Node.new(val, @tail, nil)
|
||||||
@tail.next = node
|
@tail.next = node
|
||||||
|
@ -62,8 +62,8 @@ class DoublyLinkedList
|
||||||
# will not be inserted.
|
# will not be inserted.
|
||||||
def add_at_index(index, val)
|
def add_at_index(index, val)
|
||||||
case index
|
case index
|
||||||
when 0 then add_at_head(val)
|
when 0 then insert_head(val)
|
||||||
when @size then add_at_tail(val)
|
when @size then insert_tail(val)
|
||||||
when 1...@size
|
when 1...@size
|
||||||
if index < @size - index
|
if index < @size - index
|
||||||
iter = @head
|
iter = @head
|
||||||
|
@ -166,10 +166,10 @@ end
|
||||||
obj = DoublyLinkedList.new
|
obj = DoublyLinkedList.new
|
||||||
obj.get(1)
|
obj.get(1)
|
||||||
|
|
||||||
obj.add_at_head(2)
|
obj.insert_head(2)
|
||||||
obj.print_values
|
obj.print_values
|
||||||
|
|
||||||
obj.add_at_tail(3)
|
obj.insert_tail(3)
|
||||||
obj.print_values
|
obj.print_values
|
||||||
|
|
||||||
obj.add_at_index(3, 5)
|
obj.add_at_index(3, 5)
|
||||||
|
|
Loading…
Reference in a new issue