예제 #1
0
class DoublyDoublyLinkedList_Test_Insert_At_Index_In_Middle_With_4_Elements(
        unittest.TestCase):
    def setUp(self):
        self._doubly_linkedList = DoublyLinkedList()
        self._doubly_linkedList.append(1)
        self._doubly_linkedList.append(2)
        self._doubly_linkedList.append(4)
        self._doubly_linkedList.append(5)

    def test_insert_at_negative_index_of_empty_list(self):
        expected_length = self._doubly_linkedList.length + 1
        element_to_insert = 3
        insert_at_index = 2
        self._doubly_linkedList.insert_at(
            insert_at_index,
            element_to_insert)  # list will become 1, 2, 3, 4, 5
        self.assertEqual(
            self._doubly_linkedList.length, expected_length,
            'list length did not tally after insert in the middle')
        self.assertEqual(self._doubly_linkedList.index(3), insert_at_index,
                         'index of new element is not where we insert it at!')

        #iterate and check
        expected_list = []
        for element in self._doubly_linkedList:
            expected_list.append(element)
        self.assertEqual(
            expected_list, [1, 2, 3, 4, 5],
            'List was not same as expected after insert At index')

    def tearDown(self):
        self._doubly_linkedList = None
예제 #2
0
class DoublyDoublyLinkedList_Test_Insert_At_Index_Larger_Than_Length_With_4_Elements(
        unittest.TestCase):
    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)

    def test_insert_at_negative_index_of_empty_list(self):
        expected_length = self._doubly_linkedList.length + 1
        element_to_insert = 20
        self._doubly_linkedList.insert_at(100, element_to_insert)
        self.assertEqual(
            self._doubly_linkedList.length, expected_length,
            'list length did not tally after insert at huge index')
        self.assertEqual(
            self._doubly_linkedList.tail, element_to_insert,
            'list tail must be same as new element inserted at huge index')
        self.assertEqual(self._doubly_linkedList.index(20),
                         expected_length - 1,
                         'index of new element did not add up')

    def tearDown(self):
        self._doubly_linkedList = None
예제 #3
0
class DoublyDoublyLinkedList_Test_Insert_At_0_With_0_Elements(
        unittest.TestCase):
    def setUp(self):
        self._doubly_linkedList = DoublyLinkedList()

    def test_insert_at_0_of_empty_list(self):
        self._doubly_linkedList.insert_at(0, 20)
        self.assertEqual(
            self._doubly_linkedList.length, 1,
            'list length must be 1 after inserting to empty list')
        self.assertEqual(
            self._doubly_linkedList.head, self._doubly_linkedList.tail,
            'list length must be same node after inserting to empty list')

    def tearDown(self):
        self._doubly_linkedList = None
예제 #4
0
class DoublyDoublyLinkedList_Test_Insert_At_Index_Larger_Than_Length_With_0_Elements(
        unittest.TestCase):
    def setUp(self):
        self._doubly_linkedList = DoublyLinkedList()

    def test_insert_at_negative_index_of_empty_list(self):
        self._doubly_linkedList.insert_at(100, 20)
        self.assertEqual(
            self._doubly_linkedList.length, 1,
            'list length must be 1 after inserting to empty list at huge index'
        )
        self.assertEqual(
            self._doubly_linkedList.head, self._doubly_linkedList.tail,
            'list length must be same node after inserting to empty list at huge index'
        )

    def tearDown(self):
        self._doubly_linkedList = None
예제 #5
0
    #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)