簡體   English   中英

如何反轉堆棧

[英]how to reverse a stack

我有一個任務,我想取一個堆棧,顯示輸出,然后將其反轉以顯示輸出。

它假設看起來像這樣

Stack:
262 115 74 26 34 243 22 734 113 121
Stack Reversed:
121 113 734 22 243 34 26 74 115 262

相反,我的是這樣出來的

Stack:
262 115 74 26 34 243 22 734 113 121 121 113 734 22 243 34 26 74 115 262
Stack Reversed:

有人可以看看我的代碼,看看發生了什么。 我已經嘗試了很多東西,但無法讓任何東西發揮作用。

#include <stdio.h>
#include <iostream>

#include "linkedStack.h"

using namespace std;

template <class Type>
void printStack(linkedStackType<Type>& stack);

template <class Type>
void reverseStack(linkedStackType<Type>& stack);

int main(int argc, char **argv)
{
   // Declare stack variables
   linkedStackType<int> stack;

   // Add some data to the stack
   stack.push(121);
   stack.push(113);
   stack.push(734);
   stack.push(22);
   stack.push(243);
   stack.push(34);
   stack.push(26);
   stack.push(74);
   stack.push(115);
   stack.push(262);

   cout << "\nStack:\n   ";
   printStack(stack);

   reverseStack(stack);

   cout << "\nStack Reversed:\n   ";
   printStack(stack);

   cout << "\n\n** Press any key to continue **\n";
   getchar();

   return 0;
}

template <class Type>
void printStack(linkedStackType<Type>& stack)
{
   Type item;
   linkedStackType<Type> tmpStack = stack;

   while (stack.isEmptyStack() == false)
   {
      item = stack.top();
      stack.pop();
      cout << item << " ";
   }

   stack = tmpStack;



 }

template <class Type>
void reverseStack(linkedStackType<Type>& stack)
{
  Type item;
   linkedStackType<Type> tmpStack;

   while (stack.isEmptyStack() == false)
   {
      item = stack.top();
      stack.pop();
      tmpStack.push(item);
   }

   while (tmpStack.isEmptyStack() == false)
   {
      item = tmpStack.top();
      tmpStack.pop();
      stack.push(item);
      cout << item;  

   }

   stack = tmpStack;


   return;
}

我不是 100%,但我想如果你刪除reverseStack的第二個 while 循環,你的代碼會起作用。

template <class Type>
void reverseStack(linkedStackType<Type>& stack)
{
   Type item;
   linkedStackType<Type> tmpStack;

   while (stack.isEmptyStack() == false)
   {
      item = stack.top();
      stack.pop();
      tmpStack.push(item);
   }

   //while (tmpStack.isEmptyStack() == false)
   //{
   //   item = tmpStack.top();
   //   tmpStack.pop();
   //   stack.push(item);
   //   cout << item;
   //}

   stack = tmpStack;
   return;
}

您在 reverseStack() 中有一個無關的打印循環,它將值打印在錯誤的位置。 此外,此打印循環會清除您的 tmpStack。 這解釋了結果。

您的第二個while循環清空tmpStack ,但隨后您將現在為空的堆棧分配給stack ,因此您只有一個空堆棧。

這是我反轉 std::stack 內容的解決方案:

#include <stack>
#include <algorithm>    

template <class T>
class _stack : public std::stack<T>
{
    public:
        void reverse()
        {
            std::reverse(this->c.begin(),this->c.end());
        }
};
    
template <class T>
bool reverseStack(std::stack<T> & stack)
{
    bool ret = false;
    auto * s = reinterpret_cast<_stack<T>*> (&stack);
    if(s)
    {
        s->reverse();
        ret = true;
     }
     return ret;
}
    
int main()
{
   std::stack<int> stack;
   stack.push(1);
   stack.push(2);
   stack.push(3);
   stack.push(4);
   stack.push(5);
    
   reverseStack(stack);
}

您可以使用相同的方法訪問 cointeiner 並使用迭代器打印值。

void sortStack(struct stack **s)  
{  
if (!isEmpty(*s))  
  {  
    int x = pop(s);  
    sortStack(s);  
    sortedInsert(s, x);  
  }  
}  

void sortedInsert(struct stack **s, int x)  
{  
  if (isEmpty(*s) || x > top(*s))  
  {  
    push(s, x);  
    return;  
  }  
int temp = pop(s);  
sortedInsert(s, x);  
push(s, temp);  
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM