Bab-005_Queue

download Bab-005_Queue

of 35

Transcript of Bab-005_Queue

  • 8/7/2019 Bab-005_Queue

    1/35

    Semester Genap 2010/2011

    Beni Suranto, [email protected]

  • 8/7/2019 Bab-005_Queue

    2/35

    8 A-2

    The Abstract Data Type Queue

    A queue

    New items enter at the back, or rear, of the queue

    Items leave from the front of the queue

    First-in, first-out (FIFO) property

    The first item inserted into a queue is the firstitem to leave

    This is the same as waiting in a line. Firstperson in line gets served first.

  • 8/7/2019 Bab-005_Queue

    3/35

    8 A-3

    The Abstract Data Type Queue

    ADT queue operations

    Create an empty queue

    Determine whether a queue is empty

    Add a new item to the queue Remove from the queue the item that was added

    earliest

    Remove all the items from the queue

    Retrieve from the queue the item that was addedearliest

  • 8/7/2019 Bab-005_Queue

    4/35

    8 A-4

    The Abstract Data Type Queue

    Queues

    Are appropriate for many real-worldsituations

    Example: A line to buy a movie ticket Have applications in computer science

    Example: A request to print a document

    A simulation

    A study to see how to reduce the wait involved inan application

  • 8/7/2019 Bab-005_Queue

    5/35

    8 A-5

    The Abstract Data Type Queue

    Pseudocode for the ADT queue operationscreateQueue()

    // Creates an empty queue.

    isEmpty()

    // Determines whether a queue is empty

    enqueue(newItem) throws QueueException

    // Adds newItem at the back of a queue. Throws

    // QueueException if the operation is not

    // successful

  • 8/7/2019 Bab-005_Queue

    6/35

    8 A-6

    The Abstract Data Type Queue

    Pseudocode for the ADT queue operations(Continued)dequeue() throws QueueException

    // Retrieves and removes the front of a queue.

    // Throws QueueException if the operation is

    // not successful.

    dequeueAll()

    // Removes all items from a queue

    peek() throws QueueException

    // Retrieves the front of a queue. Throws

    // QueueException if the retrieval is not

    // successful

  • 8/7/2019 Bab-005_Queue

    7/35

    8 A-7

    Recognizing Palindromes

    A palindrome

    A string of characters that reads the same from leftto right as its does from right to left

    To recognize a palindrome, a queue can beused in conjunction with a stack

    A stack can be used to reverse the order ofoccurrences

    A queue can be used to preserve the order ofoccurrences

  • 8/7/2019 Bab-005_Queue

    8/35

    8 A-8

    Recognizing Palindromes

    A nonrecursiverecognition algorithm forpalindromes

    As you traverse thecharacter string from leftto right, insert eachcharacter into both aqueue and a stack

    Compare the charactersat the front of the queueand the top of the stack

  • 8/7/2019 Bab-005_Queue

    9/35

    Implementations of the ADT

    Queue

    A queue can have either

    An array-based implementation

    A reference-based implementation

  • 8/7/2019 Bab-005_Queue

    10/35

    8 A-10

    A Reference-Based

    Implementation

    Possible implementations of a queue

    A linear linked list with two external references

    A reference to the front

    A reference to the back

  • 8/7/2019 Bab-005_Queue

    11/35

    8 A-11

    A Reference-Based

    Implementation

    Possible implementations of a queue(Continued)

    A circular linked list with one external reference

    A reference to the back

  • 8/7/2019 Bab-005_Queue

    12/35

    8 A-12

    A Reference-Based

    ImplementationInserting an item into a nonempty queue

  • 8/7/2019 Bab-005_Queue

    13/35

    8 A-13

    A Reference-Based

    Implementation

    Inserting an item into an empty queue: a) before insertion; b) after insertion

  • 8/7/2019 Bab-005_Queue

    14/35

    8 A-14

    A Reference-Based

    ImplementationDeleting an item from a queue of more than one item

  • 8/7/2019 Bab-005_Queue

    15/35

    8 A-15

    An Array-Based Implementation

    a) A naive array-based implementation of a queue; b) rightward drift can causethe queue to appear full

  • 8/7/2019 Bab-005_Queue

    16/35

    16

    Array implementation of queues

    Notice how the array contents crawl to the right as elements are insertedand deleted

    This will be a problem after a while!

    17 23 97 44 333After insertion:

    23 97 44 333After deletion:

    rear = 4front = 1

    17 23 97 44Initial queue:

    rear = 3front = 0

  • 8/7/2019 Bab-005_Queue

    17/35

    Circular arrays

    We can treat the array holding the queue elements as circular (joined

    at the ends)

    44 55 11 22 33

    0 1 2 3 4 5 6 7

    myQueue:

    rear = 1 front = 5

    Elements were added to this queue in the order 11, 22, 33, 44, 55,

    and will be removed in the same order

    Use: front = (front + 1) % myQueue.length;

    and: rear = (rear + 1) % myQueue.length;

  • 8/7/2019 Bab-005_Queue

    18/35

    18

    Full and empty queues

    If the queue were to become completely full, it would look like this:

    If we were then to remove all eight elements, making the queue completelyempty, it would look like this:

    44 55 66 77 88 11 22 33

    0 1 2 3 4 5 6 7

    myQueue:

    rear = 4 front = 5

    0 1 2 3 4 5 6 7

    myQueue:

    rear = 4 front = 5

    This is a problem!

  • 8/7/2019 Bab-005_Queue

    19/35

    19

    Full and empty queues: solutions

    Solution #1: Keep an additional variable

    Solution #2: (Slightly more efficient) Keep a gap between elements: consider

    the queue full when it has n-1 elements

    44 55 66 77 88 11 22 33

    0 1 2 3 4 5 6 7

    myQueue:

    rear = 4 front = 5count = 8

    44 55 66 77 11 22 33

    0 1 2 3 4 5 6 7

    myQueue:

    rear = 3 front = 5

  • 8/7/2019 Bab-005_Queue

    20/35

    Enqueueing a node

    17

    Node to be

    enqueued

    To enqueue (add) a node:

    Find the current last node

    Change it to point to the new last node

    Change the last pointer in the list header

    2344

    lastfirst

    97

  • 8/7/2019 Bab-005_Queue

    21/35

    21

    Dequeueing a node

    To dequeue (remove) a node:

    Copy the pointer from the first node into the header

    44 97 23 17

    lastfirst

  • 8/7/2019 Bab-005_Queue

    22/35

    22

    Queue implementation details

    With an array implementation:

    you can have both overflow and underflow

    you should set deleted elements to null

    With a linked-list implementation:

    you can have underflow

    overflow is a global out-of-memory condition

    there is no reason to set deleted elements to null

  • 8/7/2019 Bab-005_Queue

    23/35

    8 A-23

    An Array-Based Implementation

    A circular arrayeliminates theproblem of

    rightward drift

    A circular implementation of a queue

  • 8/7/2019 Bab-005_Queue

    24/35

    8 A-24

    An Array-Based Implementation

    The effect of some operations of the queue in Figure 8-8

  • 8/7/2019 Bab-005_Queue

    25/35

    8 A-25

    An Array-Based Implementation

    A problem with the circular arrayimplementation

    front and back cannot be used to distinguish

    between queue-full and queue-empty conditions

  • 8/7/2019 Bab-005_Queue

    26/35

    8 A-26

    An Array-Based Implementation

    a) front passes back when the queue becomes empty

  • 8/7/2019 Bab-005_Queue

    27/35

    8 A-27

    An Array-Based Implementation

    b) back catches up to front when the queue becomes full

  • 8/7/2019 Bab-005_Queue

    28/35

    8 A-28

    An Array-Based Implementation

    To detect queue-full and queue-emptyconditions

    Keep a count of the queue items

    To initialize the queue, set

    front to 0

    back to MAX_QUEUE 1

    count to 0

  • 8/7/2019 Bab-005_Queue

    29/35

    8 A-29

    So, how do I enqueue and

    dequeue from a circular array-based queue?

  • 8/7/2019 Bab-005_Queue

    30/35

    8 A-30

    An Array-Based Implementation

    Inserting into a queueback = (back+1) % MAX_QUEUE;

    items[back] = newItem;

    ++count;

    Deleting from a queuefront = (front+1) % MAX_QUEUE;

    --count;

  • 8/7/2019 Bab-005_Queue

    31/35

    8 A-31

    Using a List (Vector or ArrayList)

    How do I implement enqueueand dequeue?

  • 8/7/2019 Bab-005_Queue

    32/35

    8 A-32

    An Implementation That Uses the ADT List

    If the item in position 1 of a list list

    represents the front of the queue, the

    following implementations can be used dequeue()

    list.remove(1)

    peek()

    list.get(1)

  • 8/7/2019 Bab-005_Queue

    33/35

  • 8/7/2019 Bab-005_Queue

    34/35

    The Java Collections Framework

    Interface Queue JCF has a queue interface called Queue

    Derived from interface Collection

    Adds methods: element: retrieves, but does not remove head

    offer: inserts element into queue

    peek: retrieves, but does not remove head

    poll: retrieves and removes head

    remove: retrieves and removes head

  • 8/7/2019 Bab-005_Queue

    35/35