Stack and queue as MyArray Flashcards
Stack start
public class StackAsMyArrayList<E> {
MyArrayList<E> theStack;</E></E>
public StackAsMyArrayList(){ theStack = new MyArrayList<E>(); }
push
public void push(E element){
if(theStack.checkSpace()){
theStack.add(theStack.getSize(),element);
}
}
pop
public E pop(){
E temp = null;
if(theStack.getSize() > 0){
temp = theStack.remove(theStack.getSize() - 1);
}
return temp;
}
toString stack
public String toString(){
return theStack.toString();
}
Queue start
public class QueueAsMyArrayList<E> {
MyArrayList<E> theQueue;</E></E>
public QueueAsMyArrayList(){ theQueue = new MyArrayList<E>(); }
enqueue
public void enqueue(E element){
theQueue.add(theQueue.getSize(),element);
}
dequeue
public E dequeue(){
E temp = null;
if(theQueue.getSize()>0){
temp = theQueue.remove(0);
}
return temp;
}
queue toString
public String toString(){
return theQueue.toString();
}