Exemplo n.º 1
0
def merge_linkedlists(alist: LinkedList, blist: LinkedList) -> LinkedList:
    newlist = LinkedList()
    p1, p2 = alist.head, blist.head
    k1, k2 = 0, 0

    while p1.next and p2.next:
        v1, v2 = p1.next.data, p2.next.data
        if v1 >= v2:
            newlist.append(v1)
            p1 = p1.next
            k1 += 1
        else:
            newlist.append(v2)
            p2 = p2.next
            k2 += 1

    if p1 is not None and p2 is None:
        newlist.tail.next = p2
        newlist.tail = blist.tail
        newlist._size += len(blist) - k2
    elif p1 is None and p2 is not None:
        newlist.tail.next = p1
        newlist.tail = alist.tail
        newlist._size += len(alist) - k1

    return newlist
Exemplo n.º 2
0
class Queue(object):
    def __init__(self):
        self.items = LinkedList()

    def __str__(self):
        s = ', '.join(map(str, self.items))
        return f'Queue([{s}]<)'

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

    def enqueue(self, item):
        """Items enqueue in the rear(tail) of the list"""
        self.items.append(item)

    def dequeue(self):
        """Items dequeue from the front(head) of the list"""
        return self.items.popleft()

    def isEmpty(self):
        return len(self) == 0