0% found this document useful (0 votes)
48 views

Data Structures, Homework 5, Answers

1. This document provides answers to exercises from the textbook "Data Structures" by Carrano. It includes: - Strings that match a specific grammar with 7 or fewer characters - Pseudocode for a recursive function to check if a string matches a grammar - An implementation of an algorithm to recognize strings in a specific language - Evaluations of postfix expressions and conversions of infix expressions to postfix form 2. Key algorithms and concepts covered include grammars, recursive functions, stack-based evaluation of postfix expressions, and conversion of infix to postfix notation. - Examples show the step-by-step workings and outputs of these algorithms on given expressions.

Uploaded by

First Dream
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Data Structures, Homework 5, Answers

1. This document provides answers to exercises from the textbook "Data Structures" by Carrano. It includes: - Strings that match a specific grammar with 7 or fewer characters - Pseudocode for a recursive function to check if a string matches a grammar - An implementation of an algorithm to recognize strings in a specific language - Evaluations of postfix expressions and conversions of infix expressions to postfix form 2. Key algorithms and concepts covered include grammars, recursive functions, stack-based evaluation of postfix expressions, and conversion of infix to postfix notation. - Examples show the step-by-step workings and outputs of these algorithms on given expressions.

Uploaded by

First Dream
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures, Homework 5, Answers

1. Carrano, Chapter 5, Exercise 2: Consider the language that the following


grammar defines:

<S> = $ | <W> | $<S>


<W> = abb | a<W>bb

Write all strings that are in the language and that contain seven or fewer
characters.

$, $$, $$$, $$$$, $$$$$, $$$$$$, $$$$$$$,


abb, $abb, $$abb, $$$abb, $$$$abb,
aabbbb, $aabbbb

2. Carrano, Chapter 5, Exercise 7: Consider the following grammar:

<word> = R | <D> | <D><word><S>


<D> = Q | P
<S> = 1

Write pseudocode for a recursive function that returns true is a string is


in this language and returns false otherwise.
Solution:

isInLanguage(in st : string) : boolean{


if (st has only one character)
if ((st is R) or (st is Q) or (st is P))
return true
else
return false
else
if ((first character of st is Q or P)
and (last character of st is 1))
return isInLanguage(st minus its first and
and last characters)
else
return false
}
3. Carrano Chapter 6, Exercise 4: The section “Recognizing Strings in a
Language” describes a recognition algorithm for the language
L = {w$w0 : w is a possibly empty string of characters other than $, w0 = reverse(w)}

Implement this algorithm.


Solution:

bool isInLanguage (string aString){


Stack<char> aStack;

int i = 0;
while (aString[i] != ’$’){
aStack.push(ch);
++i;
} // end while

++i;

while (i < aString.length()){


char stackTop;
if (aStack.isEmpty())
return false;
else{
aStack.pop(stackTop);
if (stackTop == aString[i])
++i;
else
return false;
} // end else
} // end while

return aStack.isEmpty();
}

4. Carrano Chapter 6, Exercise 12: Evaluate the following postfix expression


by using the algorithm given in this chapter. Show the status of the stack
after each step of the algorithm. Assume the following values for the
identifiers: a = 7; b = 3; c = 12; d = −5; e = 1.
(a) abc+-
Solution: The state of the stack is given below each character after
it’s processed.
a b c + −
12
3 3 15
7 7 7 7 −8
So the answer is −8.
(b) abc-d*+
Solution:
a b c − d ∗ +
12 −5
3 3 −9 −9 45
7 7 7 7 7 7 52

So the answer is 52.


(c) ab+c-de*+
Solution:
a b + c − d e ∗ +
1
3 12 −5 −5 −5
7 7 10 10 −2 −2 −2 −2 −7

So the answer is −7.


5. Carrano, Chapter 6, Exercise 13: Convert the following infix expressions
to postfix form by using the algorithm given in this chapter. Show the
status of the stack after each step in the algorithm.

(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
− − + +

So the answer is ab-c+ (Recall all the remaining operators on the


stack are popped off and appended to postfixExp.)
(b) a/(b ∗ c)
Solution:
a / ( b ∗ c )
a a a ab ab abc abc∗
∗ ∗
( ( ( (
/ / / / / /

So the answer is abc*/.


(c) (a + b) ∗ c
Solution:
( a + b ) ∗ c
a a ab ab+ ab+ ab+c
+ +
( ( ( ( ∗ ∗

So the answer is ab+c*.


(d) a − (b + c)
Solution:
a − ( b + c )
a a a ab ab abc abc+
+ +
( ( ( (
− − − − − −

So the answer is abc+-.


(e) a − (b/c ∗ d)
Solution:
a − ( b / c ∗ d )
a a a ab ab abc abc/ abc/d abc/d*
/ / ∗ ∗
( ( ( ( ( (
− − − − − − − −

So the answer is abc/d*-.


(f) a/b/c − (d + e) ∗ f
Solution:
a / b / c − ( d
a a ab ab/ ab/c ab/c/ ab/c/ ab/c/d

( (
/ / / / − − −

+ e ) ∗ f
ab/c/d ab/c/de ab/c/de+ ab/c/de+ ab/c/de+f
+ +
( ( ∗ ∗
− − − − −

So the solution is ab/c/de+f*-.


(g) a ∗ (b/c/d) + e
Solution:
a ∗ ( b / c / d
a a a ab ab abc abc/ abc/d
/ / / /
( ( ( ( ( (
∗ ∗ ∗ ∗ ∗ ∗ ∗

) + 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/-.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy