Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Stack2{
- private int[] stack;
- private int size;
- public Stack2(){
- stack = new int[10];
- size = 0;
- }
- public Stack2(int height){
- if(height <= 0){
- throw new EmptyStackException();
- }
- stack = new int[height];
- size = 0;
- }
- public void add(int value){
- if (size == stack.length){
- throw new StackOverflowException();
- }
- stack[size] = value;
- size++;
- }
- public int pop(){
- if(size == 0){
- throw new EmptyStackException();
- }
- size--;
- return stack[size + 1];
- }
- public int peek(){
- if(size == 0){
- throw new EmptyStackException();
- }
- return stack[size - 1];
- }
- public boolean isEmpty(){
- if(size == 0){
- return true;
- }
- return false;
- }
- public int getSize(){
- return this.size;
- }
- class StackOverflowException extends RuntimeException{
- public StackOverflowException(){
- super("Nothing can be added to the stack. The stack was full and has overflowed");
- }
- public StackOverflowException(String message){
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement