Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Creates a stack with an array.
- * <p>
- * @author Mustafa Alparslan <mustafa.alparslan@boun.edu.tr>
- * @version 1.0
- */
- public class Stack {
- Character[] arr;
- int size;
- Stack()
- {
- this.arr = new Character[10];
- this.size = 10;
- }
- /**
- * Checks whether the stack is empty or not.
- * @return returns true if it is empty.
- */
- public boolean isEmpty()
- {
- if(arr[0] == null)
- return true;
- return false;
- }
- /**
- * Adds a new char to the stack.
- * If stack is full uses the method {@link #expandArr()} to expand stack.
- * @param ch is the character which will be added to stack.
- */
- public void push(char ch)
- {
- if(this.isFull()){
- this.expandArr();
- }
- arr[this.position(arr)] = ch;
- }
- /**
- * Takes the last character from stack and removes from stack.
- * @return the last char of the stack.
- */
- public char pop()
- {
- char temp =arr[this.position(arr)-1];
- arr[this.position(arr)-1]=null;
- return temp;
- }
- /**
- * Checks whether stack is full or not.
- * @return true if stack is full.
- */
- public boolean isFull()
- {
- if(arr[size-1] != null)
- return true;
- return false;
- }
- /**
- * Expands the size of stack by two.
- * Initially creates a new Temp char array and copy the values
- * from old stack to Temp.
- * Finally expands the size of stack and adds them one by one from the
- * Temp array.
- */
- public void expandArr()
- {
- Character[] temp = new Character[size];
- for(int i = 0; i < arr.length; i++){
- temp[i] = this.arr[i];
- }
- size = size*2;
- this.arr = new Character[size];
- for(int i = 0; i < temp.length; i++){
- arr[i] = temp[i];
- }
- }
- /**
- * Finds the current available position in an array
- * @param arry is the array which will be examined.
- * @return the current position.
- */
- public int position(Character[] arry)
- {
- int position = 0;
- for(int i = 0; i < arry.length; i++){
- if(arry[i] == null){
- position = i;
- break;
- }
- }
- return position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement