Section7Problems 3 - '7 Write a method reverseHalf that...

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: '7 . Write a method reverseHalf that reverses the order of half the elernents of a queue of integers. Your method should rewerse tbre order of aI]. the elements (position positions in odd-numbered L, 3, 5, etc) assuming that the first value in the queue has position 0. For the if the queue originally example, method is called: front it should front , store lL, 8, 7, 2, 9, 18, L2, values 2, L2, Bl 0] stores this sequence of integers when back after back (positions 0, 2, 4, 6) have not moved. the method finishes execut,ingr: the 0, 1, following 18, 9, Notice numbers that That subsequence of in even positions integers is sti1l: (1, 7, 9, rz',) But notice that the numbers in odd positj-ons in reverse relative order to the original. subsequence: (8, has 2, 18, o) (positions L, 3, 5, 1) are fn other words, the original now become: (o, 18, 2, 8) problern. to solve this the queue to modify. You should use a single stack as auxiliary storage parameter: Method reverseHalf shoul-d take a single 8. Write a method isPalindrome that takes a Queue of integers as a parameter whether or not tbe numbers in the gueue represent and that returns a (true palindrome if they do, false otherwise) A sequence of numbers is considered a palj-ndrome if it is the same in reverse For example, order. q stores suppose a Queue called this sequence of values: front Then the [3, 8, L7, 9, L7, 8, 3] back fol-Lowing call: (g) the same in reverse order. If ispatindrome should return E,he l istr h ad front the not caII the true because this sequence is i nstead s €-ered- t hese v aluesl-----8, !], 9, 4, I7, 8, 3l back [3, on isPalindrome same in reverse would instead return (the 9 and 4 in order false because this sequence the middl.e don't match). is The empty queue should be considered assumptions about how many elements restore the queue so that it stores call- as it did before. yiujr_g_tg u .2-e_qte_ql_e-qk a s a palindrome. You may not make any are in the queue and your method must the same sequence of values after t.he s toraqe t o_S_A_LVe_Lhis p roblem. a uxj-l_igrry ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online