12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

138 Chap. 4 Lists, Stacks, <strong>and</strong> Queues// Linked queue implementationclass LQueue implements Queue {private Link front; // Pointer <strong>to</strong> front queue nodeprivate Link rear; // Pointer <strong>to</strong> rear queuenodeint size;// Number of elements in queue// Construc<strong>to</strong>rspublic LQueue() { init(); }public LQueue(int size) { init(); } // Ignore sizeprivate void init() {// Initialize queuefront = rear = new Link(null);size = 0;}public void clear() { init(); }// Reinitialize queuepublic void enqueue(E it) { // Put element on rearrear.setNext(new Link(it, null));rear = rear.next();size++;}public E dequeue() {// remove element from frontassert size != 0 : "Queue is empty";E it = front.next().element(); // S<strong>to</strong>re dequeued valuefront.setNext(front.next().next()); // Advance frontif (front.next() == null) rear = front; // Last Objectsize--;return it;// Return Object}public E frontValue() { // Get front elementassert size != 0 : "Queue is empty";return front.next().element();}public int length() { return size; } // Return lengthFigure 4.26 Linked queue class implementation.

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

Saved successfully!

Ooh no, something went wrong!