Presentation is loading. Please wait.

Presentation is loading. Please wait.

Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies

Similar presentations


Presentation on theme: "Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies"— Presentation transcript:

1 http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies razdan@asu.edu http://dcst2.east.asu.edu/~razdan/cst230/

2 2 Queue A queue is a first-in-first-out data structure (the “English” word for a line at the bank/grocery store/etc is queue) definition: “A queue is a data structure of ordered items such that items can be inserted only at one end (called the rear) and removed only at the other end (called the front). The item at the front end of the queue is called the first item.”

3 3 Typical queue methods getFront() (often called dequeue) removes and returns the Object at the front of the queue insert( Object ) (often called enqueue) adds the Object at the rear of the queue isEmpty() size()

4 4 Queue variations priority queue circular queue http://cs.smith.edu/~streinu/Teaching/Courses /112/Applets/Queue/myApplet.html double ended queue (supports both stack- like and queue-like behaviors)

5 5 Circular Array Implementation public class ObjectQueue implements Cloneable{ private Object[] data; private int manyItems; private int front; private int rear; public ObjectQueue(){ final int INITIAL_CAPACITY = 10; manyItems = 0; data = new Object[INITIAL_CAPACITY]; }

6 6 public ObjectQueue( int initialCapacity ){ if( initialCapacity < 0 ) throw new IllegalArgumentException ("initialCapacity is nagative: "); manyItems = 0; data = new Object[ initialCapacity ]; }

7 7 public Object clone(){ ObjectQueue answer; try{ answer = (ObjectQueue) super.clone(); } catch( CloneNotSupportedException e ){ throw new RuntimeException (“Class does not implement Cloneable." ); } answer.data = (Object[]) data.clone(); return answer; }

8 8 public void ensureCapacity( int minimumCapacity ){ Object biggerArray[]; int n1, n2; if( data.length >= minimumCapacity ) return; else if( manyItems == 0 ) data = new Object[minimumCapacity]; else if( front <= rear ){ biggerArray = new Object[minimumCapacity]; System.arraycopy(data,front,biggerArray,front,manyItems); data = biggerArray; } else{ biggerArray = new Object[minimumCapacity]; n1 = data.length - front; n2 = rear + 1; System.arraycopy(data, front, biggerArray, 0, n1 ); System.arraycopy( data, 0, biggerArray, n1, n2 ); front = 0; rear = manyItems - 1; data = biggerArray; }

9 9 public int getCapacity(){ return data.length; } public Object getFront(){ Object answer; if( manyItems == 0 ) throw new NoSuchElementException ("Queue underflow."); answer = data[front]; front = nextIndex( front ); manyItems--; return answer; }

10 10 public void insert( Object item ){ if( manyItems == data.length ) ensureCapacity( manyItems * 2 + 1 ); if( manyItems == 0 ){ front = 0; rear = 0; } else rear = nextIndex( rear ); data[rear] = item; manyItems++; }

11 11 public boolean isEmpty(){ return (manyItems == 0 ); } private int nextIndex( int i ){ return( (i + 1) % data.length ) } public int size(){ return manyItems; }

12 12 public void trimToSize(){ if( manyItems != data.length ){ Object newData[] = new Object[manyItems]; int i = front; int count = 0; while( count < manyItems ){ newData[count]=data[i]; count++; i = nextIndex( i ); } front = 0; rear = manyItems-1; data = newData; }

13 13 Time Complexity Stack –isEmpty –Peek –Pop –push Queue –isEmpty –getFront –insert


Download ppt "Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies"

Similar presentations


Ads by Google