Advertisement
malixds_

splitStack

Jan 16th, 2023
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5.  
  6.     void splitStack(Stack<Integer> st){
  7.         Stack<Integer> tmpStack = new Stack<Integer>();
  8.         Stack<Integer> ans = new Stack<Integer>();
  9.  
  10.         int tmp = st.pop();
  11.         if (tmp < 0){
  12.             ans.push(tmp);
  13.         } else{
  14.             tmpStack.push(tmp);
  15.         }
  16.         while (st.empty() == false){
  17.             tmp = st.pop();
  18.             if (tmp < 0){
  19.                 ans.push(tmp);
  20.             } else{
  21.                 tmpStack.push(tmp);
  22.             }
  23.         }
  24.         while (tmpStack.empty() == false){
  25.             ans.push(tmpStack.pop());
  26.         }
  27.     }
  28.  
  29.     public static void main(String[] args) {
  30.         Stack<Integer> st = new Stack<Integer>();
  31.         Scanner sc = new Scanner(System.in);
  32.         int n = sc.nextInt();
  33.         for (int i = 0; i < n; ++i){
  34.             st.push(sc.nextInt());
  35.         }
  36.  
  37.  
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement