Unformatted text preview: Stacks and Recursion
Faculty of Computers and Information Faculty Department of Computer Science Data Structures 1. In mathematical expressions, or any other expression, there can be many parentheses to group together some operations or subparts of the expression. There are three kinds of parentheses: (, {, and [, along with their matching ones: ), }, and ] respectively. It is required to write a function that takes a string, ending with the null character ’\0’, and returns 1 if the string has correct matching parentheses and 0 otherwise. For example, the string [· · · (· · ·){· · ·} · · ·] is valid. However, the string [· · · (· · ·}{· · ·}] is not valid because } is the wrong match of (. Also, the string [· · · (· · ·){· · ·}] · · ·) is not valid, because the last ) does not match any parenthesis. Also, the string (· · · [· · · (· · ·){· · ·}] is not valid because the ﬁrst ( is not matched. Notice that, the dots in these expressions mean any sequence of characters. Hint: Make an empty stack. Read characters until the end of the string. If the character is an open anything, push it onto the stack. If it is a close anything, then if the stack is empty report and error. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error. At end of the string, if the stack is not empty report an error. 2. Try to write the function recursively; can we? If you can, then draw the recursion tree and see whether you can convert it to iterative version. Is this iterative version the same as the previous function. 3. Write the function ClearStack in the implementation level of a linked stack recursively. Draw the recursion tree, and conclude the stack depth. Is there any need for implementing this function recursively than iteratively? ...
View
Full Document
 Spring '10
 WaleedA.Yousef
 Data Structures, Recursion, Control flow, null character, match

Click to edit the document details