Yuvalxp8 icon

Stacks_YuvalPorat

Yuvalxp8 | PRO | 02/17/18 04:41:07 PM UTC | 0 ⭐ | 445 👁️ | Never ⏰ | []
Java |

1.98 KB

|

None

|

0 👍

/

0 👎

public static<T> Stack<T> duplicate(Stack<T> st)
    {
        Stack<T> ezer = new Stack<T>();
        Stack<T> st2 = new Stack<T>();
        
        while (!st.isEmpty())
            ezer.push(st.pop());
        
        while (!ezer.isEmpty())
        {
            st2.push(ezer.top());
            st.push(ezer.pop());
        }
        return st2;
    }
    
    public static Stack<Integer> createStack()
    {
        Stack<Integer> st = new Stack<Integer>();
        System.out.println("enter the next Value for finish enter 999");
 
        int n =sc.nextInt();
        while (n!=999)
        {
            st.push(n);
            System.out.println("enter the next Value for finish enter 999");
 
            n =sc.nextInt();
        }
        return st;
    }
    
    public static<T> void toString(Stack<T> st)
    {
        Stack<T> s=duplicate(st);
        while (!s.isEmpty())
         System.out.print(s.pop().toString() + " ");
    }
 
    public static Scanner sc=new Scanner(System.in);
 
    
    public static<T> int howMany(Stack<T> st)
    {
        int counter = 0;
        while(!st.isEmpty())
        {
            st.pop();
            counter ++;
        }
        return counter;
    }
    
    public static<T> int returnBiggest(Stack<Integer> st)
    {
        int currentBiggest = 0;
        currentBiggest = st.top();
        while (!st.isEmpty())
        {
            if(currentBiggest < st.top())
                currentBiggest = st.top();
            st.pop();
        }       
        return currentBiggest;
    }
    
    public static<T> Stack<Integer>  dupInfo(Stack<Integer> st)
    {
        Stack<Integer> stCopy = duplicate(st);
        Stack<Integer> flipped = flipStack(stCopy);
        Stack<Integer> info = new Stack<Integer>();
        int current,counter;
        while(!flipped.isEmpty())
        {
            current = flipped.pop();
            counter = 1;
            while(!flipped.isEmpty() && current == flipped.top())
            {
                counter ++;
                flipped.pop();
            }
            
            info.push(counter);
            info.push(current);
            counter = 0;
        }
        return info;
    }
    
    public static<T> Stack<Integer> flipStack(Stack<Integer> st)
    {
        Stack<Integer> stFlipped = new Stack<Integer>();
        int first;
        while (!st.isEmpty())
        {
            first = st.pop();
            stFlipped.push(first);
        }
        return stFlipped;
    }

Comments