Reverse a stack in place
*/
//#include "stdafx.h"
#include "iostream"
#include "stack"
using namespace std;
void Push(stack
{
int m = (int)myStack.top();
myStack.pop();
if (myStack.size() != 0)
Push(myStack , a);
else {
myStack.push(a);}
myStack.push(m);
}
void reverse(stack
{
int m = (int)myStack.top();
myStack.pop();
if (myStack.size() != 1)
reverse(myStack);
Push(myStack , m);
}
int main()
{
stack
//add the stack elements
for(int i = 1; i < 6; i++)
S.push(i);
//display the stack elements
cout << "Original Stack" << endl;
while (!S.empty()) {
cout << S.top() << '\t';
S.pop(); // remove top element
}
cout << endl;
//add the stack elements
for(int i = 1; i < 6; i++)
S.push(i);
reverse(S); //reverse the stack
//display the reversed stack
cout << "Reversed Stack" << endl;
while (!S.empty()) {
cout << S.top() << '\t';
S.pop(); // remove top element
}
cout << endl;
system("PAUSE");
return 0;
}
No comments:
Post a Comment