forked from exercism/exercism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def linked_list(ary) | ||
head = nil | ||
tail = nil | ||
|
||
ary.each do |datum| | ||
current_tail = tail | ||
tail = Element.new(datum) | ||
current_tail.next = tail unless current_tail.nil? | ||
|
||
if head.nil? | ||
head = tail | ||
end | ||
end | ||
|
||
return head | ||
end | ||
|
||
class Element | ||
attr_reader :datum | ||
attr_accessor :next | ||
|
||
def initialize(datum) | ||
@datum = datum | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
|
||
require_relative 'linked_list' | ||
|
||
class LinkedListTest < MiniTest::Unit::TestCase | ||
def setup | ||
@head = linked_list([1]) | ||
end | ||
|
||
def test_head | ||
assert_equal Element, @head.class | ||
end | ||
|
||
def test_head_datum | ||
skip | ||
assert_equal 1, @head.datum | ||
end | ||
|
||
def test_add_next | ||
skip | ||
@head.next = Element.new(2) | ||
|
||
refute_nil @head.next | ||
assert_equal Element, @head.next.class | ||
assert_equal 2, @head.next.datum | ||
end | ||
end | ||
|
||
class LinkedListRangeTest < MiniTest::Unit::TestCase | ||
def setup | ||
@head = linked_list((1..10).to_a) | ||
end | ||
|
||
def test_head_next | ||
skip | ||
assert_equal 2, @head.next.datum | ||
end | ||
|
||
def test_10_deep | ||
skip | ||
assert_equal 10, @head.next.next.next.next.next.next.next.next.next.datum | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
The linked list is a fundamental data structure in computer science so it's pretty important that all students who seek jobs at the larger companies know some data structures for the interviews. In Ruby we don't actually use linked lists very often, they come from a time when all Arrays needed to be sequentially located in memory which is not the case in the post-modern languages (though we still try at the VM level for performance reasons). | ||
|
||
As a first take, lets create a linked list with just Element objects containing the range (1..10). Next we'll use the Proxy pattern to make sure that things are done correctly and make it feel Rubyish. | ||
|
||
Examples: | ||
head = linked_list([1]) | ||
head #=> <Element @datum=1, @next=nil> | ||
head.next = Element.new(2) | ||
head #=> <Element @datum=1, @next=<Element...>> | ||
head.datum #=> 1 | ||
head.next.datum #=> 2 | ||
head = linked_list((1..10).to_a) | ||
head.next.next.next.next.next.next.next.next.next.next.datum #=> 10 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
blurb: "Write a simple linked list implementation that uses Elements and a loop" | ||
source: "Inspired by 'Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby', singly linked-lists." | ||
source_url: "http://www.brpreiss.com/books/opus8/html/page96.html#SECTION004300000000000000000" |