C++ 如何反转堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15648313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:35:24  来源:igfitidea点击:

how to reverse a stack

c++stack

提问by Gavon Black

I have an assignment where I am suppose to take a single stack, show the output and then reverse it to show the output.

我有一个作业,我假设采用单个堆栈,显示输出,然后将其反转以显示输出。

Its suppose to look like this

它应该看起来像这样

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

Instead mine is coming out like this

相反,我的是这样出来的

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

Can someone please look at my code and see what is going on. I have tried quite a few things but cannot get anything to work.

有人可以看看我的代码,看看发生了什么。我已经尝试了很多东西,但无法得到任何工作。

#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;
}

回答by Bill Lynch

I'm not 100%, but I imagine your code will work if you delete the second while loop of reverseStack.

我不是 100%,但我想如果您删除reverseStack.

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;
}

回答by Johannes Overmann

You have an extraneous print loop in reverseStack() which prints the values in the wrong place. In addition this print loop clears your tmpStack. This explains the result.

您在 reverseStack() 中有一个无关的打印循环,它在错误的位置打印值。此外,此打印循环会清除您的 tmpStack。这解释了结果。

回答by Kyle Strand

Your second whileloop empties tmpStack, but then you assign the now-empty stack to stack, so you just have an empty stack.

您的第二个while循环清空tmpStack,但随后您将现在为空的堆栈分配给stack,因此您只有一个空堆栈。

回答by Ankur Lathiya

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);  
}