Section7Problems 4

# Section7Problems 4 - 9 that takes a Stack of integers as a...

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: 9. \ that, takes a Stack of integers as a parameter Write a method switchPairs pairs of numbers starting at the bottom of the successive switches and that if stores these values: For example, the stack initially stack. bottom [ 3, 8, L7 , 9, 99, 9, L ' 7, 8, 3, L, 2, 3, 4, 14 ] top pair (17, 9) , \ your method should switch the first (99, 91, and so oor pair the third bottom [8r 3r 9, L7r 9r 99, B, (3, 8) , pair yielding this L7, Lr 3r 3r the second sequence': 2, ]-4r 4] top of are If there is the stack bottom j.n the stack, an odd number of values the value at the top For example, lf the original stack had stored: not moved. [3, 8, L"7, 9, 99, 9, L7, 8, 3, L, 2, 3, 4, L4, 421 top top of t.he pairs It would again switch of (421 would not be moved, stack bottom [8, 3, 9, !7, 9, 99, vafues, yielding 8, L7, but the vaLue at this sequence: 1-, 3, 3, 2, L4, the 4, 42) top stack. about how many elements You may not make any assumptions are in the problem. You are to use one gueue as auxiliary storagre to solve this 10. that Write takes a stack of integers a method isConsecutive as a parameter returns whether or not the stack contains and that a sequence of (returning from the bottom of the stack integers starting consecutive true false if it does not) . if it, does, returning Consecutive 6, 7, 8, 9, bottom then the integers are integers that come one after So if etc. a stack s stores the following [3, 4, 5, 5, 7, 8, 9, 10] top the other, values: as in 5, call: (s) true. 4,5, If the stack 9,10, had instead Lzl top contained this set of values: isConsecutive shouLd return [3, bottom 6,7,8, should return false because the numbers 10 and L2 are not then the call Notice we look at the numbers starting that consecutive. at the bottom ofThe following sequence of values would be consecutive the stack. except f acr f o r*tlre t haL i L a pPearg, i n r everseo rder, m etsIe4 w or*]<! r eturn s o_the false: bottom [3, 2, 1-] top restore the stack so that it stores the same sequence call as it did before. Any stack with fewer than two considered to be a list of consecutive integers. as auxiliary storagre to solve this problem. of Your method must values after the values should be You are to use one queue ...
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