コード例 #1
0
    def test_insert(self):
        """
        test insert()
        """
        # test on empty linked list
        list1 = LinkedList()
        list1.insert(10, 3)
        list_arr = list(list1)
        self.assertEqual(list_arr, [])

        # test on linked list with 1 node
        list1.insert(20, 0)
        list1.insert(30, 3)
        list_arr = list(list1)
        self.assertEqual(list_arr, [20])

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums)

        # insert in the middle
        insert_idx = 3
        insert_val = 100
        list1.insert(insert_val, insert_idx)

        # compare to reference
        list_arr = list(list1)
        reference_arr = reference_arr[:insert_idx] + [insert_val] + reference_arr[insert_idx:]
        self.assertEqual(list_arr, reference_arr)
コード例 #2
0
    def test_insert_end(self):
        """
        test insert_end()
        """
        # test insert_end on empty linked list
        list1 = LinkedList()
        key = 10
        list1.insert_end(key)
        self.assertEqual(list(list1), [key])

        # generate linked list using insert_end
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums)
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)
コード例 #3
0
ファイル: queue_list.py プロジェクト: IlyaMichlin/cs_basics
class Queue:
    """
    Queue using Circular Double Linked List
    """
    def __init__(self):
        self.queue = LinkedList()
        self.is_full = False
        self.is_empty = True

    def __len__(self):
        return len(self.queue)

    def enqueue(self, key, data=None):
        """
        add an item to the queue
        :param key: key to add
        :param data: new node data
        """
        self.queue.insert_end(key, data)

    def dequeue(self):
        """
        remove an item from the queue
        :return: item key
        """
        node = self.queue.fild_last_node()
        self.queue.remove_beginning()

        if self.queue.head is None:
            self.is_empty = True
        else:
            self.is_empty = False

        if node is None:
            return None
        return node.key

    def peek(self):
        """
        gets the element at the front of the queue without removing it
        :return: item key
        """
        node = self.queue.head
        if node is None:
            return None
        return node.key
コード例 #4
0
    def test_insert_beginning(self):
        """
        test insert_beginning()
        """
        # test insert on empty linked list
        list1 = LinkedList()
        key = 10
        idx = 10
        list1.insert(key, idx)
        self.assertEqual(list(list1), [])

        # generate linked list
        list1 = LinkedList()
        list1.insert(10, 0)
        list1.insert(20, 1)
        list1.insert(30, 1)
        reference_arr = [10, 30, 20]

        # test insert
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)
コード例 #5
0
    def test_find_node(self):
        """
        test find_node()
        """
        # test search on empty linked list
        list1 = LinkedList()
        node = list1.find_node(10)
        self.assertEqual(node, None)

        # generate linked list
        nums = 10
        list1 = generate_filled_list(LinkedList, nums, with_ref=False, as_str=True)

        # test search on value not in linked list
        node = list1.find_node(nums)
        self.assertEqual(node, None)

        # test search on value in the linked list
        search_node = '3'
        node = list1.find_node(search_node)
        self.assertEqual(node.key, search_node)
コード例 #6
0
    def test_len(self):
        """
        test __len__()
        """
        # test on empty linked list
        list1 = LinkedList()
        self.assertEqual(len(list1), 0)

        # generate linked list
        nums = 10
        list1 = generate_filled_list(LinkedList, nums, with_ref=False)

        # test len()
        self.assertEqual(len(list1), nums)
コード例 #7
0
    def test_fild_last_node(self):
        """
        test fild_last_node()
        """
        # test search on empty linked list
        list1 = LinkedList()
        node = list1.fild_last_node()
        self.assertEqual(node, None)

        # generate linked list
        nums = 1
        list1 = generate_filled_list(LinkedList, nums, with_ref=False)

        # test for finding last node with 1 node
        last_node = list1.fild_last_node()
        self.assertEqual(last_node.key, nums - 1)

        # generate linked list
        nums = 10
        list1 = generate_filled_list(LinkedList, nums, with_ref=False)

        # test for finding last node
        last_node = list1.fild_last_node()
        self.assertEqual(last_node.key, nums - 1)
コード例 #8
0
    def test_remove_by_value(self):
        """
        test remove by value
        """
        # test on empty linked list
        list1 = LinkedList()
        list1.remove_by_value(10)

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums, as_str=True)

        # test removing value not present in linked list
        list1.remove_by_value('100')
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)

        # test remove node containing value from middle of linked list
        list1.remove_by_value('3')
        list_arr = list(list1)
        reference_arr = reference_arr[:3] + reference_arr[4:]
        self.assertEqual(list_arr, reference_arr)
コード例 #9
0
    def test_remove_by_index(self):
        """
        test remove_by_index()
        """
        # test on empty linked list
        list1 = LinkedList()
        list1.remove_by_index(10)

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums)

        # test removing index outside of linked list
        list1.remove_by_index(100)
        list_arr = list(list1)
        self.assertEqual(list_arr, reference_arr)

        # test remove index from middle of linked list
        list1.remove_by_index(3)
        list_arr = list(list1)
        reference_arr = reference_arr[:3] + reference_arr[4:]
        self.assertEqual(list_arr, reference_arr)
コード例 #10
0
    def test_remove_end(self):
        """
        test remove_end()
        """
        # test on empty linked list
        list1 = LinkedList()
        list1.remove_end()

        # generate linked list
        nums = 10
        list1, reference_arr = generate_filled_list(LinkedList, nums)

        # remove from beginning
        list1.remove_end()
        list_arr = list(list1)

        # compare results to reference
        reference_arr = reference_arr[:-1]
        self.assertEqual(list_arr, reference_arr)
コード例 #11
0
ファイル: queue_list.py プロジェクト: IlyaMichlin/cs_basics
 def __init__(self):
     self.queue = LinkedList()
     self.is_full = False
     self.is_empty = True