Homework Assignment #2

Due on paper in lecture, Monday 17 October 2011

Correction in orange made Sunday 9 October.

Correction in brown made Wednesday 12 October.

Correction in purple made Friday 14 October.

• Question 1 (10): Answer Exercise 3.10 on page 67 of L&C. What they have in mind is that the top element is always in location 0 of the array, the next element is in location 1, and the bottom element has the highest location number. What is the big-O time complexity of the five basic stack operations if there are n elements in the stack? (Don't worry about any resizing of the array.) (Note: In this and all other questions, briefly justify your assertions about time complexity.)

• Question 2 (10) What is the big-O worst-case time complexity of the four methods from Discussion #4, in terms of n, the number of dogs in the team?

• Question 3 (20) Describe in English how you would implement a dropout stack with links, as in Programming Project 4.8 on page 97 of L&C. What is the big-O time complexity of the five basic stack operations, in the worst case, if there are n elements in the stack?

• Question 4 (10) Do Exercise 5.1 on page 126-7 of L&C. Indicate the contents of the queue at each step.

• Question 5 (10) Do Exercise 5.2 on page 127 of L&C. In each case, assume that the code of Exercise 5.1 has been run and that the code from the previous other parts of the problem has then also been run. Thus the four code fragments (a)-(d) run in succession. (I originally said that the code fragments were to be run independently but that makes no sense as (a) and (d) are identical.)

In addition, replace all occurrences of ``` front( )``` in Exercise 5.2 by `first( )`.

• Question 6 (10) Do Exercise 5.3 on page 127 of L&C -- here "each of the implementations" refers to the `LinkedQueue` and `CircularArrayQueue` of Chapter 5.

• Question 7 (20) Give code for Programming Project 5.9 on page 128 of L&C. You are writing a static method ```public static boolean isPalindrome (String w) ```. You should access the letters of w only in order from the beginning, once.

• Question 8 (10) Describe in English how you would implement the `isPalindrome` method with only one stack, if you had access to the number `w.length( )`. Again, access the letters of w only in order from the beginning, once.