Stacks

Ok...I wrote this code up, which works fine for converting an integer to base 2. The only problem I'm having is how do I reverse the stack? Will anyone help? Here is the code.

#include <iostream>
#include "StackLibrary.h"

using namespace std;

int main()
{
STACK <int, 10> s;
STACK <int, 10> p;
s.MakeStack();
int n;
cout<< "Enter a positve integer below 1000: ";
cout<<endl;
cin>>n;
for( int i=1; n>=1; ++i)
{
s.PushStack(n%2);
for( int j=1; j<=1; ++j)
{
n=(n/2);
}
}

while(!s.EmptyStack())
{
int q= s.PopStack();
p.PushStack(q);
}

while(!p.EmptyStack())
{
int a= p.PopStack();
cout<<a;
}

system ("pause");
return 0;
}
If stacks use iterators to access their elements, then you can try the <algorithm> header file--to be specific, the reverse function.
Did you write your 'STACK' struct as a FILO linked list? Can the STL Algorithms "std::reverse" or "std::reverse_copy" help?

- std::reverse: http://www.cplusplus.com/reference/algorithm/reverse/
- std::reverse_copy: http://www.cplusplus.com/reference/algorithm/reverse_copy/

Thanks Aceix and Computergeek...I'll give this a try! :)
Topic archived. No new replies allowed.