Section7Key 1 - Solution 1 One possible solution to CSE143 Section be1ow#7 Problems appears(Stack<Integer> public void splitStack s new

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: Solution 1. One possible solution to CSE143 Section be1ow. #7 Problems appears (Stack<Integer> public void splitStack s) { () ; new LinkedQueue<Integer> Queue<Integer> queue all elements from stack to / / transfer int s. size (l ; oldlength (!s.isEmptyO) while q.enqueue(s.popO); negatives from queue to stack / / transfer (int for i : L; i <: oldlenqth; i++) t int n = q.dequeue0; if(n<0) s.push (n) ; e ] -s e q. enqueue (n) ; ) nonnegatives / / transfer (!q.isEmptyO) while s.push(q.dequeueO ); 2. One possibLe solution appears from queue to stack below. (Stackclnteger> public void stutter s) { q : new LinkedQueue<Integer> Queue<Integer> whil-e (!s.isEmptyO) q.enqueue(s.popO); while(!q.isEmptyO ) s.push(q.dequeueO); (!s.isEmptyO) while q.enqueue(s.popO); whil-e(!q.isEmptyO) { intn : d.leqUeUeO; O; "ii' "lp""ntn ) ; s.push ( ) ) 3. One possibl-e soLution appears below. s) { O; () ; public Stack<Integer> copyStack (Stackclnteger> new ArrayStack<Integer> Stack<Integer> s2 new LinkedQueue<Integer> Queue<Integer> ( ! s. isEmpty o while s2.push (s.pop( ) whil-e ( !s2. isEmpty ( ) q. enqueue (s2 . pop ( (!q.isEmptyO) while int n : q.dequeue ); . push (n) ; s s2.push (n) ; ) r6l-rlrn 9-, a?. 4. One possible solution appears below. s2) { public boolean equals (Stack<Integer> s1, Stackclnteger> new ArrayStack<Integer> Stack<Integer> s3 O; bool-ean same t rue; --Ll1^ / | ^1 i ^r--!-/\ I ^a I ^f,*-!-'\ \ | ...
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