class AnimalQueue(object):
    """
    A Queue to store animals(cats and dogs)
    Attributes:
        dogs(LinkedList): A linked list to store dogs
        cats(LinkedList): A linked list to store cats
        order(Integer): Time when an animal is added to the queue
    """
    def __init__(self):
        self.dogs = LinkedList()
        self.cats = LinkedList()
        self.order = 0

    def enqueue(self, a):
        """
        Enqueues an animal to the queue
        Args:
            a(Animal): Animal to add to the queue
        """
        a.set_order(self.order)
        self.order += 1
        if type(a).__name__ == 'Dog':
            self.dogs.append(Element(a))
        elif type(a).__name__ == 'Cat':
            self.cats.append(Element(a))

    def dequeue(self):
        """
        Removes an animal from the queue
        Returns:
            Animal: Returned animal
        """
        if self.dogs.size() == 0:
            return self.dequeue_cats()
        elif self.cats.size() == 0:
            return self.dequeue_dogs()

        if self.dogs.peek().value.is_older_than(self.cats.peek().value):
            return self.dequeue_dogs()
        else:
            return self.dequeue_cats()

    def dequeue_dogs(self):
        return self.dogs.remove_head()

    def dequeue_cats(self):
        return self.cats.remove_head()
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
e5 = Element(5)
e6 = Element(6)
e7 = Element(7)
e8 = Element(8)
e9 = Element(9)
e10 = Element(10)
e11 = Element(11)
e12 = Element(12)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e4)
ll.append(e5)
ll.append(e6)
ll.append(e7)
ll.append(e8)
ll.append(e9)
ll.append(e10)
ll.append(e11)
ll.append(e12)

delete_node(ll.head.next.next)
delete_node(ll.head.next.next.next)
print(ll.size())
Esempio n. 3
0
    elif length == 1:
        return Result(head.next, True)
    elif length == 2:
        return Result(head.next.next, head.value == head.next.value)
    res = check_palindrome(head.next, length - 2)
    if not res.result or res.node is None:
        return res
    else:
        res.result = (head.value == res.node.value)
        res.node = res.node.next
    return res


e1 = Element(0)
e2 = Element(1)
e3 = Element(2)
e7 = Element(3)
e8 = Element(2)
e4 = Element(1)
e5 = Element(0)

ll = LinkedList(e1)
ll.append(e2)
ll.append(e3)
ll.append(e7)
ll.append(e8)
ll.append(e4)
ll.append(e5)

p = check_palindrome(ll.head, ll.size())
print(p.result)