S
Seth Eliot
Hi all,
I am new to Ruby but find it interesting. To teach myself the language
I wrote a simple linked list implementation. It works just fine, but I
suspect that my "Java is showing" and that the same logic can be written
in a much more Ruby-ish fashion.
So I'd appreciate it if you can show me the Ruby way of implementing the
following functionality.
(The one thing I know is that I am not really taking advantage of OOP by
making my methods in LinkedListSe.rb just static utility methods… that's
fine for now)
(also "next_1" is just my way of not colliding with the reserved keyword
"next")
File: ElemLL.rb
class ElemLL
attr_accessor :data, :next_1
@data
@next_1
End
File: LinkedListSe.rb
require 'ElemLL'
# Adds element to end of linked list
def addData(data, head=nil)
insertData(data, head)
end
# Inserts Element at requested index in linked list.
# Shifts the element currently at that position (if any) and any
subsequent
# elements to the right (adds one to their indices)
def insertData(data, head, index=nil)
# special case if list currently empty (not initialized)
return newList(data) unless head
# Weakly typed languages have their downsides?
return unless head.class == ElemLL
# Initial values for first element
i = 1
prev = nil
curr = head
next_1 = curr.next_1
# Iterate through elements until we reach insertion point (or end of
list)
while (curr && (!index || i<index))
prev=curr
curr = next_1;
next_1 = curr.next_1 if curr;
i=i.next
end
# when new element is inserted, the current curr will actually be
next
next_1 = curr
# Create the new element at curr
curr = ElemLL.new
curr.data = data
curr.next_1=next_1
# Set the pointer *to* the new element
if (prev)
prev.next_1 = curr
else
head=curr
end
return head
end
def traverse(head)
puts "\nLinked List contents:"
curr = head
while(curr)
puts " #{curr.data}"
curr = curr.next_1
end
end
def newList(data)
head = ElemLL.new
head.data = data
return head
end
I am new to Ruby but find it interesting. To teach myself the language
I wrote a simple linked list implementation. It works just fine, but I
suspect that my "Java is showing" and that the same logic can be written
in a much more Ruby-ish fashion.
So I'd appreciate it if you can show me the Ruby way of implementing the
following functionality.
(The one thing I know is that I am not really taking advantage of OOP by
making my methods in LinkedListSe.rb just static utility methods… that's
fine for now)
(also "next_1" is just my way of not colliding with the reserved keyword
"next")
File: ElemLL.rb
class ElemLL
attr_accessor :data, :next_1
@data
@next_1
End
File: LinkedListSe.rb
require 'ElemLL'
# Adds element to end of linked list
def addData(data, head=nil)
insertData(data, head)
end
# Inserts Element at requested index in linked list.
# Shifts the element currently at that position (if any) and any
subsequent
# elements to the right (adds one to their indices)
def insertData(data, head, index=nil)
# special case if list currently empty (not initialized)
return newList(data) unless head
# Weakly typed languages have their downsides?
return unless head.class == ElemLL
# Initial values for first element
i = 1
prev = nil
curr = head
next_1 = curr.next_1
# Iterate through elements until we reach insertion point (or end of
list)
while (curr && (!index || i<index))
prev=curr
curr = next_1;
next_1 = curr.next_1 if curr;
i=i.next
end
# when new element is inserted, the current curr will actually be
next
next_1 = curr
# Create the new element at curr
curr = ElemLL.new
curr.data = data
curr.next_1=next_1
# Set the pointer *to* the new element
if (prev)
prev.next_1 = curr
else
head=curr
end
return head
end
def traverse(head)
puts "\nLinked List contents:"
curr = head
while(curr)
puts " #{curr.data}"
curr = curr.next_1
end
end
def newList(data)
head = ElemLL.new
head.data = data
return head
end