def main(): a = Queue() a.enque(1) a.enque(2) a.enque(3) a.enque(4) print(a) a.deque() print(a) a.deque() print(a)
def ac_3(node, graph, available_colors, neighbours_assigned_colors): test_available_colors = {} need_to_backtrack = False for color in available_colors[node]: test_available_colors = {} for k, v in available_colors.items(): test_available_colors[k] = v test_available_colors[node] = color arc_already_queued = {} queue = Queue() for neighbour in graph.get(node): arc = (neighbour, node) queue.enque(arc) arc_already_queued[arc] = True print arc_already_queued while not queue.is_empty(): arc = queue.deque() arc_already_queued[arc] = False state = remove_inconsistent_values(arc, test_available_colors) removed = state[0] need_to_backtrack = state[1] if removed: for neighbour in graph.get(arc[0]): adj_arc = (neighbour, arc[0]) if not arc_already_queued.get(adj_arc): queue.enque(adj_arc) arc_already_queued[adj_arc] = True if need_to_backtrack: break print 'need to backtrack: ', need_to_backtrack print 'test available colors: ', test_available_colors if not need_to_backtrack: break available_colors = {} for k, v in test_available_colors.items(): available_colors[k] = v neighbours_assigned_colors[node] = True for neighbour in graph.get(node): if not neighbours_assigned_colors.get(neighbour): node = neighbour break counter = 0 for k, v in test_available_colors.items(): if len(test_available_colors.get(k)) == 1: counter += 1 print 'next node ', node print 'available colors: ', available_colors if counter == len(available_colors): return True print 'counter: ', counter ac_3(node, graph, available_colors, neighbours_assigned_colors)
def bfs(root, item): if root is None: return False else: queue = Queue() queue.enqueue(root) while not queue.is_empty(): root = queue.peek() print(root.value) if root.value == item: return True queue.deque() if root.left is not None: queue.enqueue(root.left) if root.right is not None: queue.enqueue(root.right)
from Queue import Queue q = Queue() while True: a = input('please input >') if a == 'end': print("end") break if a == 'push': b = input('please number input >') q.enque(b) if a == 'pop': try: print(q.deque()) except: print("empty") if a == 'head': try: print(q.head()) except: print("empty") if a == 'size': print(q.size()) q.consoleOut()