EE Data Structures and lgorithms http://www.ecs.umass.edu/~polizzi/teaching/ee/ Stacks and Queues III Lecture 8 Prof. Eric Polizzi
Queues overview First In First Out (FIFO) Input D Output Queues allow to get access to the first item inserted. If removed, you get access to the second item that was inserted, and so on. You cannot add/extract in the middle of queues Remark: Queue is the ritish word for Line Examples of queues in your daily life: Lines at the movies, TM, airport security, etc. Queues used to model real world situation: airplanes waiting to take off, data packet waiting to be transmitted over internet, etc. Simple and useful programming tool
Queues overview dding an item at the of the queue insert/ enqueue Removing an item from the of the queue remove/ dequeue ) enqueue (tail) (head) dequeue In contrast to Stack, a queue is open at both end pplications of queues (few examples quietly doing their job in your computer) Printing job management Storing keystroke data as you type at your keyboard (guarantee to stay in order using a queue) Packet forwarding in routers
Queues printing queue.pdf,. pdf,.pdf arrive to printer Operations using a Queue Now printing.pdf.pdf is finished. Now printing.pdf D.pdf comes D Now still printing.pdf D.pdf is finished. Now printing.pdf D.pdf is finished. Now printing D.pdf
Queue implementation One can use an array of objects to implement a queue maxsize- maxsize is the capacity of the queue /font indexes of the item at the / ( at the top makes insertion easier),, are primitive data (int, long, double, etc.) or objects fter D is added (enqueue/insert) fter leaves (dequeue/remove) maxsize- D maxsize- D 5
Queue implementation List of basic operations using a Queue? To do: Test Java applet Queue.html enqueue(item); dequeue(); peek() isempty() isfull() size() Problem: an array has a limited size, once reaches maxsize, what do we do? Example: fter leaves Shift all items to the after each dequeue but... this is too costly! Solution: Wrapped arround circular array maxsize- D 6
Queue implementation ircular array maxsize-... maxsize fter is added (enqueue) maxsize-... maxsize 7
Queues generic array implementation / Object stands for any data types or objects class Queue // variables // constructors // methods private int maxsize; // Queue capacity private int ; // Queue private int ; // Queue private int nitems; //#items in the queue private Object[] array; // array that holds the items public Queue(int maxsize) this.maxsize=maxsize; // set array size array = new Object[maxSize];//create array nitems = ; // no item yet = ; = -; Remark: It is possible to implement queue without the nitems field, but some methods (isempty, isfull, size) become more complex. 8
Queues generic array implementation / class Queue // variables // constructors // methods public boolean isempty() return(nitems==);//true if queue is empty public boolean isfull() return(nitems==maxsize);//true if queue is full public int size() return(nitems);// return curent number of items 9
Stacks generic array implementation / class Queue // variables // constructors // methods public Object peekfront() //peek item if (isempty()) throw new IllegalStateException("Queue is empty"); return array[]; //return item //////////////////////////////////////////////// public Object dequeue() //remove item if (isempty()) throw new IllegalStateException("Queue is empty"); Object temp=array[];//get item =(+)%maxsize; //increment wraparound nitems--; //decrement #items return temp; //////////////////////////////////////////////// public void enqueue(object item) //insert item if (isfull()) throw new IllegalStateException("Queue is full"); if(==maxsize-) =-;//increment wrap ++; array[]= item; //insert item nitems++; //increment #items
Queue Example We consider a Queue composed of int, can you guess the result? class Queuepp public static void main(string[] args) Queue myqueue = new Queue(5); for(int i=; i<=5; i++) myqueue.enqueue(i*); System.out.println(myqueue.dequeue()); myqueue.enqueue(6); System.out.println(myqueue.dequeue()); System.out.println(myqueue.dequeue()); System.out.println(myqueue.peekFront()); System.out.println(myqueue.dequeue()); System.out.println(myqueue.size()); Result is
Queue Example Printing Queue We consider a Queue composed of String, jobs are sent every minute, and it takes minutes to print; What is the status of the queue after minutes? class Queuepp public static void main(string[] args) Queue myqueue = new Queue(6); int time=; //Rq: time= will produce the same result while (time<=) myqueue.enqueue( job +time+.pdf ); if(time%==) myqueue.dequeue(); time++; System.out.println(myqueue.size()+ jobs in queues ); while(!myqueue.isempty()) System.out.println(myqueue.dequeue()); Results 5jobs in queues job6.pdf job7.pdf job8.pdf job9.pdf job.pdf