Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

StackAsMyArrayList

public class StackAsMyArrayList<E> 
{   
	MyArrayList<E> theStack;
    public StackAsMyArrayList()
    {  theStack = new MyArrayList<E>();       
    }
	
    public void push(E newElement) //insert at end of array!
    {  
		   if (!theStack.checkSpace())
		   throw new IndexOutOfBoundsException
                    ("Stack out of bounds");
		   theStack.add(theStack.getSize(),newElement);
    }
	
	public E pop() //remove end of array
    {  
		E temp = null;
		
		boolean isDone = false;
		if (theStack.getSize() > 0)
			temp=theStack.remove(theStack.getSize()-1);
		return temp; // temp will be null in special case of empty list
    }
    
	public String toString()
	{
		return theStack.toString();
	}
   
}//end class
Source by efundi.nwu.ac.za #
 
PREVIOUS NEXT
Tagged: #StackAsMyArrayList
ADD COMMENT
Topic
Name
7+8 =