SHARE
TWEET

Stack

musdawdaf Aug 13th, 2011 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *      Creates a stack with an array.
  3.  * <p>
  4.  *  @author Mustafa Alparslan <mustafa.alparslan@boun.edu.tr>
  5.  *  @version 1.0
  6.  */
  7. public class Stack {
  8.         Character[] arr;
  9.         int size;
  10.        
  11.         Stack()
  12.         {
  13.                 this.arr = new Character[10];
  14.                 this.size = 10;
  15.         }
  16.         /**
  17.          * Checks whether the stack is empty or not.
  18.          * @return returns true if it is empty.
  19.          */
  20.         public boolean isEmpty()
  21.         {
  22.                 if(arr[0] == null)
  23.                 return true;
  24.                
  25.                 return false;
  26.         }
  27.         /**
  28.          * Adds a new char to the stack.
  29.          * If stack is full uses the method {@link #expandArr()} to expand stack.
  30.          * @param ch is the character which will be added to stack.
  31.          */
  32.         public void push(char ch)
  33.         {
  34.                 if(this.isFull()){
  35.                         this.expandArr();
  36.                 }
  37.                 arr[this.position(arr)] = ch;
  38.                
  39.         }
  40.         /**
  41.          * Takes the last character from stack and removes from stack.
  42.          * @return the last char of the stack.
  43.          */
  44.         public char pop()
  45.         {      
  46.                 char temp =arr[this.position(arr)-1];
  47.                 arr[this.position(arr)-1]=null;
  48.                 return temp;
  49.         }
  50.        
  51.         /**
  52.          * Checks whether stack is full or not.
  53.          * @return true if stack is full.
  54.          */
  55.         public boolean isFull()
  56.         {
  57.                 if(arr[size-1] != null)
  58.                 return true;
  59.                
  60.                 return false;
  61.         }
  62.         /**
  63.          * Expands the size of stack by two.
  64.          * Initially creates a new Temp char array and copy the values
  65.          * from old stack to Temp.
  66.          * Finally expands the size of stack and adds them one by one from the
  67.          * Temp array.
  68.          */
  69.         public void expandArr()
  70.         {
  71.                 Character[] temp = new Character[size];
  72.                 for(int i = 0; i < arr.length; i++){
  73.                         temp[i] = this.arr[i];
  74.                 }
  75.                 size = size*2;
  76.                 this.arr = new Character[size];
  77.                 for(int i = 0; i < temp.length; i++){
  78.                         arr[i] = temp[i];
  79.                 }
  80.         }
  81.        
  82.         /**
  83.          * Finds the current available position in an array
  84.          * @param arry is the array which will be examined.
  85.          * @return the current position.
  86.          */
  87.         public int position(Character[] arry)
  88.         {
  89.                 int position = 0;
  90.                 for(int i = 0; i < arry.length; i++){
  91.                         if(arry[i] == null){
  92.                                 position = i;
  93.                                 break;
  94.                         }
  95.                 }
  96.                 return position;
  97.         }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top