Advertisement
cesarnascimento

acaiaca

Mar 23rd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package ex003;
  2.  
  3. public class Pilha {
  4.  
  5.     int[] elements;
  6.     int top = 0;
  7.     int remove = 0;
  8.  
  9.     public Pilha() {
  10.         elements = new int[4];
  11.     }
  12.  
  13.     public void push(int value) {
  14.         if (!isFull()) {
  15.             elements[top] = value;
  16.             top++;
  17.         } else {
  18.             System.out.println("Pilha cheia");
  19.         }
  20.     }
  21.  
  22.     public int pop() {
  23.         if (!isEmpty()) {
  24.             remove = elements[--top];
  25.             elements[top] = 0;
  26.             return remove;
  27.         } else {
  28.             return -1;
  29.         }
  30.     }
  31.  
  32.     private boolean isFull() {
  33.         if (top == elements.length)
  34.             return true;
  35.  
  36.         return false;
  37.     }
  38.  
  39.     private boolean isEmpty() {
  40.         if (top == 0)
  41.             return true;
  42.  
  43.         return false;
  44.     }
  45.    
  46.     public void reorganizar(Pilha pilha) {
  47.         Pilha par = new Pilha();
  48.         Pilha impar = new Pilha();
  49.         int atual;
  50.         for (int i = 0; i < elements.length; i++) {
  51.             atual = pilha.pop();
  52.             if(atual % 2 == 0) {
  53.                 par.push(atual);
  54.             }else {
  55.                 impar.push(atual);
  56.             }
  57.         }
  58.        
  59.         for (int j = 0; j < par.elements.length; j++) {
  60.             System.out.println(par.elements[j]);
  61.         }
  62.         for (int k = 0; k < impar.elements.length; k++) {
  63.             System.out.println(impar.elements[k]);
  64.         }
  65.     }
  66.    
  67.    
  68.    
  69. }
  70.  
  71.  
  72. package ex003;
  73.  
  74. public class nha {
  75.  
  76.     public static void main(String[] args) {
  77.  
  78.         Pilha p = new Pilha();
  79.        
  80.         p.push(1);
  81.         p.push(2);
  82.         p.push(3);
  83.         p.push(4);
  84.        
  85.         p.reorganizar(p);
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement