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
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)
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
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))
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()))
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()