


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



public class VectorQueue extends Vector implements Queue { public Object getFirst() { return elementAt(0); } public void removeFirst() { removeElementAt(0); } }
head ( firstFilled )
free ( firstFree )
public class RingBufferQueue implements Queue { private int num; // Number of nodes in queue. private RingBufferNode head, // Head of list (first filled node). free; // Next free node. RingBufferQueue() { head = free = new RingBufferNode(); num = 0; } // Implementation of the Collection interface. public boolean isEmpty() { return num == 0; } public int size() { return num; }
// Implementation of the Queue interface. public synchronized void addLast(Object obj) { if (free.next == head) // Full, so create a node. free.next = new RingBufferNode(free.next); free.obj = obj; free = free.next; num++; } ...
// Implementation of the Queue interface (cont.). public Object getFirst() { if (head == free) throw new NoSuchElementException; return head.obj; }
public void removeFirst() { if (head == free) throw new NoSuchElementException;
head.obj = null; head = head.next; num--; } } // End of RingBufferQueue class.
public class RingBufferNode { public Object obj; public RingBufferNode next;
public RingBufferNode() { next = this; } public RingBufferNode(RingBufferNode n) { next = n; } }
interface Deque extends Stack, Queue { public void addFirst() }
IndexedDeque: Indexed Interface (cont.)
public synchronize void addElementAt(int idx, Object obj) { if (idx < 0 || idx > count) throw new ArrayIndexOutOfBoundsException(idx); setSize(count + 1); for (int i = count – 1; i > idx; i--) setElementAt(elementAt(i - 1), i); // Slide data up one spot. setElementAt(obj, idx); }
public synchronized void removeElementAt(int idx) { if (idx < 0 || idx >= count) throw new ArrayIndexoutOfBoundsException(idx); while (++idx < count) setElementAt(elementAt(idx), idx – 1); // Slide data down one spot. setElementAt(null, idx - 1); count--; }
IndexedDeque: Indexed Interface (cont.)
*public void setSize(int size) { int capacity = data.length; while (size > capacity) capacity = 2; ensureCapacity(capacity); count = size; }
public synchronize void ensureCapacity(int capacity) { if (capacity <= data.length) return; int i = first, idx = 0; Object [] old = data; data = new Object[capacity]; while (idx < count && i < data.length) data[idx++] = old[i++]; for (i = 0; idx < count; ) data[idx++] = old[i++]; first = 0; }
IndexedDeque: Deque Interface
// Implementation of the Deque interface. public Object getLast() { return elementAt(count – 1); } public void addLast(Object obj) { addElementAt(obj,count); } public void removeLast() { removeElementAt(count – 1); } public Object getFirst() { return elementAt(0); }
public synchronized void addFirst(Object obj) { if (++count >= data.length) ensureCapacity(2 * data.length); if (--first < 0 ) first = data.length – 1; setElementAt(obj, 0); } public synchronized void removeFirst() { if (count == 0) throw new NoSuchElementException(); data[firstFilled] = null; if(++first == data.length) first = 0; count--; } } // End of IndexedQueue class.
IndexedDeque
the first element