Created
April 11, 2016 13:11
-
-
Save br3nt/c95286d33cfc06c999859d9fecb68c57 to your computer and use it in GitHub Desktop.
Linked List Implementation in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a new list | |
list = ListNode.new | |
# add some items to the head or tail | |
list.add_to_head(Node.new('Hello')) | |
list.add_to_tail(Node.new('World')) | |
list.to_a # => ["Hello", "World"] | |
# remove items from the head | |
list.head.remove | |
list.to_a # => ["World"] | |
list.head == list.tail # => true | |
# remove items from the tail | |
list.tail.remove | |
list.to_a # => [] | |
# setup for enumeration example | |
list = ListNode.new | |
list.add_to_head(Node.new(1)) | |
list.add_to_head(Node.new(2)) | |
list.add_to_head(Node.new(3)) | |
list.add_to_head(Node.new(4)) | |
# list each value | |
list.each {|node| puts node.value } | |
# select all nodes with values greater than 2 | |
list.each.select {|node| node.value > 2 } | |
# find the first node with a value of 4 | |
list.each.find {|node| node.value == 4 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListNode < Node | |
protected :remove # prevent incorrect access to Node methods | |
def initialize | |
@next = @prev = self | |
end | |
def head | |
@next unless @next == self | |
end | |
def tail | |
@prev unless @prev == self | |
end | |
def add_to_head(node) | |
node.insert_after(self) | |
end | |
def add_to_tail(node) | |
node.insert_after(self.prev) | |
end | |
def each(&block) | |
return enum_for(:each) unless block_given? | |
node = @next | |
while node != self | |
yield node | |
node = node.next | |
end | |
end | |
def to_a | |
each.collect {|node| node.value } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
protected | |
attr_writer :prev, :next | |
public | |
attr_reader :value, :prev, :next | |
def initialize(value) | |
@value = value | |
end | |
def remove | |
@prev.next = @next if @prev | |
@next.prev = @prev if @next | |
@next = @prev = nil | |
end | |
def insert_after(node) | |
remove | |
@next = node.next | |
@next.prev = self if @next | |
@prev = node | |
node.next = self | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
head = Node.new(1) | |
middle = Node.new(2) | |
middle.insert_after(head) | |
tail = Node.new(3) | |
tail.insert_after(middle) | |
middle.remove |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment