Saturday, January 28, 2012


#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main()
{
 stack<int> stack1, stack2, temp_stack;

 //loop to fill our stack with elememnts
 for(int i = 0 ; i < 5; i++)
 {
  //push elements on to stack1
  stack1.push(i);
  //print elements to screen along with the slot the elements are in
  cout << "slot " << i + 1 << " " << stack1.top() << endl;
 }

 //loop until stack1 is empty
 while(!stack1.empty())
 {
  //push the top element of stack1 onto temp_stack 
  temp_stack.push(stack1.top());
  //pop the elements off stack1 to get to the next element
  stack1.pop();
 }
 //loop until temp_stack is empty
 while(!temp_stack.empty())
 {
  // push temp_stack elements on the stack1 and stack2 before you pop
  stack1.push(temp_stack.top());
  stack2.push(temp_stack.top());

  //pop temp_stack
  temp_stack.pop();

  //print stack1 and stack2
  cout << "stack 1 " << stack1.top() << endl;
  cout << "stack 2 " << stack2.top() << endl;
 }
 }

 

No comments:

Post a Comment