def infix_to_postfix(self): """ Prevod infixniho vyrazu na postfixni """ data = self.get_data() infix_arr = [] postfix_arr = [] val = "" for item in data: if self.is_operation(item): if val != "": infix_arr.append(float(val)) infix_arr.append(item) val = "" elif item == "(": infix_arr.append(item) elif item == ")": if val != "": infix_arr.append(float(val)) infix_arr.append(item) val = "" else: val += item if val != "": infix_arr.append(float(val)) stack = Stack() for item in infix_arr: if self.is_operation(item): if self.get_operation_pritorty( item) < self.get_operation_pritorty(stack.first()): for op in stack.to_list(): if op == "(": continue postfix_arr.append(op) stack.clear() stack.push(item) elif item == "(": stack.push(item) elif item == ")": while stack.first() != "(": op = stack.pop() postfix_arr.append(op) else: postfix_arr.append(item) for op in stack.to_list(): if op == "(": continue postfix_arr.append(op) return " ".join(map(str, postfix_arr))
class Queue(object): """ Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue) """ def __init__(self): self.stack1 = Stack() self.stack2 = Stack() def enQueue(self, item): self.stack1.push(item) def deQueue(self): if len(self.stack2) == 0: if len(self.stack1) == 0: return None while self.stack1.isEmpty() != True: self.stack2.push(self.stack1.pop()) return self.stack2.pop() def peek(self): if len(self.stack2) == 0: return self.stack1.first() else: return self.stack2.peek()