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_extend_With_Predictable_Elements( unittest.TestCase): ''' Test the extend operation of LinkedList. a) Create 2 lists. b) Add the second list to the end of the first list. c) Check the indices of the newly added elements. ''' def setUp(self): self._doubly_linkedList = DoublyLinkedList() self._doubly_linkedList.append(1) self._doubly_linkedList.append(2) self._doubly_linkedList.append(3) self._doubly_linkedList.append(4) #create a second linkedlist with 5, 6, 7 self._secondList = DoublyLinkedList() self._secondList.append(5) self._secondList.append(6) self._secondList.append(7) def test_extend_of_list(self): self.assertEqual(self._doubly_linkedList.length, 4, 'Original list before extend must be 4') count_before_extend = self._doubly_linkedList.length next_index_will_be = count_before_extend self._doubly_linkedList.extend(self._secondList) #check new length self.assertEqual(self._doubly_linkedList.length, count_before_extend + self._secondList.length, 'New length after extend must add up.') #check if the elements of shadow list are present for element in self._secondList: self.assertEqual( self._doubly_linkedList.index(element), next_index_will_be, 'Indices of new elements after extend must add up') next_index_will_be += 1 def tearDown(self): self._doubly_linkedList = None self._secondList = None
print('Reversed List is now %s' % elements_in_list) #length of the list print('Length of the list is %s' % len(double_link_list)) if __name__ == '__main__': double_link_list = DoublyLinkedList() #add one element print('Adding element 1 to list') double_link_list.append(1) #we add the following list (iterable) of elements print('Adding elements 2 ,3 , 4, 5, 6, 7, 8, 9, 10') elements = [2, 3, 4, 5, 6, 7, 8, 9, 10] double_link_list.extend(elements) #head print('Head of list is: %s' % str(double_link_list.head)) #tail print('Tail of list is: %s' % str(double_link_list.tail)) print('Print list by iterating') #Find an element's index element = 4 element_index = double_link_list.index(element) if element_index == -1: print('Element %s was not found on list' % str(element)) else: print('Element %s found at index %s' %