File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "python.linting.enabled" : false ,
3+ "python.pythonPath" : " /usr/local/bin/python3.6"
4+ }
Original file line number Diff line number Diff line change 1+ class Node (object ):
2+ def __init__ (self , data , next = None ):
3+ self .next = next
4+ self .data = data
5+
6+ class LinkedList (object ):
7+ def __init__ (self , head = None ):
8+ self .head = head
9+
10+ def insert (self , data ):
11+ new_head = Node (data , self .head )
12+ self .head = new_head
13+
14+ def append (self , data ):
15+ new_node = Node (data )
16+
17+ if self .head is None :
18+ self .head = new_node
19+ return
20+
21+ current_node = self .head
22+
23+ while current_node .next is not None :
24+ current_node = current_node .next
25+
26+ current_node .next = new_node
27+
28+
29+ def is_empty (self ):
30+ return self .head == None
Original file line number Diff line number Diff line change 1+ import unittest
2+ from linked_list import *
3+
4+ class LinkedListTests (unittest .TestCase ):
5+
6+ def test_empty_list_should_have_no_head (self ):
7+ list = LinkedList ()
8+ self .assertIsNone (list .head )
9+
10+ def test_adding_one_value_should_create_head (self ):
11+ list = LinkedList ()
12+ list .insert ("First" )
13+ self .assertIsNotNone (list .head )
14+ self .assertEqual (list .head .data , "First" )
15+
16+ def test_inserting_second_value_should_replace_head (self ):
17+ list = LinkedList ()
18+ list .insert ("First" )
19+ list .insert ("Second" )
20+ self .assertEqual (list .head .data , "Second" )
21+
22+ def test_inserting_second_value_should_move_existing_head_to_next (self ):
23+ list = LinkedList ()
24+ list .insert ("First" )
25+ list .insert ("Second" )
26+ self .assertEqual (list .head .next .data , "First" )
27+
28+ def test_appending_first_value_should_set_head (self ):
29+ list = LinkedList ()
30+ list .append ("First" )
31+ self .assertEqual (list .head .data , "First" )
32+
33+ def test_appending_second_value_should_set_head_next (self ):
34+ list = LinkedList ()
35+ list .append ("First" )
36+ list .append ("Second" )
37+ self .assertEqual (list .head .next .data , "Second" )
38+
39+ def test_is_empty_should_be_true_if_no_head (self ):
40+ list = LinkedList ()
41+ self .assertTrue (list .is_empty ())
42+
43+ if __name__ == '__main__' :
44+ unittest .main ()
You can’t perform that action at this time.
0 commit comments