{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

homework5

# homework5 - 15-121 FALL 2009[CORTINA/REID-MILLER HOMEWORK 5...

This preview shows pages 1–5. Sign up to view the full content.

15-121 FALL 2009 [CORTINA/REID-MILLER] HOMEWORK 5 - due Sunday, October 11 by 11:59PM PROBLEMS (10 points) 1.(1.5 pts) Let the DoublyLinkedList<E> class represent a doubly linked list with a reference to the head node and a reference to the tail node, both of type Node<E> . Each node has a data field of type E and two Node<E> references named prev and next . The Node<E> class is defined as an inner class of DoublyLinkedList<E> . Write a method for this class named remove that takes a reference to a node of the list and removes the node from the list and returns a reference to its data. 2.(1 pt) Homer decides to implement his own stack, called HomerStack<E>. The contents of the stack are stored in a field called data of type ArrayList<E>, where INDEX 0 represents the TOP of the stack. public class HomerStack<E> { private ArrayList<E> data; //top element at index 0 //constructors and other methods not shown }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Complete the push method for the HomerStack class and determine the worst- case runtime complexity of push assuming that the stack contains n elements. Complete the pop method for the HomerStack class and determine the worst-case runtime complexity of pop assuming that the stack contains n elements. If the stack is empty, return null in this case.
3.(1.5 pts) Complete the method below, which should return a new stack containing the playing cards in the given stack in the same order, with all spades removed. public static LIFOStack<PlayingCard> removeSpades(LIFOStack<PlayingCard> s) { } 4.(1 pt) New question: The parentheses checker discussed in classreturns true if the string contains a proper nested parentheses, and false otherwise. Show the contents of the stack (from top to bottom) after reading each symbol in the string and indicate when and what value it returns for the following strings. Explain what condition in the algorithm was used to detect an imbalance in each case that an imbalance occurs. (See the Stack.zip project on the class daily schedule.) 5. [ { } ( [ { } ( < > ) ] ) ] 6. [ ( ( ) { } ] 7. ( < [ ( ) ] > ( ) 8. { < > } ) ( )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9.(2 pts) Consider a stack where you can push or pop at either end. Create a class FancyStack<E> that uses an array to store the data for the stack. Implement methods to push and pop at either end that work in O(1) expected running time. You may define any additional fields and helper methods that you feel are necessary. 10.(1 pt) Suppose a queue is implemented using an array of some fixed size. When an element is added to the queue, it is inserted into the next available array cell. When an element is removed from the queue, it is removed from the first used array cell (assuming the queue is not empty). No shifting is done until an element is inserted and the rear of the queue is in the last array cell. At this point, the elements belonging to the queue are shifted toward the beginning of the array so that the front of the queue is at position 0 in the array. If the array is full, the element is not inserted into the queue.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 20

homework5 - 15-121 FALL 2009[CORTINA/REID-MILLER HOMEWORK 5...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online