def test_single_linked_list():
    # FillConstant
    @pytest.fixture
    def element_A():
        return {
            "event_tracking_name": "VISIT",
            "event_trigger": "HOMEPAGE",
            "client_timestamp": 12879818279387,
        }

    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(3)
    node4 = ListNode(4)
    node5 = ListNode(5)
    node6 = ListNode(6)
    node7 = ListNode(7)
    node8 = ListNode(8)
    node9 = ListNode(9)

    nodeA = ListNode("A")
    nodeB = ListNode("B")
    nodeC = ListNode("C")

    link1 = SingleLinkedList()
    link1.add_list_item(node1)
    link1.add_list_item(node2)
    link1.add_list_item(node3)
    link1.add_list_item(node4)
    link1.add_list_item(node5)
    link1.add_list_item(node6)
    link1.add_list_item(node7)
    link1.add_list_item(node8)
    link1.add_list_item(node9)

    assert link1.list_length() == 9

    print("remove value 5")
    link1.remove_by_value(5)
    assert link1.list_length() == 8
    assert link1.head.data == 1
    assert link1.tail.data == 9

    link1.remove_by_value(9)
    assert link1.list_length() == 7
    assert link1.head.data == 1
    assert link1.tail.data == 8

    res = link1.search(3)
    assert res == [3]
Exemplo n.º 2
0
class Queue:
    def __init__(self):
        # initialize a linked list to store item

        self.linked_list = SingleLinkedList()
        return

    def enqueue(self, item):
        # put the item to the tail of queue

        self.linked_list.add_list_item(item)
        return

    def dequeue(self):
        # to return the first item of the queue and remove it

        if self.linked_list.head is not None:
            current_node = self.linked_list.head
            self.linked_list.head = self.linked_list.head.next
            return current_node.data

    def empty(self):
        # to clear all the items in the queue

        self.linked_list.head = None
        return

    def size(self):
        # to return the length of the queue

        size = 0
        current_node = self.linked_list.head

        while current_node is not None:
            size = size + 1
            current_node = current_node.next

        return size

    def output_list(self):
        # outputs the queue (the value of the node, actually)

        self.linked_list.output_list()
        return