Question: Write a method called isSorted that accepts a stack of integers as a parameter and returns true if the elements in the stack occur in
Write a method called isSorted that accepts a stack of integers as a parameter and returns true if the elements in the stack occur in ascending (nondecreasing) order from top to bottom. That is, the smallest element should be on top, growing larger toward the bottom. For example, if the stack stores [20, 20, 17, 11, 8, 8, 3, 2], your method should return true. An empty or one-element stack is considered to be sorted. Your method must restore the parameter stack to its original state before returning. Use one queue or stack (but not both) as auxiliary storage.
Step by Step Solution
3.46 Rating (172 Votes )
There are 3 Steps involved in it
public static boolean is Sorted Stack s if ssize 2 return t... View full answer
Get step-by-step solutions from verified subject matter experts
