Write a method called switchPairs that accepts a stack of integers as a parameter and swaps neighboring pairs of numbers starting at the bottom of the stack. For example, if the stack initially stores...



Write a method called switchPairs that accepts a stack of integers as a


parameter and swaps neighboring pairs of numbers starting at the


bottom of the stack. For example, if the stack initially stores [1, 2, 8,


6, 21, 15, 7], your method should swap the first pair (1, 2), the


second pair (8, 6), the third pair (21, 15), and so on. If the stack contains


an odd number of elements, the element at the top should remain


unmodified. So the final state of the stack would be [2, 1, 6, 8, 15,


−1, 7]. Use one queue as auxiliary storage.


10. Write a method called isConsecutive that accepts a stack of integers as


a parameter and that returns true if the stack contains a sequence of


consecutive integers starting from the bottom of the stack. Consecutive


integers are integers that come one after the other, as in 3, 4, 5, etc. If the


stack stores [5, 6, 7, 8, 9, 10], your method should return true. If


the stack had instead contained [7, 8, 9, 10, 12], your method


should return false because the numbers 10 and 12 are not consecutive.


Notice that we look at the numbers starting at the bottom of the stack.


Any stack with fewer than two values should be considered to be a list


of consecutive integers. Your method must restore the parameter stack


to its original state before returning. Use one queue as auxiliary storage.

Nov 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here