Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Functions {
- public static Scanner UI = new Scanner(System.in);
- public static<T> int stackLength(Stack<T> stack)
- {
- int counter = 0;
- Stack<T> save = duplicate(stack);
- while(!save.isEmpty())
- {
- counter++;
- save.pop();
- }
- return counter;
- }
- public static<T> Stack<T> duplicate(Stack<T> stack)
- {
- Stack<T> temp = new Stack<T>();
- Stack<T> newStack = new Stack<T>();
- while(!stack.isEmpty())
- {
- temp.push(stack.top());
- stack.pop();
- }
- while(!temp.isEmpty())
- {
- stack.push(temp.top());
- newStack.push(temp.pop());
- }
- return newStack;
- }
- public static<T> Stack<T> duplicateReverse(Stack<T> stack)
- {
- Stack<T> temp = duplicate(stack);
- Stack<T> newStack = new Stack<T>();
- while(!stack.isEmpty())
- newStack.push(temp.pop());
- return newStack;
- }
- public static Stack<Integer> intStack()
- {
- Stack<Integer> stack = new Stack<Integer>();
- 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<T> void toString(Stack<T> stack)
- {
- Stack<T> temp = duplicate(stack);
- while(!temp.isEmpty())
- {
- System.out.print(temp.pop().toString() + " , ");
- }
- System.out.print(" null");
- }
- public static int maxNum(Stack<Integer> stack)
- {
- int max = 0;
- Stack<Integer> temp = duplicate(stack);
- while(!temp.isEmpty())
- {
- if(max < temp.top())
- max = temp.pop();
- else
- temp.pop();
- }
- return max;
- }
- public static boolean inStack(Stack<Integer> stack, int num)
- {
- Stack<Integer> temp = duplicate(stack);
- while(!temp.isEmpty())
- {
- if(num == temp.top())
- return true;
- }
- return false;
- }
- public static int inStackTimes(Stack<Integer> stack, int num)
- {
- int counter = 0;
- Stack<Integer> temp = duplicate(stack);
- while(!temp.isEmpty())
- {
- if(num == temp.top())
- counter++;
- temp.pop();
- }
- return counter;
- }
- public static Stack<Integer> removeNum(Stack<Integer> stack, int num)
- {
- Stack<Integer> temp = duplicate(stack);
- Stack<Integer> newStack = new Stack<Integer>();
- while(!temp.isEmpty())
- {
- if(num == temp.top())
- temp.pop();
- else
- newStack.push(temp.pop());
- }
- return newStack;
- }
- public static<T> T getPos(Stack<T> stack, int pos)
- {
- Stack<T> temp = duplicate(stack);
- while(pos > 1)
- {
- temp.pop();
- pos--;
- }
- return temp.top();
- }
- public static<T> boolean isEquals(Stack<T> stack, Stack<T> stack2)
- {
- Stack<T> temp = duplicate(stack);
- Stack<T> 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;
- }
- }
Add Comment
Please, Sign In to add comment