示例#1
0
def reverse_queue(queue, k):
    stack = MyStack()
    if k > queue.size() or k <= 0:
        return None

    lst_reverse = queue.queue_list[0:k]
    stack.stack_list = lst_reverse
    result = []
    for i in range(stack.size()):
        result.append(stack.pop())
    return result + queue.queue_list[k:]
if __name__ == "__main__":
    # Creating and populating the stack
    stack = MyStack()
    stack.push(2)
    stack.push(97)
    stack.push(4)
    stack.push(42)
    stack.push(12)
    stack.push(60)
    stack.push(23)
    # Sorting the stack
    stack = sort_stack(stack)
    # Printing the sorted stack
    print("Stack after sorting")
    print([stack.pop() for i in range(stack.size())])


class MyStack:
    def __init__(self):
        self.stack_list = []
        self.stack_size = 0

    def is_empty(self):
        return self.stack_size == 0

    def peek(self):
        if self.is_empty():
            return None
        return self.stack_list[-1]