Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linked lists #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .Rhistory
Empty file.
6 changes: 6 additions & 0 deletions array-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def max
return @storage[biggest]
end

def sort
sorted_array = []
(@storage.length-1).times do |stuff|
if @storage[stuff+1] > @storage[stuff]
end

def empty?
@size == 0
end
Expand Down
112 changes: 90 additions & 22 deletions linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def add(value)
if @size == 0
@head = Node.new(value,nil)
@size += 1
end
else
# Traverse to the end of the list
# And insert a new node over there with the specified value
current = @head
Expand All @@ -31,31 +31,33 @@ def add(value)
end
current.next_node = Node.new(value,nil)
@size += 1
end
self
end

def delete(val)
return nil if @size == 0
if @head.value == val
# If the head is the element to be delete, the head needs to be updated
@head = @head.next_node
@size -= 1
else
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
# ... x->z
previous = @head
current = @head.next_node
while current != nil && current.value != val
previous = current
current = current.next_node
end

if current != nil
previous.next_node = current.next_node
@size -= 1
end
end
return nil if @size == 0
if @head.value == val
# If the head is the element to be delete, the head needs to be updated
@head = @head.next_node
@size -= 1
else
# ... x -> y -> z
# ... x -> y -> z
# Suppose y is the value to be deleted, you need to reshape the above list to :
# ... x->z
previous = @head
current = @head.next_node
while current != nil && current.value != val
previous = current
current = current.next_node
end

if current != nil
previous.next_node = current.next_node
@size -= 1
end
end
end

def display
Expand Down Expand Up @@ -83,6 +85,72 @@ def size
return @size
end

def sort
count = 0
previous = @head
current = @head.next_node
nxt = current.next_node
size % 2 == 0? counter = (size/2) : counter = (size/2)+1

while count < counter
# // edge case at the end.. if at the end of the list
if (current.next_node == nil)
count = 0
previous = @head
current = @head.next_node
nxt = current.next_node
end

# // edge case at the front .. if at the head & the current value is less than the head
if (previous == @head) && (current.value < previous.value)
# reassign the pointers
current.next_node, previous.next_node = previous, current.next_node
# reassign the node pointers
current, previous, @head = previous, current, current
end

# if current is greater than next value
if (nxt.value < current.value)
# if at the end
if nxt.next_node != nil
current.next_node, previous.next_node, nxt.next_node = nxt.next_node, nxt, current
previous, nxt = nxt, current.next_node
else
previous.next_node, nxt.next_node, current.next_node = nxt, current, nil
end

# if current is less than next value
elsif (nxt.value > current.value)
count += 1
# if at the end
if (nxt.next_node != nil)
previous, current, nxt = current, nxt, nxt.next_node
else
current, current.next_node = current.next_node, nil
end
end
end
# return display
end




def reverse
previous = @head
current = previous.next_node
previous.next_node = nil #set the beginning to be the end

until (current.next_node == nil)
# change the pointer to the one before it
# move node pointers one down the list
current.next_node, previous, current = previous, current, current.next_node
end
# set the last node to head and point to the previous
current.next_node, @head= previous, current
end


def max
return nil if @size == 0
max = @head.value
Expand Down
20 changes: 20 additions & 0 deletions test-linked-list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@
ll.display
puts "Size = #{ll.size}"
puts "Max = #{ll.max}"

puts "Please Sort"
ll.sort
ll.display

puts "Reverse Reverse"
ll.reverse
ll.display

puts "Adds 123"
ll.add(123)
ll.display

puts "Please Sort"
ll.sort
ll.display

puts "Reverse Reverse"
ll.reverse
ll.display