CS210 Lab: Stacks Postlab Answers


Postlab Answers:

For each of the permutations below, give a code fragment (as in the above example) to output the permutation
or provide a brief explanation of why the permutation cannot be produced.
  1. "xyz"
  2. 		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    	
  3. "xzy"
  4. 		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		cin >> ch;  mystack.push(ch);
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		ch = mystack.top;   mystack.pop;   cout << ch;
    	
  5. "yxz"
  6. 		cin >> ch;  mystack.push(ch);
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    	
  7. "yzx"
  8. 		cin >> ch;  mystack.push(ch);
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		ch = mystack.top;   mystack.pop;   cout << ch;
    	
  9. "zxy"
  10. 		cannot print z first and have x on top of y
    	
  11. "zyx"
  12. 		cin >> ch;  mystack.push(ch);
    		cin >> ch;  mystack.push(ch);
    		cin >> ch;  mystack.push(ch);
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		ch = mystack.top;   mystack.pop;   cout << ch;
    		ch = mystack.top;   mystack.pop;   cout << ch;
    	

Back to Exercise click here
Back to Stack Lab click here

CS Dept Home Page
CS Dept Class Files
CS210 Class Files

Copyright: Department of Computer Science, University of Regina.