Stacks and Queues as Linked Flashcards
1
Q
Stack start
A
public class StackAsMyLinkedList<E> {
MyLinkedList<E> theStack;</E></E>
public StackAsMyLinkedList(){ theStack = new MyLinkedList<E>(); }
2
Q
push
A
public void push(E object){
theStack.prepend(object);
}
3
Q
pop
A
public E pop(){
E temp = null;
boolean isDone = false;
temp = theStack.getFirst();
if(temp != null){ isDone = theStack.delete(temp); } if(isDone){ return temp; } else { return null; } }
4
Q
stack toString
A
public String toString(){
return theStack.toString();
}
5
Q
Queue start
A
public class QeueuAsMyLinkedList<E> {
MyLinkedList<E> theQueue;</E></E>
public QeueuAsMyLinkedList(){ theQueue = new MyLinkedList<E>(); }
6
Q
enqueue
A
public void enqueue (E element){
theQueue.append(element);
}
7
Q
dequeue
A
public E dequeue(){
boolean isDone = false;
E temp;
temp = theQueue.getFirst(); if(temp != null){ isDone = theQueue.delete(temp); } if(isDone){ return temp; } else { return null; } }
8
Q
O(n) notation
A
HEAD SIDE TAIL SIDE
Push() O(1) O(1)
Pop() O(1) O(n)