Professional Documents
Culture Documents
Data Structures, Homework 5, Answers
Data Structures, Homework 5, Answers
Write all strings that are in the language and that contain seven or fewer
characters.
int i = 0;
while (aString[i] != ’$’){
aStack.push(ch);
++i;
} // end while
++i;
return aStack.isEmpty();
}
(a) a − b + c
Solution: Below each character is the value of the string postfixExp
and then below that, the state of the stack is given (after the character
is processed).
a − b + c
a a ab ab- ab-c
− − + +
( (
/ / / / − − −
+ e ) ∗ f
ab/c/d ab/c/de ab/c/de+ ab/c/de+ ab/c/de+f
+ +
( ( ∗ ∗
− − − − −
) + e
abc/d/ abc/d/* abc/d/*e
∗ + +
So the answer is abc/d/*e+.
(h) a − (b + c ∗ d)/e
Solution:
a − ( b + c ∗ d
a a a ab ab abc abc abcd
∗ ∗
+ + + +
( ( ( ( ( (
− − − − − − −
) / e
abcd*+ abcd*+ abcd*+e
/ /
− − −
So the answer is abcd*+e/-.