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.
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
raise Exception
, you should write:
raise Exception("Meaningful message indicating the source of the error")
To run the tests, run the appropriate command below (why they are different):
py.test linked_list_test.py
pytest linked_list_test.py
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
python -m pytest linked_list_test.py
pytest
options-v
: enable verbose output-x
: stop running tests on first failure--ff
: run failures from previous test before running other test casesFor other options, see python -m pytest -h
Note that, when trying to submit an exercise, make sure the solution is in the $EXERCISM_WORKSPACE/python/linked-list
directory.
You can find your Exercism workspace by running exercism debug
and looking for the line that starts with Workspace
.
For more detailed information about running tests, code style and linting, please see Running the Tests.
Classic computer science topic
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import unittest
from linked_list import LinkedList
class LinkedListTest(unittest.TestCase):
def test_push_pop(self):
lst = LinkedList()
lst.push(10)
lst.push(20)
self.assertEqual(lst.pop(), 20)
self.assertEqual(lst.pop(), 10)
def test_push_shift(self):
lst = LinkedList()
lst.push(10)
lst.push(20)
self.assertEqual(lst.shift(), 10)
self.assertEqual(lst.shift(), 20)
def test_unshift_shift(self):
lst = LinkedList()
lst.unshift(10)
lst.unshift(20)
self.assertEqual(lst.shift(), 20)
self.assertEqual(lst.shift(), 10)
def test_unshift_pop(self):
lst = LinkedList()
lst.unshift(10)
lst.unshift(20)
self.assertEqual(lst.pop(), 10)
self.assertEqual(lst.pop(), 20)
def test_all(self):
lst = LinkedList()
lst.push(10)
lst.push(20)
self.assertEqual(lst.pop(), 20)
lst.push(30)
self.assertEqual(lst.shift(), 10)
lst.unshift(40)
lst.push(50)
self.assertEqual(lst.shift(), 40)
self.assertEqual(lst.pop(), 50)
self.assertEqual(lst.shift(), 30)
@unittest.skip("extra-credit")
def test_length(self):
lst = LinkedList()
lst.push(10)
lst.push(20)
self.assertEqual(len(lst), 2)
lst.shift()
self.assertEqual(len(lst), 1)
lst.pop()
self.assertEqual(len(lst), 0)
@unittest.skip("extra-credit")
def test_iterator(self):
lst = LinkedList()
lst.push(10)
lst.push(20)
iterator = iter(lst)
self.assertEqual(next(iterator), 10)
self.assertEqual(next(iterator), 20)
if __name__ == '__main__':
unittest.main()
class Node(object):
def __init__(self, value, succeeding=None, previous=None):
self.value = value
self.succeeding = succeeding
self.previous = previous
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def push(self, value):
new_node = Node(value)
self.length += 1
if self.tail is not None:
self.tail.succeeding = new_node
new_node.previous = self.tail
self.tail = new_node
if self.head is None:
self.head = new_node
def pop(self):
self.length -= 1
if self.tail is not None:
popped_node = self.tail
self.tail = popped_node.previous
return popped_node.value
def unshift(self, value):
new_node = Node(value)
self.length += 1
if self.head is not None:
self.head.previous = new_node
new_node.succeeding = self.head
self.head = new_node
if self.tail is None:
self.tail = new_node
def shift(self):
self.length -= 1
if self.head is not None:
shifted_node = self.head
self.head = shifted_node.succeeding
return shifted_node.value
def __len__(self):
return self.length
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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments