class DoublyDoublyLinkedList_Test_Remove_Duplicates_With_Recurring_Duplicates( unittest.TestCase): def setUp(self): #build [2, 1, 1, 1, 3, 1, 1, 1, 4] self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(2) element_to_append = 1 self._doubly_linkedList.extend( [element_to_append, element_to_append, element_to_append]) self._doubly_linkedList.append(3) self._doubly_linkedList.extend( [element_to_append, element_to_append, element_to_append]) self._doubly_linkedList.append(4) def test_remove_duplicates_of_list_with_recurring_duplicates(self): expected_head = self._doubly_linkedList._head expected_tail = self._doubly_linkedList._tail self._doubly_linkedList.remove_duplicates() expected_length = 4 self.assertEqual( self._doubly_linkedList.length, expected_length, 'list length did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._head, expected_head, 'list head did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._tail, expected_tail, 'list tail did not add up after removing duplicates') def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Remove_Duplicates_With_Duplicates_At_Tail( unittest.TestCase): def setUp(self): #build [2, 1, 1, 1] self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(2) element_to_append = 1 self._doubly_linkedList.extend( [element_to_append, element_to_append, element_to_append]) def test_remove_duplicates_of_list_with_2_identical_elements(self): expected_head = self._doubly_linkedList._head expected_tail = self._doubly_linkedList._head.next_node self._doubly_linkedList.remove_duplicates() expected_length = 2 self.assertEqual( self._doubly_linkedList.length, expected_length, 'list length did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._head, expected_head, 'list head did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._tail, expected_tail, 'list tail did not add up after removing duplicates') def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Remove_Duplicates_With_0_Elements( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() def test_remove_duplicate_of_empty_list(self): self._doubly_linkedList.remove_duplicates() def tearDown(self): self._doubly_linkedList = None
class DoublyDoublyLinkedList_Test_Remove_Duplicates_With_1_Elements( unittest.TestCase): def setUp(self): self._doubly_linkedList = DoublyLinkedList() element_to_append = 1 self._doubly_linkedList.append(element_to_append) def test_remove_duplicate_of_list_with_1_element(self): old_head = self._doubly_linkedList._head expected_tail = old_head self._doubly_linkedList.remove_duplicates() expected_length = 1 self.assertEqual( self._doubly_linkedList.length, expected_length, 'list length did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._head, old_head, 'list head did not add up after removing duplicates') self.assertEqual(self._doubly_linkedList._tail, expected_tail, 'list tail did not add up after removing duplicates') def tearDown(self): self._doubly_linkedList = None
#Lets delete 7 print('Deleting 7') element_to_remove = 7 double_link_list.remove(element_to_remove) #Try to find it. print('Trying to find %s' % str(element_to_remove)) index_of_deleted = double_link_list.index(element_to_remove) print('Index of deleted element is %s' % str(index_of_deleted)) #print list again print_list(double_link_list) # #Lets insert 7 at index 5 print('Insert 7 into the list at index 4') element_to_insert = 7 index_at = 5 double_link_list.insert_at(index=index_at, element=element_to_insert) print_list(double_link_list) # Removing duplicates print('Removing duplicates') double_link_list = DoublyLinkedList() double_link_list.extend([1, 1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 10, 10]) print_list(double_link_list) double_link_list.remove_duplicates() print_list(double_link_list) #Reverse print list using reversed print_reverse_list(double_link_list)