示例#1
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)
示例#2
0
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