Advertisement
sindi29

Bazicna upotreba na Stack

Oct 30th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package asda;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. interface Stack<E>
  8. {
  9.     public void push(E element);
  10.     public E pop();
  11.     public E peek();
  12.     public boolean isFull();
  13.     public boolean isEmpty();
  14.     public void clear();
  15. }
  16. class ArrayStack<E> implements Stack
  17. {
  18.     int depth;
  19.     int size;
  20.     E elems[];
  21.     public ArrayStack(int size)
  22.     {
  23.         this.size = size;
  24.         elems = (E[]) new Object[size];
  25.         depth = 0;
  26.     }
  27.     @Override
  28.     public void push(Object element) {
  29.         E pom = (E) element;
  30.         elems[depth++] = pom;
  31.     }
  32.  
  33.     @Override
  34.     public Object pop() {
  35.         E pom = elems[--depth];
  36.         elems[depth]=null;
  37.         return pom;
  38.     }
  39.  
  40.     @Override
  41.     public Object peek() {
  42.         return elems[depth-1];
  43.     }
  44.  
  45.     @Override
  46.     public boolean isFull() {
  47.         return depth == size;
  48.     }
  49.  
  50.     @Override
  51.     public boolean isEmpty() {
  52.         return depth ==0;
  53.     }
  54.  
  55.     @Override
  56.     public void clear() {
  57.         for(int i=0;i<depth;i++)
  58.         {
  59.             elems[i] = null;
  60.         }
  61.         depth = 0;
  62.            
  63.     }
  64. }
  65. public class Asda {
  66.  
  67.    
  68.     public static void main(String[] args) throws IOException{
  69.         ArrayStack<Byte> magacin =new  ArrayStack<>(32);
  70.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  71.         int N = Integer.parseInt(stdin.readLine());
  72.            
  73.         while(N>0)
  74.         {
  75.             magacin.push(N%2);
  76.             N = N/2;
  77.            
  78.         }
  79.         while(!magacin.isEmpty())
  80.         {
  81.             System.out.print(magacin.pop());
  82.         }
  83.         System.out.println();
  84.     }
  85.    
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement