def test_node(): our_node = Node("first") assert our_node.data == "first" assert our_node.next is None our_other_node = Node("second", our_node) assert our_other_node.next is our_node
def test_3(self): """ check that nodes are added to top of stack """ s = Stack() n1 = Node(5) n2 = Node(4) s.push(n1) s.push(n2) self.assertEqual(s.top._value, 4)
def test_7(self): """ check that len returns length of stack """ s = Stack() n1 = Node(5) n2 = Node(4) s.push(n1) s.push(n2) self.assertEqual(len(s), 2)
def test_3(self): """test that second item pushed to stack is top""" n1 = Node('A') n2 = Node('B') s = Stack() s.push(n1) s.push(n2) self.assertEqual(s.top._value, 'B')
def test_4(self): """test that pop returns top of stack""" n1 = Node('A') n2 = Node('B') s = Stack() s.push(n1) s.push(n2) returned = s.pop() self.assertEqual(returned._value, 'B')
def test_5(self): """test that top of stack changes after pop""" n1 = Node('A') n2 = Node('B') s = Stack() s.push(n1) s.push(n2) s.pop() self.assertEqual(s.top._value, 'A')
def test_4(self): """ check that nodes are removed from top of stack """ s = Stack() n1 = Node(5) n2 = Node(4) s.push(n1) s.push(n2) s.pop() self.assertEqual(s.top._value, 5)
def push(self, data: int): super().push(data) if self.min_stack.head: head: Node = self.min_stack.head node: Node = Node(head.data) if head.data <= data else Node(data) node.next = self.min_stack.head self.min_stack.head = node else: self.min_stack.head = Node(data)
def push(self, item): if not self.stack_now: self._addStack() node = Node(item) node.next = self.stack_now.top self.stack_now.top = node self.stack_now_size += 1 if self._checkIfFull(): self._archiveStack()
def push(self, item): if self.top: min_so_far = self.mins[-1] if min_so_far > item: self.mins.append(item) else: self.mins.append(min_so_far) else: self.mins.append(item) node = Node(item) node.next = self.top self.top = node
def enqueue(self, value): new_node = Node(value) if self.back is None: self.front = self.back = new_node else: self.back.next_ = new_node self.back = new_node
def push(self, value): if not self.head: self.head = Node(value) self.min_stack.add_to_stack(value) else: # value is MIN if value < self.get_min(): self.min_stack.add_to_stack(value) # take node at head current_head = self.head # assign new node to head with next_node = previous head self.head = Node(value) self.head.next_node = current_head self.length += 1
def test_6(self): """test that using pop on one node stack works""" n1 = Node('A') s = Stack() s.push(n1) s.pop() self.assertEqual(s.top, None)
def test_end_to_end(self): print('Test: Empty stack') stack = Stack() self.assertAlmostEqual(len(stack), 0) self.assertEqual(stack.peek(), None) self.assertEqual(stack.pop(), None) print('Test: One element') top = Node(5) stack = Stack(top) self.assertEqual(len(stack), 1) self.assertEqual(stack.pop(), 5) self.assertEqual(stack.peek(), None) print('Test: More than one element') stack = Stack() stack.push(1) stack.push(2) stack.push(3) self.assertEqual(len(stack), 3) self.assertEqual(stack.pop(), 3) self.assertEqual(len(stack), 2) self.assertEqual(stack.peek(), 2) self.assertEqual(stack.pop(), 2) self.assertEqual(len(stack), 1) self.assertEqual(stack.peek(), 1) self.assertEqual(stack.isEmpty(), False) self.assertEqual(stack.pop(), 1) self.assertEqual(len(stack), 0) self.assertEqual(stack.peek(), None) self.assertEqual(stack.isEmpty(), True) print('Success: test_end_to_end')
def __init__(self, values): if len(values) == 0: self.top = None return self.top = Node(values[-1]) if len(values) == 1: self.bottom = self.top prev = self.top for val in values[1::-1]: node = Node(val) node.prev = prev prev.next = node prev = node self.bottom = node
def test_end_to_end(self): print('Test: Empty stack') stack = Stack() assert_equal(stack.peek(), None) assert_equal(stack.pop(), None) print('Test: One element') top = Node(5) stack = Stack(top) assert_equal(stack.pop(), 5) assert_equal(stack.peek(), None) print('Test: More than one element') stack = Stack() stack.push(1) stack.push(2) stack.push(3) assert_equal(stack.pop(), 3) assert_equal(stack.peek(), 2) assert_equal(stack.pop(), 2) assert_equal(stack.peek(), 1) assert_equal(stack.is_empty(), False) assert_equal(stack.pop(), 1) assert_equal(stack.peek(), None) assert_equal(stack.is_empty(), True) print('Success: test_end_to_end')
def __init__(self, value=None): if value == None: self.head = None self.tail = None self.length = 0 else: self.head = Node(value) self.tail = self.head self.length = 1
def add(self, value): new_node = Node(value) if self.head == None: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = new_node self.length += 1
def trans(self, exp): """把算术表达式做成后缀式的""" postexp = [] op = Stack() op.push(Node("=")) # pass是跳过的意思,可以用来写大概框架 # ++++++++++ # 如果按下面的写,会有错 # 在for循环中的while让j+=1,并不能改变while外面j的值 # for j in range(len(exp)): j = 0 # 遍历表达式 while (j < len(exp)): # 前面要加上self # 如果是运算数,就直接将其放在postexp中,继续向下遍历,直到不是运算数,在postexp后面加上# if (self.isOP(exp[j]) is False): # 在while循环里面加上j<len(exp),因为当循环到字符串的最后一个字符时,再向下遍历,已经超出字符串, # 下标越界,再进行exp[j].isdigit()时,则会出现IndexError: string index out of range while (j < len(exp) and exp[j].isdigit()): postexp.append(exp[j]) j += 1 postexp.append("#") # 如果是运算符,就将该运算符和op栈顶的运算符进行优先级比较 else: # 这里是op.top.data(str类型),不是op.top(node类型) judge = self.precede(op.top.data, exp[j]) # 如果op栈顶运算符的优先级高,将栈顶运算符出栈并将其存入postexp中 if judge == -1: postexp.append(op.pop()) # 如果op栈顶运算符和该运算符的优先级一样,栈顶运算符出栈 if judge == 0: op.pop() j += 1 # 如果op栈顶运算符的优先级低,将该运算符入op栈 if judge == 1: op.push(Node(exp[j])) j += 1 # exp扫描完,op栈还有除了 “=”以外的运算符,将其所有存入postexp while (op.top.data != "="): postexp.append(op.pop()) # +++++++++++ return "".join(postexp)
def push(self, data: int) -> Optional[SetOfStacks]: print(data, end=" ") if self.count_current == self.stack_size: if self.current_stack == 0: print("Stack Overflow") return None else: self.count_current = 1 self.current_stack -= 1 self.head[self.current_stack] = Node(data) else: node: Node = Node(data) if self.head[self.current_stack]: node.next = self.head[self.current_stack] self.head[self.current_stack] = node self.count_current += 1 return self
def push(self, data: int): node = Node(data) if not self.head: self.head = node else: head = self.head while head.next: head = head.next head.next = node
def computVal(self, postexp): """计算后缀式的值""" vstack = Stack() n = [] d = 0 for char in postexp: isop = self.isOP(char) # ++++++++++++ # 运算数 if not isop: if (char != "#"): d = 10 * d + int(char) else: vstack.push(Node(d)) d = 0 # 运算符 else: num1 = vstack.pop() num2 = vstack.pop() vstack.push(Node(self.cal(num1, num2, char))) # ++++++++++++ return vstack.top.data
def balanced_parentheses(expression): open_parentheses = "({[" closed_parethese = ")}]" for brackets in expression: if brackets in open_parentheses: bracts = Node(brackets) obj.push(brackets) elif brackets in closed_parentheses: obj.pop() if obj.is_empty(): print("given expression is balanced") else: print("given expression is not balance")
def push(self, val): new = Node(val) new.next = self.top self.top.prev = new self.top = new
def test_1(self): """ check node creation """ n1 = Node(5) self.assertEqual(n1.value(), 5)
def test_6(self): """ check that returns value if only one node to pop """ s = Stack() n1 = Node(5) s.push(n1) self.assertEqual(s.pop(), 5)
def __init__(self, content=None, next=None, previous=None): self.previous = previous Node.__init__(self, content, next)
def test_1(self): """test creating a node""" n1 = Node('A') self.assertEqual(n1._value, 'A')
def test_2(self): """ check that nodes are added to stack """ s = Stack() n1 = Node(5) s.push(n1) self.assertEqual(s.top._value, 5)
def create_nodes(): n1, n2, n3 = Node(1), Node(2), Node('a') return n1, n2, n3
def __init__(self, top=None): super(StackMin, self).__init__(top) self.minimum_stack = Stack(Node(sys.maxsize))