Skip to content

Instantly share code, notes, and snippets.

@palmerabollo
Created October 16, 2013 11:40
Show Gist options
  • Save palmerabollo/7006426 to your computer and use it in GitHub Desktop.
Save palmerabollo/7006426 to your computer and use it in GitHub Desktop.
Java
public class Stack {
private Object [ ] items ;
private int top ;
public Stack ( int size ) {
this items = new Object [ size ] ;
this top = - 1 ;
}
private boolean isEmpty ( ) {
return top == - 1 ;
}
private boolean isFull ( ) {
return top == items. length ;
}
public Object pop ( ) throws Exception {
if ( this isEmpty ( ) ) {
throw new Exception ( "The stack is Empty" ) ;
}
return items [ top -- ] ;
}
public void push ( Object obj ) throws Exception {
if ( this isFull ( ) ) {
throw new Exception ( "The stack is full" ) ;
}
items [ ++ top ] = obj ;
}
public static void main ( String args [ ] ) {
Stack stack = new Stack ( 3 ) ;
try
{
stack. push ( new String ( "a" ) ) ;
stack. push ( new String ( "ab" ) ) ;
stack. push ( new String ( "abc" ) ) ;
stack. push ( new String ( "abcd" ) ) ;
stack. pop ( ) ;
stack. pop ( ) ;
stack. pop ( ) ;
stack. pop ( ) ;
}
catch ( Exception ex ) {
System out println ( ex. getMessage ( ) ) ;
}
System out println ( "End" ) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment