Presentation is loading. Please wait.

Presentation is loading. Please wait.

EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.

Similar presentations


Presentation on theme: "EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at."— Presentation transcript:

1 EC-211 DATA STRUCTURES LECTURE 9

2 Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at one end (Rear). – Elements are removed from the other end (Front). The element added first is also removed first (FIFO: First In, First Out).

3 Example ABC Front Rear BC Front Rear ABC Front Rear DE (a) Initial Queue (b) After removing 1 element (c) Inserting 2 more elements

4 Applications of the ADT Queue Print Queue Job Scheduling Event Simulation

5 Basic Operations Insert – inserts item at the rear of queue. Remove – deletes the front element of the queue – Attempt to remove an element from an empty queue is called an underflow. Empty – returns false or true depending on whether or not the queue contains any elements

6 Queue Implementation: Dynamic Insert: same logic as insertAtEnd of linked list Remove: same logic as removeFromstart of linked list

7 Queue Implementation: Static //Declaring a Queue Data-type: # define MAX 100 struct queue { int items[MAX]; int Front; int Rear; int Count; };

8 Queue Implementation (static) void initialize () { Front=0; Rear=MAX-1; Count=0; } bool empty () { return (Count == 0); }

9 Queue Implementation (static) void insert(int NewItem) { if (Count == MAX) { cout<<“queue overflow”; exit(1) ; } Rear = (Rear+1) % MAX; Items[Rear] = NewItem; ++(Count); }

10 Queue Implementation (static) int remove () { if (empty()) { cout <<“queue underflow”; exit(1); } int x= Items[Front]; Front = (Front+1) % MAX; --(Count); return x; }

11 Example: Reading a String of Characters When You Enter Characters at a Keyboard, the System Must Retain Them in the Order in Which you Typed Them. It Could Use a Queue for This Purpose.

12 Example: Reading a String of Characters //read a string of characters from a single line of input into a queue queue Q; while ( not end of line) { Read a new character Ch insert( &q, Ch) } Once the characters are in a queue, the system can process them as necessary

13 Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. able was I ere I saw elba We will read the line of text into both a stack and a queue. Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

14 Example: recognizing palindromes

15 IsPal(Str) //Determines whether the string Str is a palindrome. //initialize a queue and a stack queue Q; stack S; //insert each character of the string into both the queue //and the stack for( I=0 through strlen(Str)-1) {NextChar = Str[ I ] insert (&Q, NextChar) push(&S, NextChar) }//end for //compare the queue characters with the stack characters CharactersAreEqual = TRUE cout << "Enter string: " << endl; while(cin.peek() != '\\n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); }

16 Example: recognizing palindromes while( Q is not empty and CharactersAreEqual) { if ( remove(&Q) equals pop(&S)) continue; else CharacterAreEqual = FALSE }//end while return CharactersAreEqual

17 Priority Queue Is a Value-Oriented ADT and is More Sophisticated Than Position-Oriented ADTs Like Stacks and Queues Priority Value Indicates, For Example a Task’s Priority for Completion A Priority Queue can be Viewed as a Sequence of Values Which are Sorted According to Priority An Element of Higher Priority is Processed (removed from queue) Before any Element of Lower Priority Two Elements With the Same Priority are Processed (removed from queue) According to the Order in Which They Were Added to the Queue

18 Priority Queue Implementations –Sorted Array Difficult to Insert Value Very Easy to Find next Value to remove –Unsorted Array Easy to Insert Value Difficult to Find next Value to remove


Download ppt "EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at."

Similar presentations


Ads by Google