Python data structure ----- leetcode232. Implement queue with stack

 

Table of contents

 

Foreword:

Method explanation

Example:

 Code

 232. Implement Queue with Stack


Foreword:

        We all know that the characteristic of the queue is first-in-first-out, just like queuing, first-come-first-served, and the characteristic of the stack is last-in-last-out, so how do we realize the function of a queue through two stacks here? Let's learn together this time.

Method explanation

        Here you need to prepare two empty stacks, one as the stack stack_in for storing data, and the other as the stack stack_out for popping data. When there is data coming in, put the data into stack_in at this time, when there is data coming out, first check whether there is data in the stack stack_out, if there is data, then pop the data directly, otherwise Pop the data in the stack stack_in into the stack stack_out in turn until the stack stack_in is empty, then you can pop the stack stack_out. The above process is two stacks to realize the function of a queue.

The diagram is as follows: 

Example:

 Realize the functions of a, b, c, and d in sequence such as teaming and dequeueing

 Step 1: Put a, b, c, and d in the stack stack_in in turn

 

 Step 2: Pop the data in the stack stack_in out of the stack one by one, and then as in the stack stack_out

Step 3: At this time, it is only necessary to pop the stack stack_out, and the data after popping are a, b, c, d in order, which just satisfies the function of the queue.

 Code

class MyQueue(object):
    def __init__(self):
        '''初始化两个空栈'''
        self.__stack_in=[]
        self.__stack_out=[]
    def isempty(self):
        '''判断队列是否为空'''
        if not self.__stack_in and not self.__stack_out:
            return True
        return False

    def inqueue(self,data):
        '''入队'''
        self.__stack_in.append(data)
    def dequeue(self):
        '''出队'''
        if self.__stack_out:#判断栈stack_out是否空
            return self.__stack_out.pop()
        else:
            if not self.__stack_in:
                return -1 #如果都队列为空的话就返回-1
            else:
                while self.__stack_in:
                    self.__stack_out.append(self.__stack_in.pop())
            return self.__stack_out.pop()
    def showqueue(self):
        return self.__stack_out[::-1]+self.__stack_in

 232. Implement Queue with Stack

 Please use only two stacks to implement a first-in-first-out queue. The queue should support all operations supported by general queues (push, pop, peek, empty):

Implement the MyQueue class:

void push(int x) pushes element x to the end of the queue
int pop() removes from the beginning of the queue and returns the element
int peek() returns the element at the beginning of the queue
boolean empty() returns true if the queue is empty; otherwise, Return false
Description:

You can only use standard stack operations -- that is, only push to top, peek/pop from top, size, and is empty operations are legal.
Your language may not support stacks. You can use list or deque (double-ended queue) to simulate a stack, as long as it is a standard stack operation.

Example: 

输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]

解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

code show as below: 

class MyQueue:

    def __init__(self):
        self.stack_a=[]
        self.stack_b=[]

    def push(self, x: int) -> None:
        self.stack_a.append(x)

    def pop(self) -> int:
        if self.empty():
            return None
        if self.stack_b:
            return self.stack_b.pop()
        while self.stack_a:
            self.stack_b.append(self.stack_a.pop())
        return self.stack_b.pop()

    def peek(self) -> int:
        a=self.pop()
        self.stack_b.append(a)阿
        return a

    def empty(self) -> bool:
        return not (self.stack_a or self.stack_b) 


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()

 Well, the above is the whole content of this issue, see you in the next issue!

share a wallpaper

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/130172793