【问题标题】:Recursively reverse a stack递归反转堆栈
【发布时间】:2015-06-06 21:00:36
【问题描述】:

我需要在 C++ 中使用递归来反转堆栈。我只能使用poppushreverseStack没有其他功能,例如我在搜索stackoverflow和网络时发现的insertAtBottom

我试过了:

void Stack::reverseStack(){
    if (isEmpty())
        return;
    else{
        int x;
        pop(x);
        reverseStack();
        push(x);
    }
}

但这会创建一个与原始堆栈完全相同的堆栈。

【问题讨论】:

  • 是否允许将项目推入第二个堆栈,然后在最后交换堆栈?

标签: c++ recursion stack reverse


【解决方案1】:

你需要实现一个函数来在底部插入一个项目,例如

void Stack::insertAtBottom(int item) {
    if(isEmpty())
        push(item);
    else {
        int x;
        pop(x);
        insertAtBottom(item);
        push(x);
    }
}

此时您可以按如下方式实现反向

void Stack::reverseStack(){
    if (isEmpty())
        return;
    else{
        int x;
        pop(x);
        reverseStack();
        insertAtBottom(x);
    }
}

编辑: 如果它们需要在一个函数中,下面是两者的组合

void Stack::reverseStack(bool reverse=true,int item=0){
    if(reverse) {
        if (isEmpty())
            return;
        else{
            int x;
            pop(x);
            reverseStack();
            reverseStack(false,x);
        }
    } else {
        if(isEmpty())
            push(item);
        else {
            int x;
            pop(x);
            reverseStack(false,item);
            push(x);
        }
    }
}

干杯!

【讨论】:

    【解决方案2】:
    #include <iostream>
    #include <stack>
    
    using namespace std;
    
    stack<int> S, R; // S is original stack, R is reversed stack
    
    void reverseStack() {
        if(!S.empty()) {
            R.push(S.top());
            S.pop();
            reverseStack();
        }
        return;
    }
    
    int main() {
        S.push(1);
        S.push(2);
        S.push(3);
        S.push(4);
        S.push(5);
    
        reverseStack();
    
        // Check if R is reversed
        while(!R.empty()) {
            cout << R.top() << " ";
            R.pop();
        }
        return 0;
    }
    

    希望这会有所帮助!

    【讨论】:

    • R.push(S.pop()) 就足够了。它跳过了对堆栈中每个元素的 top() 方法调用。
    【解决方案3】:

    这里有一个C语言解决这个问题的方法:

    #include <stdio.h>
    // functions that can be used push(), pop(), and isEmpty()
    // the idea is to hold all the values in Function Call Stack until the stack becomes empty.
    //When the stack becomes empty, we insert all the held items one by one at the bottom of the stack   
    // stack-> 1 2 3 4 becomes 4 3 2 1 when we print
    #define size 4
    int stk[size];
    int top = -1;
    void insertAtLast(int element);
    int isStackFull()
    {
    if(top == size - 1)
        return 1;
    return 0;
    }
    void push(int val)
    {
    if(isStackFull()==1)
        return;
    else
        //Task 2: Complete the logic
        stk[++top] = val;
    }
    int isStackEmpty()
    {
    //Task 1: Write logic for isStackEmpty()
    if (top==-1)
        return 1;
    return 0;
    }
    int pop()
    {
    if(isStackEmpty()==1)
        return -1;
    else
        //Task 2: Complete the logic
        return stk[top--];
        
    }
    void reverse(){
    if(isStackEmpty()==1){
        return;
    }
    int temp=pop();
    reverse();
    
    insertAtLast(temp);
    }
    void insertAtLast(int element){
     if(isStackEmpty()==1){
        push(element);   //imp
        return;
    }
    
    int topElements=pop();  //imp
    insertAtLast(element);  //imp
    
    push(topElements);
    }
    int main() {
    push(4);
    push(3);
    push(2);
    push(1);
    for(int i=0;i<size;i++){
        printf("%d ", stk[i]);
    }
    printf("\n");
    reverse();
    
    for(int i=0;i<size;i++){
        printf("%d ", stk[i]);
    }
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2014-07-14
      • 2021-04-08
      • 1970-01-01
      • 2011-11-24
      • 2011-01-30
      • 1970-01-01
      • 2015-09-12
      • 2021-01-21
      相关资源
      最近更新 更多