Created
October 16, 2013 11:40
-
-
Save palmerabollo/7006426 to your computer and use it in GitHub Desktop.
Java
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
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