3

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;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Gavon Black
  • 33
  • 1
  • 1
  • 4
  • 1
    It will be very difficult to help you without being able to see the code behind `linkedStackType`. – Bill Lynch Mar 26 '13 at 22:14
  • Not the solution, but the code copies the stack four times. It's easy to get this down to two copies. It can be done with only one copy. – Pete Becker Mar 26 '13 at 22:17
  • @sharth: Actually, at least one bug is in the code shown, so that's less of an issue than it'd normally be. – Mooing Duck Mar 26 '13 at 22:20

5 Answers5

11

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;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

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.

Johannes Overmann
  • 4,914
  • 22
  • 38
1

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

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
  • 1
    @SongWang: no, the stack is never doubled. It's simply printed twice before it's erased entirely. – Mooing Duck Mar 26 '13 at 22:25
  • Thanks for the explanation.. That makes sense. I am sitting here trying to figure out what to add and its as simple as taking away items. – Gavon Black Mar 26 '13 at 22:30
  • Thanks for pointing that out, Mooing Duck. Song Wang, yeah, that tripped me up at first too, but then I saw that `Stack Reversed` didn't have anything below it. – Kyle Strand Mar 26 '13 at 22:32
0

This is my solution to reverse the std::stack content:

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

You can use the same approach to access the cointeiner and print the values using iterators.

-1
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);  
}  
Ankur Lathiya
  • 176
  • 1
  • 9