21
Introduction To Algorithm

Queue

Embed Size (px)

DESCRIPTION

Its Presentation of Queue(Programming). May it will help u :)

Citation preview

Page 1: Queue

Introduction To Algorithm

Page 2: Queue

Abstract Data Type

Stack & Queue is an example of ADT An array is not ADT.

Page 3: Queue

Using a Queue

Several example applications of queues are given in that Slides. This presentation describes the queue operations and two ways to implement a queue.

Data Structuresand Other ObjectsUsing C++

Page 4: Queue

What is Queue?

A QUEUE IS A CONTAINER IN WHICH INSERTIONS ARE MADE ONLY AT THE BACK DELETIONS, RETRIEVALS, AND MODIFICATIONS ARE MADE ONLY AT THE FRONT.

Page 5: Queue

The Queue Operations

A queue is like a line of people waiting for a bank teller. The queue has a front and a rear.

$ $

FrontRear

Page 6: Queue

EnQueue Operations

New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation.

$ $

FrontRear

Page 7: Queue

DeQueue Operations

When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation.

$ $

FrontRear

Page 8: Queue

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 ][ 0 ] [1][1] [ 2 ][ 2 ] [ 3 ][ 3 ] [ 4 ][ 4 ] [ 5 ][ 5 ] . . .. . .

An array of integers to An array of integers to implement a queue of implement a queue of integersintegers

4 8 6

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

Page 9: Queue

Array Implementation

The efficient implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear).

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

4 8 6

sizesize3

firstfirst0

lastlast2

Page 10: Queue

A Dequeue Operation

When an element leaves the queue, size is decremented, and first changes, too.

sizesize2

firstfirst1

lastlast2

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

4 8 6

Page 11: Queue

An Enqueue Operation

When an element enters the queue, size is incremented, and last changes, too.

sizesize3

firstfirst1

lastlast3

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

28 6

Page 12: Queue

At the End of the Array

There is special behaviour at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]:

2

sizesize3

firstfirst3

lastlast5

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

165

Page 13: Queue

At the End of the Array

The new element goes at the front of the array (if that spot isn’t already used):

sizesize4

firstfirst3

lastlast0

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

2 164

Page 14: Queue

Array Queue Reviews

Easy to implement. But it has a limited capacity with a fixed array Special behaviour is needed when the rear

reaches the end of the array.

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

4 8 6

sizesize3

firstfirst0

lastlast2

Page 15: Queue

Linked List Implementation

A queue can also be implemented with a linked list with both a head and a tail pointer.

null

10

15

7

13

Head-ptr

Tail-ptr

Page 16: Queue

Linked List Implementation

Which end do you think is the front of the queue? Why ?

10

15

7

13

Head-ptr

Tail-ptr

null

Page 17: Queue

Linked List Implementation

The head-ptr points to the front of the list.

Because it is harder to remove items from the tail of the list.

Rear

Front

10

15

7

13

Head-ptr

Tail-ptr

null

Page 18: Queue

template <class Item>class queue<Item>{public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; }

The C++ standard template library has a queue template class.

The template parameter is the type of the items that can be put in the queue.

The Queue Class

Page 19: Queue

Summary

Queues have many applications.Queues have many applications. Items enter a queue at the rear and Items enter a queue at the rear and

leave a queue at the front.leave a queue at the front. Queues can be implemented using an Queues can be implemented using an

array or using a linked list.array or using a linked list.

Page 20: Queue

There's a queue of questions that you've asked in exam, all waiting for you to accept answers for them.

Waiting in a queue in any bank for paying bills, depositing cash OR cheques.

Daily Life Examples

Page 21: Queue

THE END

Thank You