Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT II Queue.

Similar presentations


Presentation on theme: "UNIT II Queue."— Presentation transcript:

1 UNIT II Queue

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

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?

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

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

6 30/12/2013 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”.) cinemark

7 A Graphic Model of a Queue
front: All items are deleted from this end rear: All new items are added on this end

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

9 Queue ADT 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

10 Queue ADT (cont’d) 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.

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

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(); };

13 Linear Queue Operation
Explain all operations with example

14 Linear queue array implementation
template <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.

15 Circular Queue Overcome inefficient use of space
31/12/2013 Circular Queue Overcome inefficient use of space Think array as a circle then a straight line Front always chase a tail around the array There is truly an overflow Implementation: Increment by modular arithmetic

16 Circular Queue Operations
EMPTY QUEUE [1] [2] [1] [2] J2 J3 [0] [3] [0] [3] J1 [5] [4] [5] [4] front = front = 0 rear = rear = 2

17 FULL QUEUE FULL QUEUE [1] [2] [1] [2] J J3 J J9 [0] [3][0] [3] J J4 J7 J10 J J5 J6 J5 [5] [4] [5] [4] front =0 rear = 4 front =4 rear =2

18 Circular Queue Operations
Explain all operations with example

19 Circular queue array implementation
template <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.

20 #define Max 5 class Queue { int Q[Max],Front,Rear; public: Queue(){Front=0;Rear=-1;} int isFull(); int isEmpty(); void Insert(int); int Remove(); };

21 int Queue::isFull() { if(Rear!=-1&&Front==(Rear+1)%Max) return 1; return 0; } int Queue::isEmpty() if(Rear==-1)

22 void Queue::Insert(int data)
{ if(!isFull()) Rear=(Rear+1)%Max; Q[Rear]=data; } else cout<<"\nQueue Full";

23 int Queue::Remove() { int Val; if(!isEmpty()) Val=Q[Front]; Q[Front]=-1; cout<<"\nRemoved Data: "; Front=(Front+1)%Max; if(Front==Rear+1) { Front=0;Rear=-1;} } else cout<<"\nQueue Empty"; return Val;

24 Assignment 2 Implement circular queue using array and perform following operations on it. (Student Records) i) Add a record ii) Delete a record iii) Checking Empty iv) Checking Underflow v) Checking overflow

25 Linear Queue using Linked List
1/1/2014 2/1/2014 Linear Queue using Linked List Explain all operations with example

26 Linear 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 = tail = NULL; } int empty() { return head == NULL; } void put(Item x) { link t = tail tail = new node(x); if (head == NULL) head = tail; else t->next = tail; } Item get() { Item v = head->item; link t = head->next; delete head; head = t; return v; }

27 Circular Queue using Linked List
Explain all operations with example

28 Circular queue linked-list implementation
template <class Item> class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = NULL; } }; typedef node *link; link head, tail; public: QUEUE(int) { head = NULL; } int empty() const { return head == NULL; } void put(Item x) { link t = tail; tail = new node(x); if (head == NULL) { 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; }

29 Algorithm Analysis enqueue O(1) dequeue O(1) size O(1) isEmpty O(1)
isFull O(1)

30 Priority queue Elements associated with specific ordering Two types
4/1/2014 Elements associated with specific ordering Two types Ascending priority queue Descending priority queue Application Priority scheduling in OS N/W communication

31 Model of Priority Queue

32 Implementation of Priority Queue
template <class Item> class QUEUE { private: struct node { Item item; node* next; node(Item x) { item = x; next = 0; } }; typedef node *link; link Front,Rear; public: QUEUE(int) { Front = Rear = NULL; } int empty() const { return Rear == NULL; } item get() { link T=Front; item patient = Front->item ; Front = Front->next; delete T; return patient; }

33 Implementation of Priority Queue ( continue…)
void put(Item x ) { link nNode = new node(x); if (isEmpty()) Front = Rear = nNode; else { Rear->next = nNode; Rear = nNode;} } };

34

35 cout<<“\nEnter Name”; cin>>Name;
Queue Q[3]; Insert: cout<<“\nEnter Name”; cin>>Name; cout<<“\nPriority : 0: Serious 1: Medium Serious 2: General”; cin>>Pri; Q[Pri].Insert(Name); Remove: for(int i=0; i<3;i++) if(!Q[i].isEmpty()) { cout<<Q[i].Remove(); break; } else cout<<“\n Queue “<< i<<“is Empty\n”;

36 multiqueue More than one queue in a single array or Linked list
eg. Patient Queue in a Hospital Multiple Queue handle by multiple arrays eg. Multiple priority Queue for processes

37 double ended queue It is called as dequeue
(which is verb meaning to “remove element from Queue) Make use both the ends for insertion & deletion of the elements can use it as a Queue & Stack Explain with Example

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53 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)

54 Thank You !


Download ppt "UNIT II Queue."

Similar presentations


Ads by Google