|
|

This is only a preview of the paper Click here to register and get the full text. Existing members click here to login
|
|
|
1. Complete the implementation of the following method, observing the following restrictions: Ć you don't declare any additional local variables or allocate new objects. Ć you don't store null in the Queue or Stack. Ć neither StackInterface or QueueInterface has a size method (but they do have isEmpty methods) Answer: public static void purge(QueueInterface q) { // pre: q is not null, q contains only Integer objects // post: all negative Integers are removed from q, // order of other elements in q remains unchanged StackInterface s = DataFactory.makeStack(); //Check if the values of the Integer objects in q are negative or positive. If positive, put //them into stack s and remove from q. Otherwise, remove from q only. while (q.isEmpty() = = false) { if (q.peek.intValue( ) < 0) { q.dequeue( ); } else { s.push (q.dequeue( )); } } //Put back all positive integer objects into q, but in a reverse order. while (s.isEmpty( ) = = false) { q.enqueue(s.pop); } //Put the integer objects into q in the original order. while (q.isEmpty( ) = = false) { s.push (q.dequeue( )); } while (s.isEmpty( ) = = false) { q.enqueue(s.pop); } } 2. Consider the following interface. public interface ExtendedStack extends StackInterface { public Object pull(); // pre: this is not empty // post: the item on the bottom of the stack (the item first in) // is removed and returned } Here are three suggested classes that could implement this ADT: public class SinglyLinkedExtendedStack implements ExtendedStack {// Implementation using a singly-linked list private Node head; private int count; ...
Approximate Word count = 1039 Approximate Pages = 4.2 (250 words per page double spaced)
|
|
|
|
|
|