[C++ Data Structure]: Use queues to implement stacks, and use stacks to create queues

 [C++ Data Structure]: Use stack to create queue, and use queue to implement stack

1. Basic knowledge of stacks and queues

1. Stack:

2. Queue: 

3.Traversal rules of the two

4. Common STL interfaces

 2. Create a queue using a stack

3. Create a stack using a team

You're done! It’s not easy to write. If you succeed, please follow or like. Thank you~~


1. Basic knowledge of stacks and queues

The difference between hand-drawn stack and queue:

1. Stack:

2. Queue: 

 

3.Traversal rules of the two

Stack: Only the top elements can be accessed, and traversal one by one is not allowed.

Queue: Only the head and tail of the queue can be accessed and used, and traversal one by one is not allowed.

4. Common STL interfaces

栈:

stack<数据类型> name;    //构造

push(值);  //栈底添加

pop();    //出栈

top();    //返回栈顶元素

empty();  //判断栈是否为空

size();   //返回栈的大小

队列:

queue<类型> name;   //构造

push(值)    //队尾插入

pop()      //从队头移除一个元素

back()    //返回最后队尾第一个值

front()   //返回第一个值
    
empty()   //判断队列是否为空

size()    //返回队列大小

 2. Create a queue using a stack

It is recommended to study based on question 232 on Likou: 232. Use stack to implement queue

Simulate the process of entering and exiting the queue:

class MyQueue {
public:

    stack<int> queue_push;  //定义入栈相当于入队的操作
    stack<int> queue_back;  //定义出栈相当于出队的操作

    MyQueue() 
    {

    }
    
    void push(int x) 
    {
        queue_push.push(x);
    }
    
    int pop() 
    {
        if(queue_back.empty())  //如果为空则说明出队没有
        {
            while(!queue_push.empty())  //把入队中的所有数全部放入出队中
            {
                queue_back.push(queue_push.top()); //这个地方不能直接将pop()赋值传入,会报错
                queue_push.pop();
            }
        }
            int mid_num = queue_back.top();
            queue_back.pop();
            return mid_num;
    }
    
    int peek() 
    {
        int pop_num = this->pop();  //复用pop()中返回队头的第一个元素
        queue_back.push(pop_num);  //因为该功能只是返回但是不移除,因此还要放进去
        return pop_num;
    }
    
    bool empty() 
    {
        return (queue_back.empty() && queue_push.empty());
    }
};

 Note: In the peek function, it is not redefined, but the previous pop function is used. First, if it is rewritten, an error will be reported when submitting. Second, this can improve code reusability.

3. Create a stack using a team

It is recommended to study based on question 225 on Likou: 225. Use queue to implement stack

Simulate the process of pushing and popping into the stack: This can be achieved using a queue

class MyStack {
public:

    queue<int> queue_stack;

    MyStack() 
    {

    }
    
    void push(int x) 
    {
        queue_stack.push(x);
    }
    
    int pop() 
    {
        int peak = queue_stack.back();
        while(queue_stack.front() != peak)
        {
            queue_stack.push(queue_stack.front());
            queue_stack.pop();
        }
        int front_num = queue_stack.front();
        queue_stack.pop();
        return front_num;
    }
    
    int top() 
    {
        return queue_stack.back();
    }
    
    bool empty() 
    {
        return queue_stack.empty(); 
    }
};

 I wrote it myself so it's a bit complicated, but at least it's done hehe. If you have any optimizations, please discuss in the comment area! !

You're done! It’s not easy to write. If you succeed, please follow or like. Thank you~~


Guess you like

Origin blog.csdn.net/Callme_TeacherPi/article/details/127489901