Skip to content

Commit

Permalink
simpler linked list example
Browse files Browse the repository at this point in the history
  • Loading branch information
vosechu committed Jun 16, 2013
1 parent 51f1fb9 commit 8cd253e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
25 changes: 25 additions & 0 deletions assignments/ruby/simple-linked-list/example.rb
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
44 changes: 44 additions & 0 deletions assignments/ruby/simple-linked-list/test.rb
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
13 changes: 13 additions & 0 deletions assignments/shared/simple-linked-list.md
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
4 changes: 4 additions & 0 deletions assignments/shared/simple-linked-list.yml
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"

0 comments on commit 8cd253e

Please sign in to comment.