CODE
import java.util.*;
class ArrayBasedStack
{
protected int arr[];
protected int top, size, len;
public ArrayBasedStack(int n)
{
size = n;
len = 0;
arr = new
int[size];
top = -1;
}
public boolean isEmpty()
{
return top == -1;
}
public boolean isFull()
{
return top == size -1
;
}
public int getSize()
{
return len ;
}
public int peek()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return arr[top];
}
public void push(int i)
{
if(top + 1 >=
size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size
)
arr[++top] = i;
len++ ;
}
public int pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
len-- ;
return arr[top--];
}
}
Get Answers For Free
Most questions answered within 1 hours.