desislava_topuzakova

01. Reverse Numbers with a Stack

May 21st, 2021
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3. import java.util.Stack;
  4.  
  5. public class demo {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.        //"1 2 3 4 5"
  9.         String input = scanner.nextLine();
  10.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  11.         String[] splittedInput = input.split(" "); // ["1", "2", "3", "4", "5"]
  12.         for (String element : splittedInput) {
  13.             stack.push(Integer.parseInt(element));
  14.         }
  15.  
  16.         //stop: stack e empty
  17.         while(!stack.isEmpty()) {
  18.             System.out.print(stack.pop() + " ");
  19.         }
  20.  
  21.  
  22.  
  23.     }
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment