Homework 7

  1. Ch 7 Programming Problem 3

    // Ch 7 Programming Problem 3

    // Recognize L : {w$'w}

    static boolean recognize(String string) {

        Stack<Character> stack = new Stack<Character>();

        Queue<Character> queue = new LinkedList<Character>();

       

        boolean in_reverse = false;

        for (int i = 0; i < string.length(); i++) {

            char ch = string.charAt(i);

            if (!in_reverse && ch == '$') {

                in_reverse = true;

            } else if (!in_reverse) {

                queue.offer(ch);

            } else {

                stack.push(ch);

            }

        }

        while (!stack.empty() && !queue.isEmpty()) {

            if (!stack.peek().equals(queue.peek())) {

                return false;

            }

            stack.pop();

            queue.poll();

        }

        if (stack.empty() && queue.isEmpty()) {

            return true;

        } else {

            return false;

        }

    }

  1. Ch 7 Programming Problem 4

    // Ch 7 Programming Problem 4

    // Calculate result of postfix expression

    static double calculate(String expression) {

        Stack<Double> stack = new Stack<Double>();

        StringTokenizer token = new StringTokenizer(expression);

       

        while (token.hasMoreTokens()) {

            String string = token.nextToken();

            if (!is_operator(string)) {

                stack.push(Double.parseDouble(string));

            } else {

                double d2 = stack.pop();

                double d1 = stack.pop();

                switch (string.charAt(0)) {

                    case '+':

                        stack.push(d1 + d2);

                        break;

                    case '-':

                        stack.push(d1 - d2);

                        break;

                    case '*':

                        stack.push(d1 * d2);

                        break;

                    case '/':

                        stack.push(d1 / d2);

                        break;

                    default:

                        throw new RuntimeException("Invalid operator" + string);

                }

            }

        }

        if (stack.size() == 1) {

            return stack.peek();

        } else {

            throw new RuntimeException("Invalid expression " + expression);

        }

    }

 

 

    static boolean is_operator(String string) {

        if (string.equals("+") || string.equals("-") ||

                string.equals("/") || string.equals(("*"))) {

            return true;

        }

        return false;

    }

  1. Ch 8 Exercise 2

See 1 above

  1. Ch 8 Exercise 3

4 4 4

  1. Ch 8 Exercise 4

a.    public int numberOfElements() {

        return size;

    }

b.    public int numberOfElements() {

        int size = 0;

        Node itor = front;

        while (itor != null) {

            size++;

            itor = itor.next;

        }

        return size;

    }

  1. Ch 8 Exercise 7

a.    public void display() {

        ArrayQueue temp = new ArrayQueue();

        while (!this.isEmpty()) {

            Object o = this.dequeue();

            temp.enqueue(o);

            System.out.println(o);

        }

        while (!temp.isEmpty()) {

            this.enqueue(temp.dequeue());

        }

    }

b.    public void display() {

        Node itor = front;

        while (itor != null) {

            System.out.println(itor.data);

            itor = itor.next;

        }

    }