import java.util.*; public class Functions { public static Scanner UI = new Scanner(System.in); public static int stackLength(Stack stack) { int counter = 0; Stack save = duplicate(stack); while(!save.isEmpty()) { counter++; save.pop(); } return counter; } public static Stack duplicate(Stack stack) { Stack temp = new Stack(); Stack newStack = new Stack(); while(!stack.isEmpty()) { temp.push(stack.top()); stack.pop(); } while(!temp.isEmpty()) { stack.push(temp.top()); newStack.push(temp.pop()); } return newStack; } public static Stack duplicateReverse(Stack stack) { Stack temp = duplicate(stack); Stack newStack = new Stack(); while(!stack.isEmpty()) newStack.push(temp.pop()); return newStack; } public static Stack intStack() { Stack stack = new Stack(); System.out.println("Enter value (enter '999' to stop)"); int userInput = UI.nextInt(); while(userInput != 999) { stack.push(userInput); System.out.println("Enter value (enter '999' to stop)"); userInput = UI.nextInt(); } return stack; } public static void toString(Stack stack) { Stack temp = duplicate(stack); while(!temp.isEmpty()) { System.out.print(temp.pop().toString() + " , "); } System.out.print(" null"); } public static int maxNum(Stack stack) { int max = 0; Stack temp = duplicate(stack); while(!temp.isEmpty()) { if(max < temp.top()) max = temp.pop(); else temp.pop(); } return max; } public static boolean inStack(Stack stack, int num) { Stack temp = duplicate(stack); while(!temp.isEmpty()) { if(num == temp.top()) return true; } return false; } public static int inStackTimes(Stack stack, int num) { int counter = 0; Stack temp = duplicate(stack); while(!temp.isEmpty()) { if(num == temp.top()) counter++; temp.pop(); } return counter; } public static Stack removeNum(Stack stack, int num) { Stack temp = duplicate(stack); Stack newStack = new Stack(); while(!temp.isEmpty()) { if(num == temp.top()) temp.pop(); else newStack.push(temp.pop()); } return newStack; } public static T getPos(Stack stack, int pos) { Stack temp = duplicate(stack); while(pos > 1) { temp.pop(); pos--; } return temp.top(); } public static boolean isEquals(Stack stack, Stack stack2) { Stack temp = duplicate(stack); Stack temp2 = duplicate(stack2); int length = stackLength(temp); int counter = 0; while(!temp.isEmpty()) { if(temp.pop() == temp2.pop()) counter++; } if(counter == length) return true; else return false; } }