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>();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

push

A

public void push(E object){
theStack.prepend(object);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

stack toString

A

public String toString(){
return theStack.toString();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Queue start

A

public class QeueuAsMyLinkedList<E> {
MyLinkedList<E> theQueue;</E></E>

public QeueuAsMyLinkedList(){
    theQueue = new MyLinkedList<E>();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

enqueue

A

public void enqueue (E element){
theQueue.append(element);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
    }

}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

O(n) notation

A

HEAD SIDE TAIL SIDE
Push() O(1) O(1)
Pop() O(1) O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly