2B-Queues - 2 Stack and Queue Part 2 Queue Data Structures...

Info icon This preview shows pages 1–8. Sign up to view the full content.

Data Structures and Algorithms in Java 1/21 2. Stack and Queue Part 2: Queue
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 2/21 Queues Objectives Queues Priority Queues Queue interface in java.util
Image of page 2
Data Structures and Algorithms in Java 3/21 What is a queue? 3 A queue is a waiting line that grows by adding elements to its end and shrinks by taking elements from its front A queue is a structure in which both ends are used: One for adding new elements One for removing them A queue is an FIFO structure: F irst I n/ F irst O ut
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 4/21 Operations on a queue 4 The following operations are needed to properly manage a queue: clear() — Clear the queue isEmpty() — Check to see if the queue is empty enqueue(el) — Put the element el at the end of the queue dequeue() — Take the first element from the queue front() — Return the first element in the queue without removing it Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException sometimes the peek() is named instead of front().
Image of page 4
Data Structures and Algorithms in Java 5/21 5 Queue example Operation Output enqueue( 5 ) ( 5 ) enqueue( 3 ) ( 5 , 3 ) dequeue() 5 ( 3 ) enqueue( 7 ) ( 3 , 7 ) dequeue() 3 ( 7 ) front() 7 ( 7 ) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue( 9 ) ( 9 ) enqueue( 7 ) ( 9 , 7 ) size( ) 2 ( 9 , 7 ) enqueue( 3 ) ( 9 , 7 , 3 ) enqueue( 5 ) ( 9 , 7 , 3 , 5 ) dequeue() 9 ( 7 , 3 , 5 )
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Data Structures and Algorithms in Java 6/21 Applications of Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures
Image of page 6
Data Structures and Algorithms in Java 7/21 Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q , by repeatedly performing the following steps: 1. e = Q. dequeue() 2. Service element e 3. Q. enqueue( e ) The Queue Shared Service 1. Deque the next element 3. Enqueue the serviced element 2. Service the next element Round-robin (RR) is one of the simplest scheduling algorithms for processes in an operating system, which assigns time slices to each process in equal portions and in circular order, handling all processes without priority. Round-robin scheduling is both simple and easy to implement, and starvation-free. Round-robin scheduling can also be applied to other scheduling problems, such as data packet scheduling in computer networks.
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern