queue in data structure

30
QUEUE Data Structures

Upload: muhazzab-chouhadry

Post on 21-Mar-2017

41 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Queue in Data Structure

QUEUE Data Structures

Page 2: Queue in Data Structure

Introduction A Data Structure in which data can only

be inserted at one end (the rear), and deleted from the other end (the front)

These restrictions ensure that data is processed through queue in the order in which they are received (FIFO).

Page 3: Queue in Data Structure

Usage Scenarios

Page 4: Queue in Data Structure

Bus Stop Queue

Remove a person from the queue

Bus Stop

frontrear

rear rear rear rear

Page 5: Queue in Data Structure

Bus Stop Queue

Bus Stop

frontrear

rear rear

Page 6: Queue in Data Structure

Bus Stop Queue

Bus Stop

frontrear

rear

Page 7: Queue in Data Structure

Bus Stop Queue

Add a person to the queue A queue is a FIFO (First-In, First-Out) list.

Bus Stop

frontrear

rear

Page 8: Queue in Data Structure

A Print Queue Print queue: printing is much slower than

the process of selecting pages to print and so a queue is used

Page 9: Queue in Data Structure

The Unsuitability of a Print Stack Stacks are last-in, first-out (LIFO) The most recently selected document

would be the next to print Unless the printer stack is empty, your

print job may never get executed if others are issuing print jobs.

Page 10: Queue in Data Structure

 

Program #1

Program #2front

rear

Send a message to the queue

Take a message fromthe queue

Message queues in an operating system

There are times that programs need to communicate with each other. Unix operating system provides message queue as one of the mechanisms to facilitate communication.

Page 11: Queue in Data Structure

Example: Web spider

Search engines (Altavista, Google, etc) uses programs called spiders to discover new pages on the web.

Input = some particular seed page Repeatedly: select a previously-discovered

page, and traverse one of its hyperlinks.To ensure broad coverage of the entire web,spiders employ a queue data-structureto store & select discovered pages next page to

be explored

newly discoveredpages

Page 12: Queue in Data Structure

Basic Queue Operations

Enqueue: Insertion of an element at rear of queue

Dequeue: deletion of an element at the front of queue

Queue front: examines the element at front of queue

Queue rear: examines element at rear of queue

Page 13: Queue in Data Structure

Basic Queue Operations:UnderflowOverflow

Page 14: Queue in Data Structure
Page 15: Queue in Data Structure

Exercise What would be the contents of queue Q after the following

code is executed and the following data are entered?

Q = createQueueloop (not end of file)

read numberif (number not 0)

enqueue(Q, number)else

dequeue(Q)end if

end loop

Data: 5, 7, 12, 4, 0, 4, 8, 67, 23, 0, 44, 33, 6, 0

Page 16: Queue in Data Structure

Queue Implementation Issues Consider a queue implemented in an

array of size 10 After 5 enqueue operations, the shaded

portion of the array has been filled:

0 1 2 9

front rear

Page 17: Queue in Data Structure

Queue Implementation Issues After two dequeue operations, if array

contents are not shifted, we have:0 1 2 9

front rear

• The queue capacity is now reduced to 8 items

Page 18: Queue in Data Structure

Queue Implementation Issues Option 1 using an underlying array:

enhance the enqueue operation to increase the size of the underlying array whenever there is an item in the final array location: dequeue is a fast operation; enqueue is fast

except when the array needs replacing Could use a large amount of space: once an

item is removed, its array location is never reused

Page 19: Queue in Data Structure

Queue Implementation Issues Option 2

Shift all the remaining items at each dequeue operation

Page 20: Queue in Data Structure

Circular Queues We can implement fixed-capacity queues

using arrays so that the queue capacity never decreases: use circular queues

Concept can be extended so that the underlying array can be replaced by a larger array if necessary

Page 21: Queue in Data Structure

Circular Queues

Treat the underlying array as if it is circular: in an n-element array, the location “before” index 0 is index n-1; the location “after” index n-1 is index 0

10

1211

10

• Use modular arithmetic to compute next and previous indices

Page 22: Queue in Data Structure

Circular Queues1

0

1211

10

10

1211

10

10

1211

10

After 7 enqueues

front

rear

After 5 dequeues

front

rear

After 8 more enqueues

front

rear

Page 23: Queue in Data Structure

Array Based Queue Implementation template <class Qtype> class Queue { Qtype queue[SIZE]; int front, rear; public: Queue() { front = rear = -1; } void enqueue(Qtype num); Qtype dequeue(); bool isfull(); Qtype isempty(); };

Page 24: Queue in Data Structure

isfull template <class Qtype> bool

Queue<Qtype>::isfull() { if(rear+1==front || (rear+1==SIZE &&

front==0)) { return true; } else{ return false; } }

Page 25: Queue in Data Structure

isempty template <class Qtype> bool

Queue<Qtype>::isempty() { if(rear==-1) return true; } else{ return false; } }

Page 26: Queue in Data Structure

enqueue template <class Qtype> void

Queue<Qtype>::enqueue(Qtype num) { if(isfull()) { cout << "Queue is full.\n"; } else{ if(front==-1){ front++; } rear = (rear + 1) % SIZE; queue[rear] = item; } }

Page 27: Queue in Data Structure

dequeue template <class Qtype> Qtype Queue<Qtype>::dequeue() { if(isempty()) { cout << "Queue is empty.\n"; return -999; } else{ data=queue[front]; if(front==rear){ read=front=-1; } else{ front = (front + 1) % SIZE; } return data; } }

Page 28: Queue in Data Structure

Priority Queues Works like an ordinary queue. However, the order of how things are

removed depend on a priority key. Ascending Priority Queue – The item with

the smallest key has the highest priority. Descending Priority Queue – The item

with the largest key has the highest priority.

They will be covered after later.

Page 29: Queue in Data Structure

Queues vs. Stacks Stacks are a LIFO container => store data in the

reverse of order received

Queues are a FIFO container => store data in the order received

Stacks then suggest applications where some sort of reversal or unwinding is desired.

From an ordering perspective, then, Queue are the “opposite” of stacks

Easy solution to the palindrome problem

Page 30: Queue in Data Structure

End