Yuvalxp8

Magazines

Nov 13th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Functions {
  4.     public static Scanner UI = new Scanner(System.in);
  5.     public static<T> int stackLength(Stack<T> stack)
  6.     {
  7.         int counter = 0;
  8.         Stack<T> save = duplicate(stack);
  9.         while(!save.isEmpty())
  10.         {
  11.             counter++;
  12.             save.pop();
  13.         }
  14.         return counter;
  15.     }
  16.  
  17.     public static<T> Stack<T> duplicate(Stack<T> stack)
  18.     {
  19.         Stack<T> temp = new Stack<T>();
  20.         Stack<T> newStack = new Stack<T>();
  21.         while(!stack.isEmpty())
  22.         {
  23.             temp.push(stack.top());
  24.             stack.pop();
  25.         }
  26.         while(!temp.isEmpty())
  27.         {
  28.             stack.push(temp.top());
  29.             newStack.push(temp.pop());
  30.         }
  31.         return newStack;
  32.     }
  33.  
  34. public static<T> Stack<T> duplicateReverse(Stack<T> stack)
  35.     {
  36.         Stack<T> temp = duplicate(stack);
  37.         Stack<T> newStack = new Stack<T>();
  38.         while(!stack.isEmpty())
  39.             newStack.push(temp.pop());
  40.         return newStack;
  41.     }
  42.  
  43.     public static Stack<Integer> intStack()
  44.     {
  45.         Stack<Integer> stack = new Stack<Integer>();
  46.         System.out.println("Enter value (enter '999' to stop)");
  47.         int userInput = UI.nextInt();
  48.         while(userInput != 999)
  49.         {
  50.             stack.push(userInput);
  51.             System.out.println("Enter value (enter '999' to stop)");
  52.             userInput = UI.nextInt();
  53.         }
  54.         return stack;
  55.     }
  56.  
  57.     public static<T> void toString(Stack<T> stack)
  58.     {
  59.         Stack<T> temp = duplicate(stack);
  60.         while(!temp.isEmpty())
  61.         {
  62.             System.out.print(temp.pop().toString() + " , ");
  63.         }
  64.         System.out.print(" null");
  65.     }
  66.  
  67.     public static int maxNum(Stack<Integer> stack)
  68.     {
  69.         int max = 0;
  70.         Stack<Integer> temp = duplicate(stack);
  71.         while(!temp.isEmpty())
  72.         {
  73.             if(max < temp.top())
  74.                 max = temp.pop();
  75.             else
  76.                 temp.pop();
  77.         }
  78.         return max;
  79.     }
  80.     public static boolean inStack(Stack<Integer> stack, int num)
  81.     {
  82.         Stack<Integer> temp = duplicate(stack);
  83.         while(!temp.isEmpty())
  84.         {
  85.             if(num == temp.top())
  86.                 return true;
  87.         }
  88.         return false;
  89.     }
  90.     public static int inStackTimes(Stack<Integer> stack, int num)
  91.     {
  92.         int counter = 0;
  93.         Stack<Integer> temp = duplicate(stack);
  94.         while(!temp.isEmpty())
  95.         {
  96.             if(num == temp.top())
  97.                 counter++;
  98.             temp.pop();
  99.         }
  100.         return counter;
  101.     }
  102.  
  103.     public static Stack<Integer> removeNum(Stack<Integer> stack, int num)
  104.     {
  105.         Stack<Integer> temp = duplicate(stack);
  106.         Stack<Integer> newStack = new Stack<Integer>();
  107.         while(!temp.isEmpty())
  108.         {
  109.             if(num == temp.top())
  110.                 temp.pop();
  111.             else
  112.                 newStack.push(temp.pop());
  113.         }
  114.         return newStack;
  115.     }
  116.  
  117.     public static<T> T getPos(Stack<T> stack, int pos)
  118.     {
  119.         Stack<T> temp = duplicate(stack);
  120.         while(pos > 1)
  121.         {
  122.             temp.pop();
  123.             pos--;
  124.         }
  125.         return temp.top();
  126.     }
  127.  
  128.     public static<T> boolean isEquals(Stack<T> stack, Stack<T> stack2)
  129.     {
  130.         Stack<T> temp = duplicate(stack);
  131.         Stack<T> temp2 = duplicate(stack2);
  132.         int length = stackLength(temp);
  133.         int counter = 0;
  134.         while(!temp.isEmpty())
  135.         {
  136.             if(temp.pop() == temp2.pop())
  137.                 counter++;
  138.         }
  139.         if(counter == length)
  140.             return true;
  141.         else
  142.             return false;
  143.     }
  144. }
Add Comment
Please, Sign In to add comment