📌  相关文章
📜  在不改变顺序的情况下交换 Stack 和 Queue 的元素

📅  最后修改于: 2022-05-13 01:56:14.062000             🧑  作者: Mango

在不改变顺序的情况下交换 Stack 和 Queue 的元素

给定一个包含M个元素的堆栈St和一个包含N个元素的队列Q。任务是将堆栈的每个元素放入队列,并将队列的每个元素放入堆栈,而不改变它们的顺序。

例子

Naive Approach:基本的做法是将堆栈和队列的元素存储在一个单独的数组中,然后再次将它们放入队列和堆栈中。

时间复杂度: O(N + M)
辅助空间: O(N + M)

优化方法:利用栈和队列的后进先出和先进先出特性,可以在不占用任何额外空间的情况下解决这个问题。

请按照以下步骤解决问题:

  1. 将队列中的每个元素放入堆栈。
  2. 将栈中多余的元素再次放入队列,栈中多余的元素就是队列中来的元素。现在,队列颠倒了。
  3. 现在,将堆栈的每个元素放入队列中。
  4. 最后,将队列的原始元素放入堆栈。
  5. 现在再次重复第一步和第二步,以保持队列中堆栈元素的顺序。

请按照以下插图更好地理解该方法:

插图:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to Put every element of stack
// into queue and queue into stack
// without changing its order
void changeElement(stack& St,
                   queue& Q)
{
 
    // Calculate size of queue Q
    int Size = Q.size();
    int Temp = Size;
    int N = St.size();
 
    // Put every element of queue into stack
    while (!Q.empty()) {
        St.push(Q.front());
        Q.pop();
    }
 
    // Put extra element of stack into
    // queue again, extra element of stack
    // is the element coming from queue.
    // Now, the queue is reversed
    while (Size != 0) {
        Q.push(St.top());
        St.pop();
        Size--;
    }
 
    // Put every element of stack into queue
    while (!St.empty()) {
        Q.push(St.top());
        St.pop();
    }
 
    Size = Temp;
 
    // Put initial element of queue
    // into stack
    while (Size != 0) {
        St.push(Q.front());
        Q.pop();
        Size--;
    }
 
    // Repeat the first and second steps
    while (!Q.empty()) {
        St.push(Q.front());
        Q.pop();
    }
    while (N != 0) {
        Q.push(St.top());
        St.pop();
        N--;
    }
}
 
// Function to traverse till stack is
// not empty and print the element in it
void printStack(stack& St)
{
 
    while (!St.empty()) {
        cout << St.top() << " ";
        St.pop();
    }
    cout << endl;
}
 
// Function to traverse till queue is not
// empty and print the element in it
void printQueue(queue& Q)
{
 
    while (!Q.empty()) {
        cout << Q.front() << " ";
        Q.pop();
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    stack St;
    queue Q;
 
    // Fill element into stack
    St.push(4);
    St.push(3);
    St.push(2);
    St.push(1);
 
    // Fill element into queue
    Q.push(8);
    Q.push(7);
    Q.push(6);
    Q.push(5);
 
    changeElement(St, Q);
    cout << "Stack = ";
    printStack(St);
    cout << "Queue = ";
    printQueue(Q);
    return 0;
}


Python3
# python3 program for the above approach
import collections
 
# Function to Put every element of stack
# into queue and queue into stack
# without changing its order
def changeElement(St, Q):
 
    # Calculate size of queue Q
    Size = len(Q)
    Temp = Size
    N = len(St)
 
    # Put every element of queue into stack
    while (len(Q) != 0):
        St.append(Q.popleft())
 
    # Put extra element of stack into
    # queue again, extra element of stack
    # is the element coming from queue.
    # Now, the queue is reversed
    while (Size != 0):
        Q.append(St.pop())
        Size -= 1
 
    # Put every element of stack into queue
    while (len(St) != 0):
        Q.append(St.pop())
 
    Size = Temp
 
    # Put initial element of queue
    # into stack
    while (Size != 0):
        St.append(Q.popleft())
 
        Size -= 1
 
    # Repeat the first and second steps
    while (len(Q) != 0):
        St.append(Q.popleft())
 
    while (N != 0):
        Q.append(St.pop())
 
        N -= 1
 
# Function to traverse till stack is
# not empty and print the element in it
def printStack(St):
 
    while (len(St) != 0):
        print(St.pop(), end=" ")
 
    print()
 
# Function to traverse till queue is not
# empty and print the element in it
def printQueue(Q):
 
    while (len(Q) != 0):
        print(Q.popleft(), end=" ")
 
    print()
 
# Driver Code
if __name__ == "__main__":
 
    St = collections.deque()
    Q = collections.deque()
 
    # Fill element into stack
    St.append(4)
    St.append(3)
    St.append(2)
    St.append(1)
 
    # Fill element into queue
    Q.append(8)
    Q.append(7)
    Q.append(6)
    Q.append(5)
 
    changeElement(St, Q)
    print("Stack = ", end="")
    printStack(St)
    print("Queue = ", end="")
    printQueue(Q)
 
    # This code is contributed by rakeshsahni


输出
Stack = 8 7 6 5 
Queue = 1 2 3 4 

时间复杂度:O(M+N)
辅助空间:O(1)