Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reverse a stack

Tags:

c++

stack

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;
}
like image 399
Gavon Black Avatar asked Aug 01 '26 14:08

Gavon Black


2 Answers

I'm not 100%, but I imagine your code will work if you delete the second while loop of 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;
}
like image 57
Bill Lynch Avatar answered Aug 03 '26 03:08

Bill Lynch


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.

like image 31
Johannes Overmann Avatar answered Aug 03 '26 03:08

Johannes Overmann