/
QUEUES  The Idea of a Queue A queue is like a lineup in a bank objects enter at the back QUEUES  The Idea of a Queue A queue is like a lineup in a bank objects enter at the back

QUEUES The Idea of a Queue A queue is like a lineup in a bank objects enter at the back - PDF document

yoshiko-marsland
yoshiko-marsland . @yoshiko-marsland
Follow
539 views
Uploaded On 2015-01-15

QUEUES The Idea of a Queue A queue is like a lineup in a bank objects enter at the back - PPT Presentation

Queues are used in many kinds of software such as Operating systems Queues keep track of processes waiting for a turn in the CPU Simulations In a Laundromat simulation a queue might keep track of loads of laundry waiting for a dryer Graphical user i ID: 31372

Queues are used

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "QUEUES The Idea of a Queue A queue is l..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

QUEUES1 TheIdeaofaQueueAqueueislikealineupinabank:objectsenterattheback,andleavefromthefront.Queuesareusedinmanykindsofsoftware,suchas:Operatingsystems.QueueskeeptrackofprocesseswaitingforaturnintheCPU.Simulations.InaLaundromatsimulation,aqueuemightkeeptrackofloadsoflaundrywaitingforadryer.Graphicaluserinterfaces(GUIs).Queueskeeptrackofeventswaitingtobehandled,likemultiplebuttonclicks.IntheseslidesweshowJavacodeforqueueswithoutthe\generics"providedinthenewerJava1.5.That'sbecausewe'dliketoworkwitharrays,whicharenotsoeasytousewithgenerics.Ingeneral,thatmightnotbetheidealchoice.2 De nitionofaQueueData:Asequenceofobjects.Theobjectsareremovedintheordertheywereinserted;thisisreferredtoas\ rstin, rstout",orFIFO.The rstelement,atthe\front"ofthequeue,iscalledthe\head".Thelastelementiscalledthe\tail".Operations:enqueue(o):Appendotothequeue.head():Returnfrontelementofthequeue.Precondition:Thequeueisnotempty.dequeue():Removeandreturnthefrontelementofthequeue.Precondition:Thequeueisnotempty.size():Returnthenumberofelementsinthequeue.3 AJavaInterfaceRememberthataninterfaceislikeaclass,butwithoutanyinstancevariablesormethodbodies.Itde neswhataclassmustdo,butisnotaclassitself.Weneedtode neaclasslaterthat\implements"theinterface.publicinterfaceQueue{/**Appendotome.*/voidenqueue(Objecto);/**Returnmyfrontelement.Precondition:size()!=0.*/Objecthead();/**Removeandreturnmyfrontelement.Precondition:size()!=0.*/Objectdequeue();/**Returnthenumberofelementsinme.*/intsize();}4 ImplementingQueue/**AQueuewithfixedcapacity.*/publicclassArrayQueueimplementsQueue{/**Thenumberofelementsinme.*/privateintsize;/**contents[0..size-1]containsmyelements.*/privateObject[]contents;//Representationinvariant://size�=0.//Ifsizeis0,Iamempty.//Ifsize�0://contents[0]isthehead//contents[size-1]isthetail//contents[0..size-1]containsthe//Objectsintheordertheywereinserted./**AnArrayQueuewithcapacityfornelements.*/publicArrayQueue(intn){contents=newObject[n];}(Continuedonnextslide)5 /**Appendotome.*/publicvoidenqueue(Objecto){contents[size++]=o;}/**Removeandreturnmyfrontelement.ThisisO(size()).*Precondition:size()!=0.*/publicObjectdequeue(){Objecthead=contents[0];//Movealltheotherelementsuponespot.for(inti=0;i!=size-1;++i){contents[i]=contents[i+1];}--size;returnhead;}/**Returnmyfrontelement.*Precondition:size()!=0.*/publicObjecthead(){returncontents[0];}/**Returnthenumberofelementsinme.*/publicintsize(){returnsize;}}6 QuestionsWhyistheconstructorO(capacity)?Wouldthefollowinghaveworkedindequeue()?for(inti=size-1;i!=0;--i){contents[i-1]=contents[i];}UsingArrayQueueWecouldn'tconstructaQueue,butwecanconstructanArrayQueue.Example(compilesandruns):publicclassAlsoOkay{publicstaticvoidfill(Queueq,intn){for(inti=0;i!=n;i++){q.enqueue(newInteger(i));}}publicstaticvoidmain(String[]args){//CouldalsobedeclaredasArrayQueue.Queueq=newArrayQueue(15);fill(q,15);}}7 QueueimprovementsAJavaArrayListhasoperationsthatletitbeusedlikeaQueue(Exercise:lookupwhichones).WhynotjustuseArrayListallthetime?BecausewecanoptimizeourQueueimplementation(s)forthespeci cQueueoperations.Question:WhatoperationtakesthemosttimeinArrayQueue?8 SomealternativetechniquesDon'tbothershiftingKeepavariablethatsayswheretheheadis(sinceit'sgoingtomovedown).Whatifthequeueeventuallyslidesdowntotheendofthearray?UseacirculararrayAsabove,butwhenthetail(orhead)getstotheend,wraparoundtothebeginning.Thatis,treatthearrayasifitwerecircular.9 Herearetwowaysofdrawingaqueuecontainingabcd:01234567abcdQuestion:Deviseasequenceofoperationsthatwouldleadtothissituation.10 ClassCircularQueue/**AQueuewithfixedcapacityand(exceptforcreation)*constant-timeoperations.*/publicclassCircularQueueimplementsQueue{/**Thenumberofelementsinme.*/privateintsize;/**Theindexoftheheadandtailofthequeue.*/privateinthead,tail;/**Theitemsinme,storedincontents[head..tail],withwraparound.*/privateObject[]contents;//Representationinvariant://Let"capacity"becontents.length.//size�=0.//Ifsizeis0,Iamempty.//Ifsize�0://contents[head]isthehead//contents[tail]isthetail//ifhead=tail,contents[head..tail]contains//theObjectsintheordertheywereinserted;size=tail-head+1.//ifhead.23;础tail,contents[head..capacity-1,0..tail]//containstheObjectsintheordertheywereinserted;//size=tail-head+1+capacity.11 /**ACircularQueuewithcapacityfornelements.*/publicCircularQueue(intn){contents=newObject[n];tail=contents.length-1;}/**Appendotome.*/publicvoidenqueue(Objecto){tail=(tail+1)%contents.length;contents[tail]=o;++size;}/**Removeandreturnmyfrontelement.Precondition:size()!=0.*/publicObjectdequeue(){Objectresult=contents[head];head=(head+1)%contents.length;--size;returnresult;}/**Returnmyfrontelement.Precondition:size()!=0.*/publicObjecthead(){returncontents[head];}/**Returnthenumberofelementsinme.*/publicintsize(){returnsize;}}Question:Whynotcalculatesizeinsteadofstoringit?12