Exemplo n.º 1
0
 def pop(self):
     last_stack = Stack()
     last_stack = self.get_last_stack()
     if last_stack:
         return last_stack.pop()
         if last_stack.is_empty():
             self.stacks.remove(last_stack)
     return
def sort_stack(stack):
    tmp_stack = Stack()

    while not stack.is_empty():
        val = stack.pop().data
        while not tmp_stack.is_empty():
            if val < tmp_stack.peek():
                # print("val (", val, ") < tmp_stack.peek() (", tmp_stack.peek(), ")")
                tmp = tmp_stack.pop().data
                stack.push(tmp)
            else:
                break
        tmp_stack.push(val)

    #     print("tmp_stack : ", end="")
    #     tmp_stack.print()
    #     print("stack : ", end="")
    #     stack.print()
    #
    # print("stack is empty? -", stack.is_empty())

    while not tmp_stack.is_empty():
        # val = tmp_stack.pop().data
        # print("val pushed back is ", val)
        # stack.push(val)
        stack.push(tmp_stack.pop().data)
Exemplo n.º 3
0
 def push(self, value):
     last_stack = Stack()
     last_stack = self.get_latest_stack()
     if last_stack.is_full():
         new_stack = Stack()
         new_stack.push(value)
         self.stacks.append(new_stack)
     else:
         last_stack.push(value)
     return
class MyQueue:
    def __init__(self):
        self.in_stack = Stack()
        self.out_stack = Stack()

    def push(self, value):
        self.in_stack.push(value)

    def pop(self):
        while self.in_stack.is_empty() is False:
            self.out_stack.push(self.in_stack.pop())
        return self.out_stack.pop()
Exemplo n.º 5
0
 def pop_at(self, index):
     stack_idx = Stack()
     stack_idx = self.stacks[index]
     popped = stack_idx.pop()
     if stack_idx.is_empty():
         self.stacks.remove(stack_idx)
     # elif len(self.stacks) > index + 1:
     for i in range(index+1, len(self.stacks), 1):
         stack_idx.push(self.stacks[i].pop())
         stack_idx = self.stacks[i]
     return  popped
 def __init__(self):
     self.in_stack = Stack()
     self.out_stack = Stack()
                stack.push(tmp)
            else:
                break
        tmp_stack.push(val)

    #     print("tmp_stack : ", end="")
    #     tmp_stack.print()
    #     print("stack : ", end="")
    #     stack.print()
    #
    # print("stack is empty? -", stack.is_empty())

    while not tmp_stack.is_empty():
        # val = tmp_stack.pop().data
        # print("val pushed back is ", val)
        # stack.push(val)
        stack.push(tmp_stack.pop().data)
    # print("stack end: ", end="")
    # stack.print()


q = Stack()
q.push(12)
q.push(52)
q.push(32)
q.push(465)
q.print()

sort_stack(q)
q.print()