def dfs(self, rootId): """ Execute a Depth-First Search (DFS) in the graph starting from the specified node. :param rootId: the root node ID (integer). :return: the DFS list of nodes. """ # if the root does not exists, return None if rootId not in self.nodes: return None # DFS nodes initialization dfs_nodes = [] # queue initialization s = Stack() s.push(rootId) explored = {rootId} # nodes already explored while not s.isEmpty(): # while there are nodes to explore ... node = s.pop() # get the node from the stack explored.add(node) # mark the node as explored # add all adjacent unexplored nodes to the stack for adj_node in self.getAdj(node): if adj_node not in explored: s.push(adj_node) dfs_nodes.append(node) return dfs_nodes
def iterativeQuickSort(l, left, right, det = False): """ :param l: the list to sort. :param left: the left-most position. :param right: the right-most position. :param det: determinism. :return: void. """ theStack = Stack() theStack.push(left) theStack.push(right) while not theStack.isEmpty(): right = theStack.pop() left = theStack.pop() if __debug__: print ("quickSortIter-step({},{})".format(left, right)) if right <= left: continue mid = partition(l, left, right, det) theStack.push(left) theStack.push(mid - 1) theStack.push(mid + 1) theStack.push(right)
def __init__(self, value): self._root = NodeTree() self._size = 0 self._nodeAux = NodeTree() self._size = self._size + 1 self._root.__set_value__(value) self._stack = Stack() self._stack.__push__(value, 0)
def reverse(string): myStack = Stack(len(string)) for i in string: myStack.push(i) result = "" while not myStack.isEmpty(): result += myStack.pop() return result
def dtob(decimal, base = 2): myStack = Stack() while decimal > 0: myStack.push(decimal % base) decimal //= base # 나머지 result = "" while not myStack.isEmpty(): result += str(myStack.pop()) return result
def addTwoNumbers(l1, l2): l1_node = l1 stack1 = Stack() while l1_node: stack1.push(l1_node.val) l1_node = l1_node.next l2_node = l2 stack2 = Stack() while l2_node: stack2.push(l2_node.val) l2_node = l2_node.next result = ListNode(None) s = 0 while stack1.size() > 0 and stack2.size() > 0: num1 = stack1.pop() num2 = stack2.pop() node = ListNode(((num1 + num2 + s) % 10)) node.next = result.next result.next = node s = int((num1 + num2 + s) / 10) stack = stack1 if stack1.size() > 0 else stack2 while stack.size() > 0: num = stack.pop() node = ListNode(((num + s) % 10)) node.next = result.next result.next = node s = int((num + s) / 10) if s > 0: node = ListNode(s) node.next = result.next result.next = node return result.next
def check_bal_par(par_str): if not len(par_str): return True if len(par_str) % 2 != 0: return False s = Stack() for item in par_str: if s.is_empty(): s.push(item) else: if s.peek() == '(' and item == ')': s.pop() else: s.push(item) return s.is_empty()
def parseParenthesis(string): balanced = 1 index = 0 myStack = Stack(len(string)) while (index < len(string)) and (balanced == 1): check = string[index] if check == "(": myStack.push(check) else: if myStack.isEmpty(): balanced = 0 else: myStack.pop() index += 1 if balanced == 1 and myStack.isEmpty(): return True else: return False
def is_valid(self, s): array_stack = Stack() str_list = list(s) for index, char in enumerate(str_list): if char == '(' or char == '{' or char == '[': array_stack.push(char) else: if array_stack.is_empty(): return False top_char = array_stack.pop() if char == ')' and top_char != '(': return False if char == ']' and top_char != '[': return False if char == '}' and top_char != '{': return False return array_stack.is_empty()
def producer(stack): for i in range(0, 6): data = 'Task' + str(i) print('Producer pushing:', data) stack.push(data) sleep(2) def consumer(label, stack): while True: print(label, 'stack.pop():', stack.pop()) print('Create shared stack') stack = Stack() print('Stack:', stack) print('Creating and starting consumer threads') consumer1 = Thread(target=consumer, args=('Consumer1', stack)) consumer2 = Thread(target=consumer, args=('Consumer2', stack)) consumer3 = Thread(target=consumer, args=('Consumer3', stack)) consumer1.start() consumer2.start() consumer3.start() print('Creating and starting producer thread') producer = Thread(target=producer, args=[stack]) producer.start()
from stack.Stack import Stack __author__ = 'lijiayan' if __name__ == '__main__': s = Stack() s.push(1) s.push(2) s.push(3) for i in range(s.size()): print(s.pop()) print("栈大小:" + str(s.size()))
def infixToPostfix(myExp, myStack): postFix = [] for i in range(len(myExp)): if (isOperand(myExp[i])): postFix.append(myExp[i]) elif (myExp[i] == "("): myStack.push(myExp[i]) elif (myExp[i] == ")"): topOperator = myStack.pop() while (not myStack.isEmpty() and topOperator != "("): postFix.append(topOperator) topOperator = myStack.pop() else: while (not myStack.isEmpty()) and (precedence(myExp[i]) <= precedence((myStack.peek()))): postFix.append(myStack.pop()) myStack.push(myExp[i]) while (not myStack.isEmpty()): postFix.append(myStack.pop()) return " ".join(postFix) if __name__ == "__main__": myExp = 'a+b*(c^d-e)^(f+g*h)-i' myExp = [i for i in myExp] print("Infix: ", " ".join(myExp)) myStack = Stack(len(myExp)) print("Postfix: ", infixToPostfix(myExp, myStack))