Top Banner

of 18

188652_633779747741654665

Apr 03, 2018

Download

Documents

manojmis2010
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/29/2019 188652_633779747741654665

    1/18

    Chapter 8 introduces the

    queue data type.

    Several exampleapplications of queues are

    given in that chapter.

    This presentation describes

    the queue operations and

    two ways to implement a

    queue.

    Using a Queue

    Data Structures

    and Other Objects

    Using C++

  • 7/29/2019 188652_633779747741654665

    2/18

    The Queue Operations

    A queue is like a line

    of people waiting for a

    bank teller. The queuehas a front and a rear.$ $

    Front

    Rear

  • 7/29/2019 188652_633779747741654665

    3/18

    The Queue Operations

    New people must enter the queue at the

    rear. The C++ queue class calls this a

    push, although it is usually called anenqueue operation.

    $ $

    Front

    Rear

  • 7/29/2019 188652_633779747741654665

    4/18

    The Queue Operations

    When an item is taken from the queue,

    it always comes from the front. The

    C++ queue calls this a pop, although itis usually called a dequeue operation.

    $ $

    Front

    Rear

  • 7/29/2019 188652_633779747741654665

    5/18

    The Queue Class

    The C++ standard

    template library has

    a queue templateclass.

    The template

    parameter is the

    type of the items

    that can be put in

    the queue.

    template classqueue

    {public:

    queue( );

    void push(const Item& entry);

    void pop( );bool empty( ) const;

    Item front( ) const;

  • 7/29/2019 188652_633779747741654665

    6/18

    Array Implementation

    A queue can be implemented with an array, as

    shown here. For example, this queue contains the

    integers 4 (at the front), 8 and 6 (at the rear).

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .

    An array of integersto implement a

    queue of integers

    4 8 6

    We don't care what's in

    this part of the array.

  • 7/29/2019 188652_633779747741654665

    7/18

    Array Implementation

    The easiest implementation also keeps

    track of the number of items in the

    queue and the index of the firstelement (at the front of the queue), the

    last element (at the rear).

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .

    4 8 6

    size3

    first0

    last2

  • 7/29/2019 188652_633779747741654665

    8/18

    A Dequeue Operation

    When an element leaves the queue,

    size is decremented, and first changes,

    too.

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .

    4 8 6

    size2

    first1

    last2

  • 7/29/2019 188652_633779747741654665

    9/18

    An Enqueue Operation

    When an element enters the queue,

    size is incremented, and last changes,

    too.

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .

    28 6

    size3

    first1

    last3

  • 7/29/2019 188652_633779747741654665

    10/18

    At the End of the Array

    There is special behavior at the end of

    the array. For example, suppose we

    want to add a new element to thisqueue, where the last index is [5]:

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

    2 16

    size3

    first3

    last5

  • 7/29/2019 188652_633779747741654665

    11/18

    At the End of the Array

    The new element goes at the front of

    the array (if that spot isnt already

    used):

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

    2 16

    size4

    first3

    last0

    4

  • 7/29/2019 188652_633779747741654665

    12/18

    Array Implementation

    Easy to implement

    But it has a limited capacity with a fixed array

    Or you must use a dynamic array for anunbounded capacity

    Special behavior is needed when the rear reachesthe end of the array.

    [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .

    4 8 6

    size3

    first0

    last2

  • 7/29/2019 188652_633779747741654665

    13/18

    Linked List Implementation

    10

    15

    7

    null

    13

    A queue can also be

    implemented with a linked list

    with both a head and a tailpointer.

    head_ptr

    tail_ptr

  • 7/29/2019 188652_633779747741654665

    14/18

    Linked List Implementation

    10

    15

    7

    null

    13

    Which end do you think is the

    front of the queue? Why?

    head_ptr

    tail_ptr

  • 7/29/2019 188652_633779747741654665

    15/18

    Linked List Implementation

    10

    15

    7

    null

    head_ptr

    13

    The head_ptr points to the front

    of the list.

    Because it is harder to remove

    items from the tail of the list.

    tail_ptr

    Front

    Rear

  • 7/29/2019 188652_633779747741654665

    16/18

    Like stacks, queues have many applications.

    Items enter a queue at the rear and leave a

    queue at the front.

    Queues can be implemented using an array

    or using a linked list.

    Summary

  • 7/29/2019 188652_633779747741654665

    17/18

    THE END

    Presentation copyright 2006, Addison Wesley Longman,

    For use withData Structures and Other Objects Using C++

    by Michael Main and Walter Savitch.

    Some artwork in the presentation is used with permission from Presentation Task Force

    (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club

    Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

    Students and instructors who useData Structures and Other Objects Using C++ are welcome

    to use this presentation however they see fit, so long as this copyright notice remains

    intact.

  • 7/29/2019 188652_633779747741654665

    18/18