mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
commit
4eb1a37084
2 changed files with 160 additions and 0 deletions
80
data_structures/LinkedLists/DoubleList.rb
Normal file
80
data_structures/LinkedLists/DoubleList.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
# Define a node in the list
|
||||
class Node
|
||||
attr_accessor :value, :next, :prev
|
||||
def initialize(value)
|
||||
@value = value
|
||||
@next = nil
|
||||
@prev = nil
|
||||
end
|
||||
end
|
||||
|
||||
# A Class for double linked lists (each element links to the next one, and to the previous one)
|
||||
class DoubleList
|
||||
attr_accessor :head, :tail
|
||||
def initialize()
|
||||
@head = nil
|
||||
@tail = nil
|
||||
end
|
||||
|
||||
def insert_tail(value)
|
||||
new_node = Node.new(value)
|
||||
if (@head == nil)
|
||||
@head = new_node
|
||||
@tail = new_node
|
||||
else
|
||||
@tail.next = new_node
|
||||
new_node.prev = @tail
|
||||
@tail = new_node
|
||||
end
|
||||
end
|
||||
|
||||
def insert_head(value)
|
||||
new_node = Node.new(value)
|
||||
if (@head == nil)
|
||||
@head = new_node
|
||||
@tail = new_node
|
||||
else
|
||||
new_node.next = @head
|
||||
@head.prev = new_node
|
||||
@head = new_node
|
||||
end
|
||||
end
|
||||
|
||||
def delete_tail()
|
||||
if (@tail != nil)
|
||||
@tail = @tail.prev
|
||||
if (@tail != nil)
|
||||
@tail.next = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def delete_head()
|
||||
if (@head != nil)
|
||||
@head = @head.next
|
||||
if (@head != nil)
|
||||
@head.prev = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def print_list()
|
||||
print "["
|
||||
if (@head != nil)
|
||||
printNode = @head
|
||||
while (printNode != nil)
|
||||
print "#{printNode.value}"
|
||||
if (printNode != @tail)
|
||||
print ", "
|
||||
end
|
||||
printNode = printNode.next
|
||||
end
|
||||
end
|
||||
print "]"
|
||||
STDOUT.flush
|
||||
end
|
||||
|
||||
def is_empty()
|
||||
return (@head==nil)
|
||||
end
|
||||
end
|
80
data_structures/LinkedLists/SingleList.rb
Normal file
80
data_structures/LinkedLists/SingleList.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
# Define a node in the list
|
||||
class Node
|
||||
attr_accessor :value, :next
|
||||
def initialize(value)
|
||||
@value = value
|
||||
@next = nil
|
||||
end
|
||||
end
|
||||
|
||||
# A Class for single linked lists (each element links to the next one, but not to the previous one)
|
||||
|
||||
class SingleList
|
||||
attr_accessor :head
|
||||
def initialize()
|
||||
@head = nil
|
||||
end
|
||||
|
||||
def insert_tail(value)
|
||||
newNode = Node.new(value)
|
||||
if (@head == nil)
|
||||
@head = newNode
|
||||
else
|
||||
tempNode = @head
|
||||
while (tempNode.next != nil)
|
||||
tempNode = tempNode.next
|
||||
end
|
||||
tempNode.next = newNode
|
||||
end
|
||||
end
|
||||
|
||||
def insert_head(value)
|
||||
newNode = Node.new(value)
|
||||
if (@head == nil)
|
||||
@head = newNode
|
||||
else
|
||||
newNode.next = @head
|
||||
@head = newNode
|
||||
end
|
||||
end
|
||||
|
||||
def print_list()
|
||||
print "["
|
||||
if (@head != nil)
|
||||
printNode = @head
|
||||
while (printNode != nil)
|
||||
print "#{printNode.value}"
|
||||
if (printNode.next != nil)
|
||||
print ", "
|
||||
end
|
||||
printNode = printNode.next
|
||||
end
|
||||
end
|
||||
print "]"
|
||||
STDOUT.flush
|
||||
end
|
||||
|
||||
def delete_head
|
||||
if (@head != nil) && (@head.next != nil)
|
||||
newHead = @head.next
|
||||
@head = newHead
|
||||
elsif (@head != nil) && (@head.next == nil)
|
||||
@head = nil
|
||||
end
|
||||
end
|
||||
|
||||
def delete_tail
|
||||
if (@head != nil)
|
||||
tempNode = @head
|
||||
while (tempNode.next.next != nil)
|
||||
tempNode = tempNode.next
|
||||
end
|
||||
tempNode.next = nil
|
||||
end
|
||||
end
|
||||
|
||||
def isEmpty()
|
||||
return (@head==nil)
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue