Section7Key 4 - 9 One possible so}ution appears beJ_ow...

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

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

Unformatted text preview: 9. One possible so}ution appears beJ_ow. \ public void switchpairs (Stack<Integer> s) { q - new Linkedeueue<fnteger>O Queue<Integer> ( !s. isEmpty o whil-e q.enqueue(s.pop )); ( !q.isEmpty o whiLe s . push (q. dequeue O ) ; ( !s. isEmpty ( ) whil-e q.enqueue(s.pop )); (q. size ( ) whj-le { int n1 q. dequeue ( ) , int n2 = q.dequeueO, s .push (n21 ; s.push (nl1 ; ) (!q.isEmpryO) if s.push(q.dequeueO); ) 10. One possible pubJ-ic if solution appears below. ; boolean isConsecutive (Stack<fnteger> s) t (s.sizeO (: 1) return true; else { Queue<Integer> nerr, Linkedeueue<Integer> int prev : s.popO; q. enqueue (prev) ; boolean ok : true; (!s.isEmpryO) while { int next s.pop O ; (prev if next l- 1) ok false; q.enqueue (next) ; pf€v : next; ) (!q.isEmpryO) while s.push(q.dequeueO); O; while q.enqueue(s.popO); while (!q.isEmpryO) s.push(q.dequeueO); return ok; ( ! s. isEmpry o 11. One possible solution appears bel-ow. public voj_d reorder (eueue<f nteger> q) { Stack<Integer> s : new ArrayStack<fnteger>O; j_nt oldSize q. si ze 0 , (int for i : O; i < oldSize; i++1 t rnt n q. dequeue O ; (n if s.push (n) ; el_se q.enqueue (n); int newSize : q.size(r; whil-e (!s.isEmptyO) q. enqueue (s.pop O ) ; (int for i : O; i < newSize; q.enqueue (q.dequeue O ); i++1 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online