Advertisement
FahimFaisal

ArrayStack

Mar 8th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1.  
  2. package stackPractice;
  3.  
  4.  
  5.  
  6. /**
  7.  *
  8.  * @author acer
  9.  */
  10. public class ArrayStack implements Stack{
  11.     int size;
  12.     Object [] data;
  13.     int top;
  14.    
  15.     public ArrayStack(){
  16.     size=0;
  17.     top=-1;
  18.     data=new Object[7];  
  19.    
  20.     }
  21.    
  22.     // The number of items on the stack
  23.     public int size(){
  24.         return top+1;
  25.     }
  26. // Returns true if the stack is empty
  27.     public boolean isEmpty(){
  28.         if(top==-1){
  29.             return true;
  30.         }else{
  31.             return false;
  32.         }
  33.     }
  34. // Pushes the new item on the stack, throwing the
  35. // StackOverflowException if the stack is at maximum capacity. It
  36. // does not throw an exception for an "unbounded" stack, which
  37. // dynamically adjusts capacity as needed.
  38.     public void push(Object e) throws StackOverflowException{
  39.         if(top+1==data.length){
  40.             throw new StackOverflowException();
  41.            
  42.         }
  43.      
  44.             top++;
  45.             data[top]=e;
  46.             size++;
  47.        
  48.     }
  49. // Pops the item on the top of the stack, throwing the
  50. // StackUnderflowException if the stack is empty.
  51.     public Object pop() throws StackUnderflowException{
  52.         if(top==-1){
  53.             throw new StackUnderflowException();
  54.         }
  55.  
  56.             Object val=data[top];
  57.             data[top]=null;
  58.             top--;
  59.             size--;
  60.             return val;
  61.            
  62.       }
  63. // Peeks at the item on the top of the stack, throwing
  64. // StackUnderflowException if the stack is empty.
  65.     public Object peek() throws StackUnderflowException{
  66.         if(top==-1){
  67.             throw new StackUnderflowException();
  68.         }
  69.         return data[top];
  70.     }
  71. // Returns a textual representation of items on the stack, in the
  72. // format "[ x y z ]", where x and z are items on top and bottom
  73. // of the stack respectively.
  74.     public String toString(){
  75.         String str="[";
  76.         for(int i=top;i>=0;i--){
  77.             if(i==0){
  78.                 str+=data[i]+"]";
  79.             }else{
  80.             str+=data[i]+",";
  81.             }
  82.         }
  83.         return str;
  84.     }
  85. // Returns an array with items on the stack, with the item on top
  86. // of the stack in the first slot, and bottom in the last slot.
  87.     public Object[] toArray(){
  88.         Object [] arr=new Object[size()];
  89.         int k=0;
  90.         for(int i=top;i>=0;i--){
  91.             arr[k]=data[i];
  92.             k++;
  93.         }
  94.         return arr;
  95.     }
  96. // Searches for the given item on the stack, returning the
  97. // offset from top of the stack if item is found, or -1 otherwise.
  98.     public int search(Object e){
  99.        int idx=-1;
  100.        
  101.         for(int i=0;i<size();i++){
  102.             if(e.equals(data[i])){
  103.                idx=i;
  104.             }
  105.         }
  106.         return idx;
  107.     }
  108.    
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement