I am new to the concept of data structures and I have created a Stack for my own usage.
public class ArrayBasedStackImpl<T> {
@SuppressWarnings("unchecked")
private T[] DataStack= (T[]) new Object[10];
private int stack_pointer=-1;
public void push(T t) throws Exception
{
if(stack_pointer>9)
{
throw new Exception("Stack is full");
}
DataStack[++stack_pointer]=t;
}
public T pop() throws Exception
{
if(stack_pointer==-1)
{
throw new Exception("Stack Empty");
}
else
{
return DataStack[stack_pointer--];
}
}
public T peek()
{
return DataStack[stack_pointer];
}}
I have used an Array as a backend storage to start with.Before actually implementing it in full scale,there are a few questions that I have below
Is it a good idea to use throw new Exception, or should I be writing my own exception class for this,considering the case that the stack is memory limited.
Is Array actually a good idea for a Stack based implementation,my usage scenario is where I push 10 objects of considerable size into it,so I want to be able to free memory once I pop an item of the stack.How can I delete an item from an array,I googled much but couldnt find anything good.Will a linked list be better in this place when I want to clear it from memory.
I am using generics here,is it a bad practice to use Array with Generics?