homework5_001

homework5_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 5 - due...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 FALL 2009 [CORTINA]HOMEWORK 5 - due Tuesday, October 13 by 11:59PM - EXTENDEDELECTRONIC HANDIN AVAILABLE FRI 10/9 AFTER 5 PM 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 Eand two Node<E> references named prevand next. The Node<E>class is defined as an inner class of DoublyLinkedList<E>. Write a method for this class named removethat 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}Complete the pushmethod for the HomerStackclass and determine the worst-case runtime complexity of pushassuming that the stack contains n elements. Complete the popmethod for the HomerStackclass and determine the worst-case runtime complexity of popassuming 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) 5.The postfix evaluator discussed in class computes the value of any valid postfix expression using a stack. What is the value of the following postfix expression? Show the contents of the stack (from top to bottom) after each operator is processed. 5 4 12 + 8 / * 3 + 6 2 * -6.The infix-to-postfix converter discussed in class translates an infix expression to the equivalent postfix expression using a stack. Convert the following infix expression to a postfix expression and show the contents of the stack (from top to bottom) after each operator is processed. 8 * ( 7 + 3 * 6 / ( 5 - 2 ) - 1 ) + 47.(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. 8.(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 position 0 in the array....
View Full Document

Page1 / 20

homework5_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 5 - due...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online