Section7Key 3

# Section7Key 3 - 6 One possible solution appears below public void rearrange(eueue<Integer> q Stack<Integer> s = new

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 6. One possible solution appears below. public void rearrange (eueue<Integer> q) { Stack<Integer> s = new ArrayStackclnteger>O; int q.sizeO; oldSize (int for i - 0; i < oldSize; i++) t q.dequeueO; int n: if(n%20) q.enqueue (n) i else s . push (n) ; ) int ewenCount = q.sizeO i whil-e (!s.isEmptyO) q.enqueue(s.popO ); (int for i : 0; i < evenCount; i++1 q.enqueue (g.dequeue O ); (int for i : O; i < oldSize evenCount; i++) s .push (q. dequeue ( ) ) ; whil-e (!s.isEmptyO) q.enqueue(s.popO); ) 1. One possible solution appears be1ow. public (eueue<Integer> void reverseHalf q) t Stack<Integer> s : new ArrayStack<Integer>O; int oldl,ength q.size(r, / / transfer elements in odd spots to stack (int for i : 0; i < otdlength; i++1 j_f (i * 2 0) q.enqueue (e.dequeue O ); else s.push (q.dequeue O ); / / reconstruct 1ist, taking alternately from (int for i:0; i < oJ-dlength; i++; (i \ 2 :: if 0) q.engueue (e.dequeue O ); el-se q.enqueue(s.popO); ) 8. One possible solution appears below. { ); queue and stack public boolean (eueue<Integer> isPalindrome q) Stack<Integer>_ s : new ArrayStack<Integer>( (int for i : 0; i. < q. size O i i++1 { q.dequeueO; int n: q. enqueue (n) i s.push (n) ; ) boo.l-ean ok true; (int for i : O; i < q.sizeO; i++; { q.dequeue ( ) ; int n1 int n2 s.pop ( ) ; (nl l: n2') if ok f al-se; q:enqueue (al); ) return ok; ...
View Full Document

## This note was uploaded on 11/03/2009 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Ask a homework question - tutors are online