CSC 207 | Algorithms and Object Oriented Design | Spring 2009 |
Summary: In this lab, you will practice using stacks to solve some data organization and manipulation problems.
mkdir somewhere/stacksqueues
cp ~weinman/public_html/courses/CSC207/2010S/labs/code/stacksqueues/Stacks.java .
removeAll
in Stacks.java
, using your algorithm to remove all
instances of item
from stack
(you should
use the equals
method). You may create only Stack(s)
or an AnyType
for temporary storage.
main
routine to test your method.
Stacks.java
public static <AnyType> void removeDuplicates( Stack<AnyType> stack)
removeDuplicates
should use only an extra stack (for
storage) and calls to your removeAll
method to eliminate
the duplicates from stack
but otherwise preserve the
elements in the same order.
For example, the code
Stack<Integer> stack = new Stack<Integer>();
stack.push(5);
stack.push(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(5);
stack.push(3);
System.out.println (stack);
removeDuplicates (stack);
System.out.println (stack);
3532135 3521
main
to test your method.
Queues
write a function
public static <AnyType> Queue<AnyType> split(Queue<AnyType> queue)
queue
should
have roughly half-as many entries in it (order preserved), and the
returned object should be a new queue with the other half.
Note that java.util.LinkedList
implements java.util.Queue
, which is the data structure you should use in your algorithm. You will need to import both, but the static type of all your collections variables should be Queue
.
/** Merges two queues based on their arrival time. item1 (from either
* queue) is considered to have arrived before item2 (from the other
* queue) if item1.compareTo(item2)<0.
*
* Postconditions: queue1.size() == 0 and queue2.size() == 0.
*
* @returns A new Queue containing all elements from both queues
*/
public static <AnyType extends Comparable<? super AnyType>> Queue<AnyType> merge
( Queue<AnyType> queue1,
Queue<AnyType> queue2 )
java.util.LinkedList
implements java.util.Queue
. The static type of all your collections variables should be Queue
.