def push(self, data): new_node = single_node(data) new_node.next = self.stack_head self.stack_head = new_node if data <= self.min_head.data: new_min = single_node(data) new_min.next = self.min_head self.min_head = new_min
def push(self, data): ''' Push data onto the top of the stack ''' new_node = linked_list.single_node(data) new_node.next = self.head self.head = new_node
def push(self, data): n = self.set_of_stacks[-1].length if n > self.stack_height_limit: new_node = single_node(data) new_stack = stack_with_length(new_node) self.set_of_stacks.append(new_stack) else: self.set_of_stacks[-1].push(data)
def enqueue(self, data): new_node = linked_list.single_node(data) if self.length: self.tail.next = new_node self.tail = new_node else: self.head = new_node self.tail = new_node self.length += 1
def recursive_solution(node, k): if node is None: return (0, node) next_node = node.next count, returned_node = recursive_solution(next_node, k) if count == k: return (count, returned_node) else: count += 1 return (count, node) if __name__ == "__main__": print "Test" head = single_node(1) linked_list = singly_linked_list(head) linked_list.add(2) linked_list.add(3) linked_list.add(4) linked_list.add(5) linked_list.add(6) print "Input List: %s"%str(linked_list) print "Expected output: 2" trivial_soln = trivial_solution(linked_list, 2).data print "Trivial Solution: %d"%trivial_soln runner_soln = runner_solution(linked_list, 2).data print "Runner Solution: %d"%runner_soln
n = self.set_of_stacks[-1].length if n > self.stack_height_limit: new_node = single_node(data) new_stack = stack_with_length(new_node) self.set_of_stacks.append(new_stack) else: self.set_of_stacks[-1].push(data) def pop(self): popped_node = self.set_of_stacks[-1].pop() if self.set_of_stacks[-1].length == 0: self.set_of_stacks.pop() return popped_node.data if __name__ == "__main__": first_node = single_node(1) ss = set_of_stacks(first_node, 2) ss.push(2) ss.push(3) ss.push(4) ss.push(5) print "Test" print "There should be two stacks" print "There are %d stacks\n"%len(ss.set_of_stacks) rv = ss.pop() print "Expected 5, Popped %d"%rv print "There should be two stacks" print "There are %d stacks\n"%len(ss.set_of_stacks) rv = ss.pop() print "Expected 4, Popped %d"%rv print "There should be one stacks"
def recursive_solution(node, k): if node is None: return (0, node) next_node = node.next count, returned_node = recursive_solution(next_node, k) if count == k: return (count, returned_node) else: count += 1 return (count, node) if __name__ == "__main__": print "Test" head = single_node(1) linked_list = singly_linked_list(head) linked_list.add(2) linked_list.add(3) linked_list.add(4) linked_list.add(5) linked_list.add(6) print "Input List: %s" % str(linked_list) print "Expected output: 2" trivial_soln = trivial_solution(linked_list, 2).data print "Trivial Solution: %d" % trivial_soln runner_soln = runner_solution(linked_list, 2).data print "Runner Solution: %d" % runner_soln
new_min.next = self.min_head self.min_head = new_min def pop(self): popped_head = self.stack_head self.stack_head = popped_head.next if popped_head.data <= self.min_head.data: self.min_head = self.min_head.next return popped_head def min(self): return self.min_head.data if __name__ == "__main__": s = stack_with_min(stack_node_with_min(data=1)) s.push(2) s.push(3) print "Test for solution 1:" print "Expected Min: 1" print "Min in stack: %d\n"%s.min() s = two_stack(single_node(1)) s.push(2) s.push(3) print "Test for solution 2:" print "Expected Min: 1" print "Min in stack: %d"%s.min()