Example #1
0
    def copy(self):
        q = Queue()
        temp = LinkedList()
        current = self.head

        if self.head is None:
            return

        while current is not None:
            q.enqueue(deepcopy(current.getData()))
            current = current.next

        for item in q.items:
            temp.add(item)

        return temp
Example #2
0
def main():

    q = Queue()
    a = Queue()
    b = Queue()

    n = read_int()

    for i in range(0, n):

        nome_operacao = input("Nome e Operação (separados por espaço) --> ")
        nome_operacao = nome_operacao.split()

        q.enqueue(nome_operacao[1])
        q.enqueue(nome_operacao[0])

    print("Início:")
    print("q: ", q)
    print("a: ", a)
    print("b: ", b)

    process(q, a, b)

    print("-------------------------------")
    print("Final:")
    print("q: ")
    print("a: ", a)
    print("b: ", b)
Example #3
0
def merge(a, b):
    sa = Stack()
    sb = Stack()

    #   Inserir os items de cada queue em stacks independentes
    for i in a.items:
        sa.push(i)

    for j in b.items:
        sb.push(j)

    #   Comparar os 2 stacks
    sc = Stack()
    while not sa.isEmpty() and not sb.isEmpty():
        if sa.peek() < sb.peek():
            tmp = sa.pop()
            sc.push(tmp)
            tmp = sb.pop()
            sc.push(tmp)
        elif sb.peek() < sa.peek():
            tmp = sb.pop()
            sc.push(tmp)
            tmp = sa.pop()
            sc.push(tmp)

    #   Virar o stack ao contrario
    sd = Stack()
    while not sc.isEmpty():
        tmp = sc.pop()
        sd.push(tmp)

    #   Inserir o stack temporario na queue final
    c = Queue()
    for y in sd.items:
        c.enqueue(y)

    return c
Example #4
0
def main():

    a = Queue()
    b = Queue()

    # items1 = input("Elementos da primeira fila (ordenados e separados por espaço) --> ")

    items1 = [5, 10, 30]
    for itema in items1:
        a.enqueue(itema)

    # items2 = input("Elementos da segunda fila (ordenados e separados por espaço) --> ")

    items2 = [6, 11, 31]
    for itemb in items2:
        b.enqueue(itemb)

    print("a: ", a.items)
    print("b: ", b.items)
    print("c:", merge(a, b))
Example #5
0
def simulation(numSeconds, pagesPerMinute, num_Alunos):

    labprinter = Printer(pagesPerMinute)
    printQueue = Queue()
    waitingtimes = []

    for currentSecond in range(numSeconds):

        if newPrintTask():
            task = Task(currentSecond)
            printQueue.enqueue(task)

        if (not labprinter.busy()) and (not printQueue.isEmpty()):
            nexttask = printQueue.dequeue()
            waitingtimes.append(nexttask.waitTime(currentSecond))
            labprinter.startNext(nexttask)

        labprinter.tick()

    averageWait=sum(waitingtimes)/len(waitingtimes)
    print("Average Wait %6.2f secs %3d tasks remaining."%(averageWait,printQueue.size()))
Example #6
0
def hot_potato(namelist, num):
    simqueue = Queue()
    for name in namelist:
        simqueue.enqueue(name)

    while simqueue.size() > 1:
        for i in range(num):
            simqueue.enqueue(simqueue.dequeue())

        simqueue.dequeue()

    return simqueue.dequeue()