CS210 Lab: Stacks Postlab


Postlab Exercise:

Given the input string "xyz", which of the permutations of this string listed in Section A can be output by a code fragment consisting of only the statement pairs:

cin >> ch; mystack.push(ch);
and
ch = mystack.top; mystack.pop; cout << ch;

where ch is a character and mystack is a stack of characters? Note that the above statements can be repeated in any order.

For instance, the string "zyx" can be output from the original input ("xyz") with the following code fragments.

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;

Section A

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. "xzy"
  3. "yxz"
  4. "yzx"
  5. "zxy"
  6. "zyx"

For Answers, 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.