Created
July 19, 2011 02:37
-
-
Save anupsavvy/1091196 to your computer and use it in GitHub Desktop.
Creation of simple array based stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.operations.stack; | |
public class Stack{ | |
private static final int SIZE = 1024; | |
private int N; | |
private int t = -1; | |
private int[] arr = null; | |
public Stack(){ | |
this.N = SIZE; | |
arr = new int[this.N]; | |
} | |
public Stack(int N){ | |
this.N = N; | |
arr = new int[this.N]; | |
} | |
public int pop() throws StackException{ | |
if(!isEmpty()){ | |
return this.arr[t--]; | |
} | |
else{ | |
throw new StackException("Error : Stack is Empty"); | |
} | |
} | |
public void push(int num) throws StackException{ | |
if(size() < this.N){ | |
this.arr[++t] = num; | |
} | |
else{ | |
throw new StackException("Error : Stack is full"); | |
} | |
} | |
public int size(){ | |
return t+1; | |
} | |
public boolean isEmpty(){ | |
return t < 0; | |
} | |
} | |
class StackException extends RuntimeException{ | |
public StackException(String message){ | |
super(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment