Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphoarty/804517b689775e2f687360a2094a1c57 to your computer and use it in GitHub Desktop.
Save graphoarty/804517b689775e2f687360a2094a1c57 to your computer and use it in GitHub Desktop.
public class Stack {
static final int max = 5;
int[] stack = new int[max];
int top;
Stack(){
top = 0;
}
int pop(){
try{
return stack[--top];
}catch(Exception e){
System.out.println("Cannot pop anymore!");
return -1;
}
}
void push(int num){
if(top<max){
stack[top++] = num;
}else{
System.out.println("Elements cannot be added!");
}
}
boolean isStackFull(){
if(top>=max-1){
return true;
}else{
return false;
}
}
}
public class StackImplementation {
public static void main(String[] args){
Stack stack = new Stack();
System.out.println( (stack.pop()==-1) ? "Empty Stack!" : "Not Empty!" );
stack.push(30);
System.out.println(stack.pop());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment