CSE143 Midterm Cheat Sheet
Queues should be constructed using the Queue interface and the
LinkedList implementation. For example, to construct a queue of String
values, you would say:
Queue q = new LinkedList();
Stacks should be constructed using the Stack class (there is no interface).
For example, to construct a stack of String values, you would say:
Stack s = new Stack();
To transfer from a queue to a stack:
while (!q.isEmpty())
s.push(q.remove());
To transfer from a stack to a queue:
while (!s.isEmpty())
q.add(s.pop());
-------------------------------------------------------------------------------
For Stack, you are limited to the following operations:
public void push(E value); // push given value onto top of the stack
public E pop(); // removes and returns the top of the stack
public boolean isEmpty(); // returns whether or not stack is empty
public int size(); // returns number of elements in the stack
For Queue you are allowed the following operations:
public void add(E value); // inserts given value at the end of the queue
public E remove(); // removes and returns the front of the queue
public boolean isEmpty(); // returns whether or not queue is empty
public int size(); // returns number of elements in the queue
YOU ARE NOT ALLOWED TO USE FOREACH LOOPS OR ITERATORS FOR EITHER STRUCTURE.
-------------------------------------------------------------------------------
Below is an example of a method that could be added to the LinkedIntList class
to compute the sum of the list:
public int sum() {
int sum = 0;
ListNode current = front;
while (current != null) {
sum += current.data;
current = current.next;
}
return sum;
}