11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

130 Chap. 4 Lists, Stacks, <strong>and</strong> Queues/** Linked queue implement<strong>at</strong>ion */class LQueue implements Queue {priv<strong>at</strong>e Link front; // Pointer to front queue nodepriv<strong>at</strong>e Link rear; // Pointer to rear queuenodeint size;// Number of elements in queue/** Constructors */public LQueue() { init(); }public LQueue(int size) { init(); } // Ignore size/** Initialize queue */priv<strong>at</strong>e void init() {front = rear = new Link(null);size = 0;}/** Reinitialize queue */public void clear() { init(); }/** Put element on rear */public void enqueue(E it) {rear.setNext(new Link(it, null));rear = rear.next();size++;}/** Remove <strong>and</strong> return element from front */public E dequeue() {assert size != 0 : "Queue is empty";E it = front.next().element(); // Store dequeued valuefront.setNext(front.next().next()); // Advance frontif (front.next() == null) rear = front; // Last Objectsize--;return it;// Return Object}/** @return Front element */public E frontValue() {assert size != 0 : "Queue is empty";return front.next().element();}/** @return Queue size */public int length() { return size; }Figure 4.28 Linked queue class implement<strong>at</strong>ion.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!