Skip to content

Instantly share code, notes, and snippets.

@anupsavvy
Created July 19, 2011 02:37
Show Gist options
  • Save anupsavvy/1091196 to your computer and use it in GitHub Desktop.
Save anupsavvy/1091196 to your computer and use it in GitHub Desktop.
Creation of simple array based stack
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