unit ii queue. syllabus contents concept of queue as adt implementation using linked and sequential...

32
UNIT II Queue

Upload: pauline-wheeler

Post on 18-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

UNIT II

Queue

Page 2: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

2

Syllabus Contents

• Concept of queue as ADT• Implementation using linked and sequential

organization. – linear – circular queue

• Concept – multiqueue– double ended queue – priority queue

• Applications of queue

Page 3: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

3

Abstract Data Type

• What does ‘abstract’ mean?• From Latin: to ‘pull out’—the essentials– To defer or hide the details– Abstraction emphasizes essentials and defers the

details, making engineering artifacts easier to use

• I don’t need a mechanic’s understanding of what’s under a car’s hood in order to drive it– What’s the car’s interface?– What’s the implementation?

Page 4: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

4

ADT = properties + operations

• An ADT describes a set of objects sharing the same properties and behaviors – The properties of an ADT are its data (representing the

internal state of each object• double d; -- bits representing exponent & mantissa are

its data or state– The behaviors of an ADT are its operations or

functions (operations on each instance)• sqrt(d) / 2; //operators & functions are its behaviors

• Thus, an ADT couples its data and operations– OOP emphasizes data abstraction

Page 5: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

5

Model for an abstract data type Inside the ADT are two different parts of the model: data structure and operations (public and private).

Page 6: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

6

Definition of a Queue• A queue is a data structure that models/enforces the first-

come first-serve order, or equivalently the first-in first-out (FIFO) order.

• That is, the element that is inserted first into the queue will be the element that will deleted first, and the element that is inserted last is deleted last.

• A waiting line is a good real-life example of a queue. (In fact, the British word for “line” is “queue”.)

Page 7: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

7

A Graphic Model of a Queue

rear:All new items are added on this end

front:All items are deleted from this end

Page 8: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

8

Operations on Queues• Insert(item): (also called enqueue)

– It adds a new item to the rear of the queue• Remove( ): (also called delete or dequeue)

– It deletes the front item of the queue, and returns to the caller. If the queue is already empty, this operation returns NULL

• getfront( ):– Returns the value in the front element of the queue

• getrear( ):– Returns the value in the rear element of the queue

• isEmpty( )– Returns true if the queue has no items

• isFull()– Returns true if the queue is full

• size( )– Returns the number of items in the queue

Page 9: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

9

objects: a finite ordered list with zero or more elements.methods: for all queue Queue, item element, max_ queue_ size positive integer Queue createQ(max_queue_size) ::= create an empty queue whose maximum size is max_queue_size Boolean isFullQ(queue, max_queue_size) ::= if(number of elements in queue == max_queue_size) return TRUE else return FALSE

Queue ADT

Page 10: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

10

Queue Enqueue(queue, item) ::= if (IsFullQ(queue)) queue_full else insert item at rear of queue and return queue Boolean isEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE

Element dequeue(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue.

Queue ADT (cont’d)

Page 11: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

11

Array-based Queue Implementation• As with the array-based stack implementation,

the array is of fixed size– A queue of maximum N elements

• Slightly more complicated– Need to maintain track of both front and rear

Implementation 1

Implementation 2

Page 12: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

12

FIFO queue ADT interface

template <class Item>class QUEUE { private: // Implementation-dependent code public: QUEUE(int); int empty(); void put(Item); Item get(); int full(); };

Page 13: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

13

Linear Queue Operation

• Explain all operations with example

Page 14: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

14

Linear queue array implementationtemplate <class Item>class QUEUE { private: Item *q; int N, front, rear; public: QUEUE(int maxN) { q = new Item[maxN+1]; N = maxN+1; front = 0; rear = 0; } int empty() const { return front % N == rear; } void put(Item item) { q[rear++] = item; rear = rear % N; } Item get() { front = front % N; return q[front++]; } int full() const { return rear == maxN; } };

If front = rear, then empty; if put would make them equal, then full. Array is 1 larger to allow checks.

Page 15: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

15

EMPTY QUEUE[1] [2] [1] [2]

[0] [3] [0] [3]

[5] [4] [5] [4]

front = 0 front = 0 rear = 0 rear = 2

J2

J1

J3

Circular Queue Operations

Page 16: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

16

FULL QUEUE FULL QUEUE

[1] [2] [1] [2]

[0] [3][0] [3]

[5] [4] [5] [4]

front =0rear = 4

front =4rear =2

J2 J3

J1 J4

J5 J6 J5

J7

J8 J9

J6

J10

Page 17: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

17

Circular Queue Operations

• Explain all operations with example

Page 18: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

18

Circular queue array implementationtemplate <class Item>class QUEUE { private: Item *q; int N, front, rear; public: QUEUE(int maxN) { q = new Item[maxN]; N = maxN; front = maxN; rear = 0; } int empty() const { return front % N == rear; } void put(Item item) { q[rear++] = item; rear = rear % N; } Item get() { front = front % N; return q[front++]; } int full() const { return (rear+1)%N == front; }

};If front = rear, then empty; if put would make them equal, then full. Array is 1 larger to allow checks.

Page 19: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

19

Linear Queue using Linked List

• Explain all operations with example

Page 20: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

20

Linear queue linked-list implementationtemplate <class Item>class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link head, tail; public: QUEUE(int) { head = 0; } int empty() const { return head == 0; } void put(Item x) { link t = tail; tail = new node(x); if (head == 0) head = tail; else t->next = tail; } Item get() { Item v = head->item; link t = head->next; delete head; head = t; return v; } };

Page 21: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

21

Circular Queue using Linked List

• Explain all operations with example

Page 22: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

22

Circular queue linked-list implementation

template <class Item>class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link head, tail; public: QUEUE(int) { head = 0; } int empty() const { return head == 0; } void put(Item x) { link t = tail; tail = new node(x); if (head == 0) { head = tail; head->next=head;} else { t->next = tail; tail->next = head; } } Item get() { Item v = head->item; link t = head->next; delete head; head = t; tail->next = head; return v; } };

Page 23: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

23

Algorithm Analysis

• enqueue O(1)• dequeue O(1)• size O(1)• isEmpty O(1)• isFull O(1)

Page 24: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

24

multiqueue

More than one queue in a single array or Linked listeg. Patient Queue in a Hospital

Multiple Queue handle by multiple arrays eg. Multiple priority Queue for processes

Page 25: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

25

Page 26: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

26

double ended queueIt is called as dequeue (which is verb meaning to “remove element from Queue)Make use both the ends for insertion & deletion of the elementscan use it as a Queue & Stack

Explain with Example

Page 27: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

27

Priority queue

Elements associated with specific ordering Two types

⁻ Ascending priority queue⁻ Descending priority queue

Application⁻ Priority scheduling in OS⁻ N/W communication

Page 28: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

28

Model of Priority Queue

Page 29: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

29

Implementation of Priority Queuetemplate <class Item>class QUEUE { private: struct node { Item item; int priority; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link head; public: QUEUE(int) { head = 0; } int empty() const { return head == 0; } item get() { item patient = head->item ; head = head->next; return patient; }

Page 30: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

30

Implementation of Priority Queue ( continue…)

void put(Item x int p) { link temp , prev , nPatient = new node(x); nPatient->priority = p; if (head == 0) head = nPatient; else { temp = prev = head; while( temp->priority >= p & temp != 0) { prev = temp; temp = temp->next;} if( temp == head ) { nPatient->next = head ; head = nPatient; } else { prev->next = nPatient; nPatient->next = temp; } } };

Page 31: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

31

Queues Applications

• An electronic mailbox is a queue– The ordering is chronological (by arrival time)

• A waiting line in a store, at a service counter, on a one-lane road, Patient in a Hospital

• Applications related to Computer Science– Threads– Job scheduling (e.g. Round-Robin algorithm for

CPU allocation)

Page 32: UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –

32

Thank You !