Exercises

  1. 19.3 (Merging Ordered-List Objects) Write a program that merges two ordered-list objects of integers into a single ordered-list object of integers. Method Merge of class ListMerge should receive references to each of the list objects to be merged and should return a reference to the merged-list object.

  2. 19.4 (Reversing a Line of Text with a Stack) Write a program that inputs a line of text and uses a stack object to display the line reversed.

  3. 19.5 (Palindromes) Write a program that uses a stack to determine whether a string is a palindrome (i.e., the string is spelled identically backward and forward). The program should ignore capitalization, spaces and punctuation.

  4. 19.6 (Evaluating Expressions with a Stack) Stacks are used by compilers to evaluate expressions and generate machine-language code. In this and the next exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses.

    Humans generally write expressions like 3 + 4 and 7 / 9, in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation, in which the operator is written to the right of its two operands. The preceding infix expressions would appear in postfix notation as 3 4 + and 7 9 /, respectively.

    To evaluate a complex infix expression, a compiler would first convert the expression to postfix notation, then evaluate the postfix version of the expression. Each of these algorithms requires only a single left-to-right pass of the expression. Each algorithm uses a stack object in support of its operation, and in each algorithm the stack is used for a different purpose. Here, you’ll implement the infix-to-postfix conversion algorithm. In the next exercise, you’ll implement the postfix-expression evaluation algorithm.

    Write class InfixToPostfixConverter to convert an ordinary infix arithmetic expression (assume a valid expression is entered), with single-digit integers, such as

    
    (6 + 2) * 5 - 8 / 4
    

    to a postfix expression. The postfix version of the preceding infix expression is

    
    6 2 + 5 * 8 4 / -
    

    The program should read the expression into StringBuilder infix, then use class StackInheritance (implemented in Fig. 19.13) to help create the postfix expression in StringBuilder postfix. The algorithm for creating a postfix expression is as follows:

    1. Push a left parenthesis '(' on the stack.

    2. Append a right parenthesis ')' to the end of infix.

    3. While the stack is not empty, read infix from left to right and do the following:

      • If the current character in infix is a digit, append it to postfix.

      • If the current character in infix is a left parenthesis, push it onto the stack.

      • If the current character in infix is an operator:

        • Pop operators (if there are any) at the top of the stack while they have equal or higher precedence than the current operator, and append the popped operators to postfix.

        • Push the current character in infix onto the stack.

      • If the current character in infix is a right parenthesis:

        • Pop operators from the top of the stack and append them to postfix until a left parenthesis is at the top of the stack.

        • Pop (and discard) the left parenthesis from the stack.

    The following arithmetic operations are allowed in an expression:

    • + addition

    • - subtraction

    • * multiplication

    • / division

    • ^ exponentiation

    • % modulus

    Some of the methods you may want to provide in your program follow:

    1. Method ConvertToPostfix, which converts the infix expression to postfix notation.

    2. Method IsOperator, which determines whether c is an operator.

    3. Method Precedence, which determines whether the precedence of operator1 (from the infix expression) is less than, equal to or greater than the precedence of operator2 (from the stack). The method returns true if operator1 has lower precedence than or equal precedence to operator2. Otherwise, false is returned.

  5. 19.7 (Evaluating a Postfix Expression with a Stack) Write class PostfixEvaluator, which evaluates a postfix expression (assume it is valid) such as

    
    6 2 + 5 * 8 4 / -
    

    The program should read a postfix expression consisting of digits and operators into a String-Builder. Using the stack class from Exercise 19.6, the program should scan the expression and evaluate it. The algorithm (for single-digit numbers) is as follows:

    1. Append a right parenthesis ')' to the end of the postfix expression. When the right-parenthesis character is encountered, no further processing is necessary.

    2. When the right-parenthesis character has not been encountered, read the expression from left to right.

      • If the current character is a digit, do the following:

        • Push its integer value on the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in Unicode).

      • Otherwise, if the current character is an operator:

        • Pop the two top elements of the stack into variables x and y.

        • Calculate y operator x.

        • Push the result of the calculation onto the stack.

    3. When the right parenthesis is encountered in the expression, pop the top value of the stack. This is the result of the postfix expression.

    [Note: In Part b above (based on the sample expression at the beginning of this exercise), if the operator is '/', the top of the stack is 4 and the next element in the stack is 8, then pop 4 into x, pop 8 into y, evaluate y / x and push the result, 2, back on the stack. This note also applies to operator '-'.] The arithmetic operations allowed in an expression are:

    • + addition

    • - subtraction

    • * multiplication

    • / division

    • ^ exponentiation

    • % modulus

    You may want to provide the following methods:

    1. Method EvaluatePostfixExpression, which evaluates the postfix expression.

    2. Method Calculate, which evaluates the expression op1 operator op2.

  6. 19.8 (Level-Order Binary Tree-Traversal) The program of Fig. 19.21 illustrated three recursive methods of traversing a binary tree—inorder, preorder, and postorder traversals. This exercise presents the level-order traversal of a binary tree, in which the node values are displayed level by level, starting at the root-node level. The nodes on each level are displayed from left to right. The level-order traversal is not a recursive algorithm. It uses a queue object to control the output of the nodes. The algorithm is as follows:

    1. Insert the root node in the queue.

    2. While there are nodes left in the queue, do the following:

      • Get the next node in the queue.

      • Display the node’s value.

      • If the reference to the left child of the node is not null:

        Insert the left child node in the queue.

      • If the reference to the right child of the node is not null:

        Insert the right child node in the queue.

    Write method LevelOrderTraversal to perform a level-order traversal of a binary-tree object. Modify the program of Fig. 19.21 to use this method. [Note: You also will need to use the queue-processing methods of Fig. 19.16 in this program.]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset