Section7Problems 2 - 4. tists\ takes as paramet,ers two...

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: 4. tists\ takes as paramet,ers two stacks containing Write a method equals that if that returns true the two stacks are equal and that and of integers To be considered equal, the two stacks would have false otherwise. returns values in the same order. Your metho. the same sequence of integer to store but must return them to their original state is to examine the two stacks terminating. before problem. as auxiliary You are to use one stack storage to solve this the two Stacks to compare. eguals should take two parameters: Methc 5. takes a Stack of integers Write a method coJ-lapse that pair each successive it by replacing of that collapses pair. For example, stores suppose a stack this of the bottom 17,2,8,9,4,13,7, Lr 9r 10l top top. of it, as a parameter and integers with the sur sequence of va.Lues: appear Assume that, stack values with 2 on top of it, the bottom, the top of the stack. from bot,tom to with 8 on top In other words, 7 is or and so ofrr with 10 at pair into pair 9 (7 + 2), The first into the second should be collapsed (8 + 9), the third pair into L7 should be collapsed collapsed 13) and so on to yield: bottom As before, the stack, elements, 19, L7, L'7, 8, 191 top should be L7 (4 + (with 9 on the bottom of stack values appear from bottom to top I'l on top of it, etc). ff the stack stores an odd number of is not collapsed. element For example, the sequence: the final [1,2,3,4,5] into: 7, 5] top top of the stack unchanged. storage to the stack sofve thj-s to collapse problem. Your top bottom would collapse bottom [3, with the 5 at the You are to use one queue as auxiliary parameter: method should take a single 6. that takes a Queue of integers Write a method rearrange as a parameter and rearranges the order of the values that so that all of the even values preserves .orde. appear before the odd values and that otherwise the original q stores For example, suppose a Queue called of the list. this sequence of valuee; front Then the [3, 5, 4, I7, 6,83, L,84, L6r 37] back following (q) ; call: rearrange shouLd rearrange of values: front 14, 6, the order so that the queue stores the following sequence 84, L6, 3, 5, I'7, 83, L, 371 back followed o rtdtnZll by l is Notice all of the evens appear at the front that of the queue o rder-oFthg the o dds-and t haE-the e vens i #re s ame a s-ttr-the and the order of the odds is the same as in the origrJ-nal Ij-st. You are to use one stack as auxiliary parameter: method should take a single storage to the queue problem. sofve this to rearranqe. Your ...
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