Thursday, May 3, 2012


class Queue<T>
{
SLL<T> list = new SLL<T>();
public int size() { return list.size(); }
public void enqueue(T t) { list.addAtTail(t); }
public T dequeue(void) { return list.removeFront(); }
}

class Stack<T>
{
SLL<T> list = new SLL<T>();
public int size() { return list.size(); }
public void push(T t) { list.addAtFront(t); }
public T dequeue(void) { return list.removeFront(); }
}

class Stack<T>
{
ArrayList<T> list = new ArrayList<T>();
public int size() { return list.size(); }
public void push(T t) { list.add(list.size(), t); }
public T pop(void) { return list.remove(list.size()); }
}

class Stack<T> extends SLL<T>
{
public int size() { return super.size(); }
public void push(T t) { addAtFront(t); }
public T dequeue(void) { return removeFront(); }
}

// NOT adapter pattern
class Stack<T>
{
SLNode<T> head, tail;
void push(T t) {
SLNode n = new Node<T>(t,head);
head = n;
size++;
}
T pop() {
SLNode n = head;
head = head.next;
return n.value;
size--;
}
}

class PQ<T>
{
Comparator<T> comp;
SLL<T> list = new SLL<T>();
PQ(Comparator<T> c)
{
comp = c;
}
void enqueue(T t) { list.insertAtEnd(t); }
T dequeue()
{
T theMin = list.getFirstItem();
for (T element : list)
{
if(comp.compare(theMin, element) < 0)
{
theMin = element;
}
}
// change to explicit iterator
for (T element : list)
{
if(comp.compare(theMin, element) == 0)
{
it.remove();
return theMin;
}
}
}
}

No comments:

Post a Comment