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
|
||||
end
|
||||
|
||||
def insert_end(value)
|
||||
def insert_tail(value)
|
||||
newNode = Node.new(value)
|
||||
if @head.nil?
|
||||
@head = newNode
|
||||
|
@ -59,8 +59,10 @@ class CircularList
|
|||
puts(STDOUT.flush)
|
||||
end
|
||||
|
||||
def delete_head
|
||||
if !@head.nil? && (@head.next != @head)
|
||||
def delete_tail
|
||||
return if @head.nil?
|
||||
|
||||
if @head.next != @head
|
||||
newHead = @head.next
|
||||
tempNode = newHead
|
||||
tempNode = tempNode.next while tempNode.next != @head
|
||||
|
@ -88,17 +90,17 @@ end
|
|||
|
||||
obj = CircularList.new
|
||||
|
||||
obj.insert_end(1)
|
||||
obj.insert_end(2)
|
||||
obj.insert_end(3)
|
||||
obj.insert_end(4)
|
||||
obj.insert_end(5)
|
||||
obj.insert_tail(1)
|
||||
obj.insert_tail(2)
|
||||
obj.insert_tail(3)
|
||||
obj.insert_tail(4)
|
||||
obj.insert_tail(5)
|
||||
obj.print_list
|
||||
|
||||
obj.insert_head(6)
|
||||
obj.print_list
|
||||
|
||||
obj.delete_head
|
||||
obj.delete_tail
|
||||
obj.print_list
|
||||
|
||||
obj.delete_tail
|
||||
|
|
|
@ -38,7 +38,7 @@ class DoublyLinkedList
|
|||
|
||||
# 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.
|
||||
def add_at_head(val)
|
||||
def insert_head(val)
|
||||
node = Node.new(val, nil, @head)
|
||||
@tail = node unless @head
|
||||
@head.prev = node if @head
|
||||
|
@ -47,8 +47,8 @@ class DoublyLinkedList
|
|||
end
|
||||
|
||||
# Append a node of value val to the last element of the linked list.
|
||||
def add_at_tail(val)
|
||||
return add_at_head(val) unless @head
|
||||
def insert_tail(val)
|
||||
return insert_head(val) unless @head
|
||||
|
||||
node = Node.new(val, @tail, nil)
|
||||
@tail.next = node
|
||||
|
@ -62,8 +62,8 @@ class DoublyLinkedList
|
|||
# will not be inserted.
|
||||
def add_at_index(index, val)
|
||||
case index
|
||||
when 0 then add_at_head(val)
|
||||
when @size then add_at_tail(val)
|
||||
when 0 then insert_head(val)
|
||||
when @size then insert_tail(val)
|
||||
when 1...@size
|
||||
if index < @size - index
|
||||
iter = @head
|
||||
|
@ -166,10 +166,10 @@ end
|
|||
obj = DoublyLinkedList.new
|
||||
obj.get(1)
|
||||
|
||||
obj.add_at_head(2)
|
||||
obj.insert_head(2)
|
||||
obj.print_values
|
||||
|
||||
obj.add_at_tail(3)
|
||||
obj.insert_tail(3)
|
||||
obj.print_values
|
||||
|
||||
obj.add_at_index(3, 5)
|
||||
|
|
Loading…
Reference in a new issue