@Override publicvoidprintDeque() { ListNode<T> current = sentinel.next; while (current != sentinel) { System.out.print(current.val + " "); current = current.next; } System.out.println(); }
@Override public T removeFirst() { if (size == 0) { returnnull; } TremoveValue= sentinel.next.val; sentinel.next = sentinel.next.next; sentinel.next.prev = sentinel; size -= 1; return removeValue; }
@Override public T removeLast() { if (size == 0) { returnnull; } TremoveValue= sentinel.prev.val; sentinel.prev = sentinel.prev.prev; sentinel.prev.next = sentinel; size -= 1; return removeValue; }
@Override public T get(int index) { ListNode<T> current = sentinel.next; for (inti=0; i < index; i++) { current = current.next; } return current.val; }
public T getRecursive(int index) { return getRecursiveHelper(sentinel.next, index); }
private T getRecursiveHelper(ListNode<T> node, int index) { if (index == 0) { return node.val; } else { return getRecursiveHelper(node.next, index - 1); } }
/** * @author Moiads */ //Note: This file will not compile until you complete the Deque implementations publicclassGuitarString { /** * Constants. Do not change. In case you're curious, the keyword final * means the values cannot be changed at runtime. We'll discuss this and * other topics in lecture on Friday. */ // Sampling Rate privatestaticfinalintSR=44100; // energy decay factor privatestaticfinaldoubleDECAY=.996;
/* Buffer for storing sound data. */
privatefinal Deque<Double> buffer;
/* Create a guitar string of the given frequency. */ publicGuitarString(double frequency) { intcapacity= (int) Math.round(SR / frequency); buffer = newArrayDeque<>(); for (inti=0; i < capacity; i++) { buffer.addFirst(0.0); } }
/* Pluck the guitar string by replacing the buffer with white noise. */ publicvoidpluck() { for (inti=0; i < buffer.size(); i++) { buffer.removeFirst(); doubler= Math.random() - 0.5; buffer.addLast(r); } }
/* Advance the simulation one time step by performing one iteration of * the Karplus-Strong algorithm. */ publicvoidtic() { doubleresult= buffer.removeFirst(); result = 0.5 * (result + buffer.get(0)) * DECAY; buffer.addLast(result); }
/* Return the double at the front of the buffer. */ publicdoublesample() { return buffer.get(0); } }