chap07 - queue data structure

Upload: raffy-ceniza

Post on 06-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Chap07 - Queue Data Structure

    1/42

    Data Structures Using Java 1

    Chapter 7

    Queues

  • 8/3/2019 Chap07 - Queue Data Structure

    2/42

    Data Structures Using Java 2

    Chapter Objectives

    Learn about queues

    Examine various queue operations

    Learn how to implement a queue as an array

    Learn how to implement a queue as a linked list

    Discover priority queues

    Discover queue applications

  • 8/3/2019 Chap07 - Queue Data Structure

    3/42

    Data Structures Using Java 3

    Queues

    Definition: data structure in which the

    elements are added at one end, called the

    rear, and deleted from the other end, calledthe frontor first

    First In First Out (LIFO) data structure

  • 8/3/2019 Chap07 - Queue Data Structure

    4/42

    Data Structures Using Java 4

    Basic Operations on a Queue

    initializeQueue: Initializes the queue to an

    empty state

    isEmptyQueue: Determines whether the

    queue is empty. If the queue is empty, it

    returns the value true; otherwise, it returns

    the value false

  • 8/3/2019 Chap07 - Queue Data Structure

    5/42

    Data Structures Using Java 5

    Basic Operations on a queue

    isFullQueue: Determines whether the queueis full. If the queue is full, it returns the

    value true; otherwise, it returns the valuefalse

    front: Returns the front (first) element of thequeue; the queue must exist

    back: Returns the front (first) element of thequeue; the queue must exist

  • 8/3/2019 Chap07 - Queue Data Structure

    6/42

    Data Structures Using Java 6

    Basic Operations on a queue

    addQueue: Adds a new element to the rear

    of the queue; the queue must exist and must

    not be full

    deleteQueue: Removes the front element of

    the queue; the queue must exist and must

    not be empty

  • 8/3/2019 Chap07 - Queue Data Structure

    7/42

    Data Structures Using Java 7

    Queue Exception Class

    Adding an element to a full queue, and

    removing an element from an empty queue,

    generates errors and exceptions calledqueue overflow and queue underflow

    exception

    Exception classes designed to handle theseexceptions

  • 8/3/2019 Chap07 - Queue Data Structure

    8/42

    Data Structures Using Java 8

    Implementation of Queues as

    Arrays Initially queue is empty; queueFront and queueRear

    point directly to first and last elements of queue

    To implement a queue as an array we need: An array

    The variables queueFront and queueRear to keep track

    of the first and last elements of the queue

    The variable maxQueueSize to specify the maximum

    size of the queue

  • 8/3/2019 Chap07 - Queue Data Structure

    9/42

    Data Structures Using Java 9

    Implementation of Queues as

    Arrays

  • 8/3/2019 Chap07 - Queue Data Structure

    10/42

    Data Structures Using Java 10

    Implementation of Queues as

    Arrays

  • 8/3/2019 Chap07 - Queue Data Structure

    11/42

    Data Structures Using Java 11

    Circular Queue

    Possible problem: If a sequence ofoperations eventually sets index queueRear

    to point to last array position, it gives theimpression that the queue is full.

    However, the queue has only two or threeelements and front of the array is empty(see Figure 7-4).

  • 8/3/2019 Chap07 - Queue Data Structure

    12/42

    Data Structures Using Java 12

    Circular Queue

  • 8/3/2019 Chap07 - Queue Data Structure

    13/42

    Data Structures Using Java 13

    Circular Queue

  • 8/3/2019 Chap07 - Queue Data Structure

    14/42

    Data Structures Using Java 14

    Circular Queue

  • 8/3/2019 Chap07 - Queue Data Structure

    15/42

    Data Structures Using Java 15

    Implementation of Queues as

    ArraysCase 1: Suppose that after certain operations, the array

    containing the queue is as shown below

  • 8/3/2019 Chap07 - Queue Data Structure

    16/42

    Data Structures Using Java 16

    Implementation of Queues as

    ArraysdeleteQueue operation results in an empty queue

  • 8/3/2019 Chap07 - Queue Data Structure

    17/42

    Data Structures Using Java 17

    Implementation of Queues as

    ArraysCase 2: Let us now consider the queue shown below

  • 8/3/2019 Chap07 - Queue Data Structure

    18/42

    Data Structures Using Java 18

    Implementation of Queues as

    ArraysResulting array in Figure 7-11 represents a full queue

  • 8/3/2019 Chap07 - Queue Data Structure

    19/42

    Data Structures Using Java 19

    Full Queue vs. Empty Queue

    Problem: distinguishing between an empty and a

    full queue

    Arrays in Figures 7-9 and 7-11 have identicalvalues for queueFront and queueRear

    Solutions:

    Keep a count

    Let queueFront indicate index of array position

    precedingfirst element of queue, rather than index of

    actual first element itself (see Figure 7-12)

  • 8/3/2019 Chap07 - Queue Data Structure

    20/42

    Data Structures Using Java 20

    UML Diagram of the

    class QueueClass

  • 8/3/2019 Chap07 - Queue Data Structure

    21/42

    Data Structures Using Java 21

    Initialize Queue

    public void initializeQueue()

    {

    for(int i = queueFront; i < queueRear;

    i = (i + 1) %maxQueueSize)

    list[i] = null;

    queueFront = 0;

    queueRear = maxQueueSize - 1;

    count = 0;

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    22/42

    Data Structures Using Java 22

    Empty Queue and Full Queue

    public boolean isEmptyQueue()

    {

    return (count == 0);}

    public boolean isFullQueue()

    {return (count == maxQueueSize);

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    23/42

    Data Structures Using Java 23

    front

    public DataElement front() throws

    QueueUnderflowException

    {

    if(isEmptyQueue())

    throw new QueueUnderflowException();

    DataElement temp = list[queueFront].getCopy();

    return temp;

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    24/42

    Data Structures Using Java 24

    back

    public DataElement back() throws

    QueueUnderflowException

    {

    if(isEmptyQueue())

    throw new QueueUnderflowException();

    DataElement temp = list[queueRear].getCopy();

    return temp;

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    25/42

    Data Structures Using Java 25

    Add Queue

    public void addQueue(DataElement queueElement)

    throws QueueOverflowException

    {

    if(isFullQueue())

    throw new QueueOverflowException();

    queueRear = (queueRear + 1) % maxQueueSize; //use the mod

    //operator to advance queueRear

    //because the array is circular

    count++;

    list[queueRear] = queueElement.getCopy();}

  • 8/3/2019 Chap07 - Queue Data Structure

    26/42

    Data Structures Using Java 26

    Delete Queue

    public void deleteQueue() throws QueueUnderflowException

    {

    if(isEmptyQueue())

    throw new QueueUnderflowException();

    count--;list[queueFront] = null;

    queueFront = (queueFront + 1) % maxQueueSize; //use the mod

    //operator to advance queueFront

    //because the array is circular

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    27/42

    Data Structures Using Java 27

    Constructor

    Constructor

    creates an array of the size specified by the user

    Default value is 100

    Initializes queueFront queueRear to indicate that

    the queue is empty

  • 8/3/2019 Chap07 - Queue Data Structure

    28/42

    Data Structures Using Java 28

    Linked Queue as an ADT

  • 8/3/2019 Chap07 - Queue Data Structure

    29/42

    Data Structures Using Java 29

    Empty and Full Queue

    Queue is empty if queueFront is NULL

    Queue is full only if we run out of memory

  • 8/3/2019 Chap07 - Queue Data Structure

    30/42

    Data Structures Using Java 30

    addQueue

    Adds a new element to the end of the queue

    Access the reference variable queueRear to

    implement addQueue

  • 8/3/2019 Chap07 - Queue Data Structure

    31/42

    Data Structures Using Java 31

    Front, Back, and Delete Queue

    If queue is nonempty:

    operation front returns the first element of the queue

    operation back returns the last element of the queue operation deleteQueue removes the first element of the

    queue

    If queue is empty: method front terminates the program

    method back terminates the program

  • 8/3/2019 Chap07 - Queue Data Structure

    32/42

    Data Structures Using Java 32

    Priority Queue

    FIFO rules of a queue are relaxed

    Customers or jobs with higher priority are

    pushed to front of queue

    To implement:

    use an ordinary linked list, which keeps the

    items in order from the highest to lowest priority

    use a treelike structure

  • 8/3/2019 Chap07 - Queue Data Structure

    33/42

    Data Structures Using Java 33

    Application of Queues

    Simulation: technique in which one system modelsthe behavior of another system; used when it is tooexpensive or dangerous to experiment with real

    systems

    Simulation examples: wind tunnels used to experiment with design of car

    bodies

    flight simulators used to train airline pilots

    Computer simulations: objects being usuallyrepresented as data

  • 8/3/2019 Chap07 - Queue Data Structure

    34/42

    Data Structures Using Java 34

    Theater Problem

    The manager of a local movie theater is hearing complaintsfrom customers about the time they have to wait in line to

    buy tickets. The theater currently has only one cashier.

    Another theater is preparing to open in the neighborhoodand the manager is afraid of losing customers. The manager

    wants to hire enough cashiers so that a customer does nothave to wait too long to buy a ticket, but does not want tohire extra cashiers on a trial basis and potentially waste timeand money.

    One thing that the manager would like to know is the

    average time a customer has to wait for service. The manager wants someone to write a program to simulate

    the behavior of the theater.

  • 8/3/2019 Chap07 - Queue Data Structure

    35/42

    Data Structures Using Java 35

    Queuing System

    Server:object that provides the service

    Customer: object receiving the service

    transaction time: service time; time it takesto serve a customer

    time-driven simulation:clock isimplemented as a counter and the passageof time (e.g. 1 minute) can be implementedby incrementing the counter (by 1)

  • 8/3/2019 Chap07 - Queue Data Structure

    36/42

    Data Structures Using Java 36

    Application of Queues

  • 8/3/2019 Chap07 - Queue Data Structure

    37/42

    Data Structures Using Java 37

    Application of Queues

  • 8/3/2019 Chap07 - Queue Data Structure

    38/42

    Data Structures Using Java 38

    Application of Queues

    iti C t Q

  • 8/3/2019 Chap07 - Queue Data Structure

    39/42

    Data Structures Using Java 39

    waitingCustomerQueueclass WaitingCustomerQueue extends QueueClass

    {

    //default constructor

    public WaitingCustomerQueue(){

    super();

    }

    //constructor with a parameter

    public WaitingCustomerQueue(int size)

    {

    super(size);}

    //copy constructor

    public WaitingCustomerQueue(WaitingCustomerQueue otherQ)

    {

    super(otherQ);

    }

    //Method to increment the waiting time of each

    //customer in the queue by one time unit.//Postcondition: The waiting time of each customer in

    // the queue is incremented by one time unit.

    public void updateWaitingQueue()

    {

    //Definition as given below.

    }

    }

  • 8/3/2019 Chap07 - Queue Data Structure

    40/42

    Data Structures Using Java 40

    Poisson Distribution

    Need to know the number of customers arriving at a given

    time unit and how long it takes to serve each customer.

    Use Poisson distribution from statistics, which saysprobability ofy events occurring at a given time is given by:

    where is the expected value thaty events occur at that time.

  • 8/3/2019 Chap07 - Queue Data Structure

    41/42

    Data Structures Using Java 41

    Chapter Summary

    Queue Data Structure

    Restricted Version of arrays and linked

    list

    Basic operations

    First In First Out (FIFO)

    Queues Implemented as Arrays

  • 8/3/2019 Chap07 - Queue Data Structure

    42/42

    Chapter Summary

    Queues Implemented as Linked Lists

    Priority Queues

    Application of Queues