Implement a doubly linked list.
Like an array, a linked list is a simple linear data structure. Several common data types can be implemented using linked lists, like queues, stacks, and associative arrays.
A linked list is a collection of data elements called nodes. In a singly linked list each node holds a value and a link to the next node. In a doubly linked list each node also holds a link to the previous node.
You will write an implementation of a doubly linked list. Implement a Node to hold a value and pointers to the next and previous nodes. Then implement a List which holds references to the first and last node and offers an array-like interface for adding and removing items:
push
(insert value at back);pop
(remove value at back);shift
(remove value at front).unshift
(insert value at front);To keep your implementation simple, the tests will not cover error
conditions. Specifically: pop
or shift
will never be called on an
empty list.
If you want to know more about linked lists, check Wikipedia.
For installation and learning resources, refer to the Ruby resources page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby linked_list_test.rb
To include color from the command line:
ruby -r minitest/pride linked_list_test.rb
Classic computer science topic
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun'
require_relative 'linked_list'
class DequeTest < Minitest::Test
def test_push_pop
deque = Deque.new
deque.push(10)
deque.push(20)
assert_equal 20, deque.pop
assert_equal 10, deque.pop
end
def test_push_shift
skip
deque = Deque.new
deque.push(10)
deque.push(20)
assert_equal 10, deque.shift
assert_equal 20, deque.shift
end
def test_unshift_shift
skip
deque = Deque.new
deque.unshift(10)
deque.unshift(20)
assert_equal 20, deque.shift
assert_equal 10, deque.shift
end
def test_unshift_pop
skip
deque = Deque.new
deque.unshift(10)
deque.unshift(20)
assert_equal 10, deque.pop
assert_equal 20, deque.pop
end
def test_example
skip
deque = Deque.new
deque.push(10)
deque.push(20)
assert_equal 20, deque.pop
deque.push(30)
assert_equal 10, deque.shift
deque.unshift(40)
deque.push(50)
assert_equal 40, deque.shift
assert_equal 50, deque.pop
assert_equal 30, deque.shift
end
def test_pop_to_empty
deque = Deque.new
deque.push(10)
assert_equal 10, deque.pop
deque.push(20)
assert_equal 20, deque.shift
end
def test_shift_to_empty
deque = Deque.new
deque.unshift(10)
assert_equal 10, deque.shift
deque.unshift(20)
assert_equal 20, deque.pop
end
end
class Deque
class Node
attr_reader :value
attr_accessor :prev_node, :next_node
def initialize(value, prev_node: self, next_node: self)
@value = value
@prev_node = prev_node
@next_node = next_node
end
end
private_constant :Node
def initialize
@head = nil
end
def push(value)
if head
tail = head.prev_node
new_tail = Node.new(value, prev_node: tail, next_node: head)
tail.next_node = new_tail
head.prev_node = new_tail
else
self.head = Node.new(value)
end
end
def pop
if (tail = head.prev_node) == head
self.head = nil
else
head.prev_node = tail.prev_node
tail.prev_node.next_node = head
end
tail.value
end
def shift
if (shifted_head = head) == head.next_node
self.head = nil
else
self.head = shifted_head.next_node
head.prev_node = shifted_head.prev_node
head.prev_node.next_node = head
end
shifted_head.value
end
def unshift(value)
if head
new_head = Node.new(value, prev_node: head.prev_node, next_node: head)
head.prev_node = new_head
self.head = new_head
else
self.head = Node.new(value)
end
end
private
attr_accessor :head
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments