EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or...

18
EC-211 DATA STRUCTURES LECTURE 9

Transcript of EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or...

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 one end (Rear). – Elements are removed from the other end (Front).

• The element added first is also removed first (FIFO: First In, First Out).

Example

A B C

Front

Rear

B C

Front

Rear

A B C

Front

Rear

D E

(a) Initial Queue (b) After removing 1 element

(c) Inserting 2 more elements

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

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

Queue Implementation: Dynamic

• Insert: same logic as insertAtEnd of linked list• Remove: same logic as removeFromstart of

linked list

Queue Implementation: Static//Declaring a Queue Data-type:

# define MAX 100

struct queue

{

int items[MAX];

int Front;

int Rear;

int Count;

};

Queue Implementation (static)

void initialize ()

{

Front=0;

Rear=MAX-1;

Count=0;

}

bool empty ()

{

return (Count == 0);

}

Queue Implementation (static)

void insert(int NewItem)

{

if (Count == MAX)

{

cout<<“queue overflow”;

exit(1) ;

}

Rear = (Rear+1) % MAX;

Items[Rear] = NewItem;

++(Count);

}

Queue Implementation (static)

int remove ()

{

if (empty())

{

cout <<“queue underflow”;

exit(1);

}

int x= Items[Front];

Front = (Front+1) % MAX;

--(Count);

return x;

}

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.

Example: Reading a String of Characters

//read a string of characters from a single line of input into a queuequeue 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

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.

Example: recognizing palindromes

Example: recognizing palindromesIsPal(Str)//Determines whether the string Str is a palindrome.

//initialize a queue and a stackqueue Q;stack S;//insert each character of the string into both the queue //and the stackfor( I=0 through strlen(Str)-1){ NextChar = Str[ I ]

insert (&Q, NextChar)push(&S, NextChar)

} //end for//compare the queue characters with the stack charactersCharactersAreEqual = 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)); } }

Example: recognizing palindromeswhile( Q is not empty and CharactersAreEqual){

if ( remove(&Q) equals pop(&S))continue;

elseCharacterAreEqual = FALSE

} //end whilereturn CharactersAreEqual

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

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