• No results found

Course Objectives. At the end of the lesson students are expected to be able to: Understand queue concepts and applications.

N/A
N/A
Protected

Academic year: 2022

Share "Course Objectives. At the end of the lesson students are expected to be able to: Understand queue concepts and applications."

Copied!
73
0
0

Loading.... (view fulltext now)

Full text

(1)

QUEUE

(2)

Course Objectives

At the end of the lesson students are expected to be able to:

• Understand queue concepts and applications.

• Understand queue structure and operations that can be done on queue.

• Understand and know how to implement queue using

array and linked list : linear array, circular array, linear

link list and circular list.

(3)

1.0 Introduction to Queue

(4)

Introduction to Queue

• New items enter at the back, or rear, of the queue

• Items leave from the front of the queue

• First-in, first-out (FIFO) property

– The first item inserted into a queue is the first item to leave

– Middle elements are logically inaccessible

(5)

Introduction to Queue

• Important in simulation & analyzing the

behavior of complex systems

(6)

Queue Applications

• Real-World Applications

– Cashier lines in any store – Check out at a bookstore – Bank / ATM

– Call an airline

(7)

Queue Applications

• Computer Science Applications

– Print lines of a document

– Printer sharing between computers – Recognizing palindromes

– Shared resource usage (CPU, memory

access, …)

(8)

Queue Applications

• Simulation

– A study to see how to reduce the wait

involved in an application

(9)

Queue implementation

Add/

Enqueue Remove/

Dequeue

Back/Rear Front/Head

A B C

Basic Structure of a Queue:

•Data structure that hold the queue

•head

•rear

(10)

Queue implementation

Add/

Enqueue

Head Rear

A B C D

Insert D into Queue (enQueue) : D is inserted at rear

Head Rear

B C D

Remove/

Dequeue A

Delete from Queue (deQueue) : A is removed

(11)

Queue operations

• Queue operations

– Create an empty queue – Destroy a queue

– Determine whether a queue is full

– Add a new item to the queue (enQueue) – Determine whether a queue is empty

– Remove the item that was added earliest(deQueue) – Retrieve at Front(getFront)

– Retrieve at Back the item that was added

earliest(getRear)

(12)

Queue Implementation

Implementation:

– Array-based (Linear or Circular) – Pointer-based : Link list (Linear or

Circular)

(13)

2.0 Queue Implementation Using

Array(Linear)

(14)

Queue Implementation Using Array(Linear)

• Number of elements in Queue are fixed during declaration.

• Need isFull() operation to determine

whether a queue is full or not.

(15)

Queue Implementation Using Array(Linear)

• Queue structure need at least 3 elements:

1) Element to store items in Queue

2) Element to store index at head

3) Element to store index at rear

(16)

Create Queue Operation

• Declare

– front & back are indexes in the array – Initial condition: front =0 & back = -1 – Size of an array in queue

0 1 2 3 Max size

Queue 0

front

-1 back

(17)

Create Queue operation

Example Code 1

#include <iostream>

using namespace std;

#define max 5

int front = 0, back = -1;

char item[max], newitem;

Create Queue

0 1 2 3 4

item

0 front

-1 back

Continue…

Front refer to index 0

(18)

enQueue operation

void enQueue(){

cout<<"\n\t#################\n";

cout<<"\n\t1. enQueue\n";

//check queue is full if(back == max - 1){

cout<<"\n\tQueue Is Full, Cannot Add Item In Queue\n";

}else{

cout<<"\n\t\tEnter Item:";

cin>>newitem;

back++;

item[back]=newitem;

cout<<endl;

} }

enQueue

Continue…

0 front

0 back

0 1 2 3 4

A

item

back = -1+1 back = 0 From back/rear

item[back] = newitem

back++

Front refer to index 0

(19)

enQueue operation

Continue…

0 front

1 back

0 1 2 3 4

A B

item

back = 0 +1 back = 1 From back/rear

item[back] = newitem

0 front

2 back

0 1 2 3 4

A B C

item

back = 1 +1 back = 2 From back/rear

item[back] = newitem

back++

back++

Front refer to index 0 Front refer to index 0

(20)

enQueue operation

Continue…

0 front

3 back

0 1 2 3 4

A B C D

item

back = 2 +1 back = 3 From back/rear

item[back] = newitem

0 front

4 back

0 1 2 3 4

A B C D E

item

back = 3 +1 back = 4 From back/rear

item[back] = newitem back++

back++

Front refer to index 0

Front refer to index 0

(21)

deQueue operation

void deQueue(){

cout<<"\n\t#################\n";

cout<<"\n\t2.deQueue\n";

if(back < front){

cout<<"\n\tThere is no data to remove from queue\n";

}else{

char itemdeleted;

itemdeleted=item[front];

item[front] = NULL;

cout<<"\n\tItem Remove From Queue:"<<itemdeleted<<endl;

front++;

}

cout<<endl;

}

deQueue

Continue…

0 front

4 back

0 1 2 3 4

A B C D E

item

back = 3 + 1 back = 4 itemdeleted = item[front]

front = 0

From front/head item[front] = NULL Front refer to index 0

(22)

1 front

4 back

0 1 2 3 4

NULL B C D E

item

back = 3 + 1 back = 4 front = 0 +

1

front = 1 front++

Continue…

1 front

4 back

0 1 2 3 4

NULL B C D E

item

back = 3 + 1 back = 4 itemdeleted = item[front]

front = 1 From front/head

item[front] = NULL

2 front

4 back

0 1 2 3 4

NULL NULL C D E

item

back = 3 + 1 back = 4 front = 1 +

1

front = 2 front++

Front refer to index 1

Front refer to index 1

Front refer to index 2

deQueue operation

(23)

Continue…

2 front

4 back

0 1 2 3 4

NULL NULL C D E

item

back = 3 + 1 back = 4

itemdeleted = item[front]

front = 2

From front/head item[front] = NULL

3 front

4 back

0 1 2 3 4

NULL NULL NULL D E

item

back = 3 + 1 back = 4 front = 2 +

1

front = 3 front++

Front refer to index 2

Front refer to index 3

deQueue operation

(24)

Continue…

3 front

4 back

0 1 2 3 4

NULL NULL NULL D E

item

back = 3 + 1 back = 4

itemdeleted = item[front]

front = 3

From front/head item[front] = NULL

4 front

4 back

0 1 2 3 4

NULL NULL NULL NULL E

item

back = 3 + 1 back = 4 front = 3 +

1

front = 4 front++

Front refer to index 3

Front refer to index 4

deQueue operation

(25)

Continue…

4 front

4 back

0 1 2 3 4

NULL NULL NULL NULL E

item

back = 3 + 1 back = 4

itemdeleted = item[front]

front = 4

From front/head item[front] = NULL

5 front

4 back

0 1 2 3 4

NULL NULL NULL NULL NULL

item

back = 3 + 1 back = 4 front = 4 +

1

front = 5 front++

Front refer to index 4

deQueue operation

(26)

Retrieve at front(getFront) operation

void getFront(){

cout<<"\n\t#################\n";

cout<<"\n\t3.getFront\n";

if(back < front){

cout<<"\n\tThere is no data to at front\n";

}else{

cout<<"\n\tItem At Front:"<<item[front]<<endl;

} }

Continue…

(27)

Retrieve at back(getRear) operation

void getRear(){

cout<<"\n\t#################\n";

cout<<"\n\t4.getRear\n";

if(back < front){

cout<<"\n\tThere is no data to at rear\n";

}else{

cout<<"\n\tItem At Rear:"<<item[back]<<endl;

} }

Continue…

(28)

destroyQueue operation

void destroyQueue(){

delete [] item;

}

Continue…

(29)

displayQueue operation

void displayQueue(){

cout<<"\n\tDisplay Item In Queue\n";

if(back < front){

cout<<"\n\tThere is no data in queue to be displayed\n";

}else{

cout<<"\t";

for(int i=0; i < max; i++ ){

cout<<"\t"<<item[i];

}

cout<<endl;

} }

Continue…

(30)

Queue Implementation Using Array(Linear)

int main() {

int selection;

menu:

cout<<"\nPlease Choose Your Selection\n";

cout<<"\n1\tenQueue\n";

cout<<"\n2\tdeQueue\n";

cout<<"\n3\tGetFront\n";

cout<<"\n4\tGetRear\n";

cout<<"\n5\tDestroyQueue\n";

cout<<"\n6\tDisplay\n";

cout<<"\n\tSelection is:";

cin>>selection;

Continue…

(31)

Queue Implementation Using Array(Linear)

Continue…

switch(selection){

case 1: enQueue();

displayQueue();

goto menu;

break;

case 2: deQueue();

displayQueue();

goto menu;

break;

case 3: getFront();

displayQueue();

goto menu;

break;

(32)

Queue Implementation Using Array(Linear)

case 4: getRear();

displayQueue();

goto menu;

break;

case 5: destroyQueue();

displayQueue();

goto menu;

break;

case 6: displayQueue();

goto menu;

break;

default:cout<<"\n\tWrong Selection\n";

} return 0;

}

(33)

Queue Implementation Using Array(Linear)

• Problem: Rightward-Drifting:

• After a sequence of additions & removals, items will drift towards the end of the array

• enQueue operation cannot be performed on the queue below, since back = max – 1.

5 front

4 back

0 1 2 3 4

NULL NULL NULL NULL NULL

item

back = 3 + 1 back = 4 front = 4 +

1

front = 5 front++

(34)

Queue Implementation Using Array(Linear)

• Rightward drifting solutions

– Shift array elements after each deletion

• Shifting dominates the cost of the

implementation

(35)

Queue Implementation Using Array(Linear)

– Use a circular array: When Front or Back

reach the end of the array, wrap them around to the beginning of the array

• Problem:

– Front & Back can't be used to

distinguish between queue-full & queue-

empty conditions

(36)

Queue Implementation Using Array(Linear)

• Solution:

– Use a counter

– Count == 0 means empty queue

– Count == MAX_QUEUE means full

queue

(37)

3.0 Queue Implementation Using

Array(Circular)

(38)

Queue Implementation Using Array(Circular)

• Number of elements in Queue are fixed during declaration.

• Need isFull() operation to determine

whether a queue is full or not.

(39)

Queue Implementation Using Array(Circular)

• Queue structure need at least 3 elements:

1) Element to store items in Queue 2) Element to store index at head 3) Element to store index at rear

4) Element to store index in counter

(40)

Create Queue Operation

• Declare

– front & back are indexes in the array – count to store index

– Initial condition: front =0 , back = -1, count = 0

– Size of an array in queue

(41)

Queue Implementation Using Array(Circular)

– The Wrap-around effect is obtained by using modulo arithmetic (%-operator)

front = 0

back = -1 0

1

2

3 4

5 6

7

count = 0

(42)

Queue Implementation Using Array(Circular)

– enQueue

• Increment back, using modulo arithmetic

• Insert item

• Increment count – deQueue

• Increment front using modulo arithmetic

• Decrement count – Disadvantage

• Overhead of maintaining a counter or

flag

(43)

Queue Implementation Using Array(Circular)

front = 0

back = -1 0

1

2

3 4

5 6

7

count = 0

Example Code 2:

#include <iostream>

using namespace std;

#define max 8

char queue[max], newitem;

int front = 0, back = -1, count = 0;

Continue…

queue

(44)

Queue Implementation Using Array(Circular)

void enQueue(){

cout<<"\n\t#### enQueue Circular ####\n";

if(count == max){

cout<<"\n\tQueue Circular Is Full!!!\n";

}else{

cout<<"\n\tfront:"<<front<<"\t"<<"back:"<<back<<"\tcount:"<<count<<“\tmax:”<<max<<"\n";

cout<<"\n\tEnter Item:";

cin>>newitem;

back = (back + 1)% max;

queue[back] = newitem;

count++;

} }

Continue…

front = 0 back = 0 0

1

2 3

4 5

6 7

count = 1

A back = (-1 + 1) % 8

back = 0 % 8 back = 0 queue[0] = A count = 0 + 1 count = 1 0

0 0 8√ 0

(45)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 1 0

1

2 3

4 5

6 7

count = 2

A back = (0 + 1) % 8

back = 1 % 8 back = 1 queue[1] = B count = 1 + 1 count = 2 0

0 1 8√ 1

B From previous slide: front = 0, back = 0, count = 1 queue

(46)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 2 0

1

2 3

4 5

6 7

count = 3

A back = (1 + 1) % 8

back = 2 % 8 back = 2 queue[2] = C count = 2 + 1 count = 3 0

0 2 8√ 2

B C From previous slide: front = 0, back = 1, count = 2

queue

(47)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 3 0

1

2 3

4 5

6 7

count = 4

A back = (2 + 1) % 8

back = 3 % 8 back = 3 queue[3] = D count = 3 + 1 count = 4 0

0 3 8√ 3

B C D From previous slide: front = 0, back = 2, count = 3

queue

(48)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 4 0

1

2 3

4 5

6 7

count = 5

A back = (3 + 1) % 8

back = 4 % 8 back = 4 queue[4] = E count = 4 + 1 count = 5 0

0 4 8√ 4

B C E D

From previous slide: front = 0, back = 3, count = 4

queue

(49)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 5

0

1

2 3

4 5

6 7

count = 6

A back = (4 + 1) % 8

back = 5 % 8 back = 5 queue[5] = F count = 5 + 1 count = 6 0

0 5 8√ 5

B C E D

From previous slide: front = 0, back = 4, count = 5

queue

F

(50)

enQueue Implementation Using Array(Circular)

Continue…

front = 0

back = 6 0

1

2 3

4 5

6 7

count = 7

A back = (5 + 1) % 8

back = 6 % 8 back = 6 queue[6] = G count = 6 + 1 count = 7 0

0 6 8√ 6

B C E D

From previous slide: front = 0, back = 5, count = 6

queue

F G

(51)

enQueue Implementation Using Array(Circular)

Continue…

front = 0 back = 7

0

1

2 3

4 5

6 7

count = 8

A back = (6 + 1) % 8

back = 7 % 8 back = 7 queue[7] = H count = 7 + 1 count = 8 0

0 7 8√ 7

B C E D

From previous slide: front = 0, back = 6, count = 7

queue

F G

H

(52)

deQueue Implementation Using Array(Circular)

void deQueue(){

cout<<"\n\t#### deQueue Circular ####\n";

if(count == 0){

cout<<"\n\tQueue Circular Is Empty, No Data To Be Deleted!!!\n";

}else{

queue[front] = NULL;

front=(front + 1) % max;

count--;

} }

Continue…

front = 1 back = 7

0

1

2 3

4 5

6 7

count = 7 queue[0] = NULL

front = (0 + 1) % 8 front = 1 % 8 front = 1 count = 8 - 1 count = 7 0

0 1 8√ 1

B C E D

queue

F G

H

(53)

deQueue Implementation Using Array(Circular)

Continue…

From previous slide: front = 1, back = 7 , count = 7

front = 2 back = 7

0

1

2 3

4 5

6 7

count = 6 queue[1] = NULL

front = (1 + 1) % 8 front = 2% 8

front = 2 count = 7 - 1 count = 6 0

0 2 8√ 2

C E D

queue

F G

H

(54)

Queue Implementation Using Array(Circular)

void displayQueue(){

cout<<"\n\t#### Display Queue Circular ####\n";

cout<<"\n\tfront:"<<front<<"\t"<<"back:"<<back<<"\tcount:"<<count<<“\tmax:”<<max<<"\n";

if(count == 0){

cout<<"\n\tQueue Circular Is Empty, No Data To Be Display\n";

}else{

cout<<"\n\tItem In Queue Circular\n";

for(int i = 0; i < max; i++){

cout<<"\t"<<queue[i];

} } }

Continue…

(55)

Queue Implementation Using Array(Circular)

int main(){

int selection;

menu:

cout<<"\n\nPlease Choose Your Selection\n";

cout<<"\n1\tenQueue Circular\n";

cout<<"\n2\tdeQueue Circular\n";

cout<<"\n3\tDisplay Queue\n";

cout<<"\n\tSelection is:";

cin>>selection;

Continue…

(56)

Queue Implementation Using Array(Circular)

switch(selection){

case 1: enQueue();

displayQueue();

goto menu;

break;

case 2: deQueue();

displayQueue();

goto menu;

break;

case 3: displayQueue();

goto menu;

break;

Continue…

(57)

Queue Implementation Using Array(Circular)

default:cout<<"\n\tWrong Selection\n";

} return 0;

}

(58)

4.0 Queue Implementation Using

Linked List(Linear)

(59)

Queue Implementation Using Linked List(Linear)

Pointer-Based Implementation

• More straightforward than array-based

• Need Two external pointer (Front & Back) which front to

trace deQueue operation and back to trace deQueue

operation.

(60)

Create Queue Implementation Using Linked List(Linear)

Example Code 1:

#include <iostream>

using namespace std;

struct nodeQueue{

char name;

int age;

nodeQueue *next;

};

name age next

Compiler get the initial illustrated structure of node

Continue…

(61)

Create Queue Implementation Using Linked List(Linear)

nodeQueue *back_ptr = NULL;

nodeQueue *front_ptr=NULL;

Continue…

NULL

NULL back_ptr

front_ptr

(62)

enQueue Implementation Using Linked List(Linear)

void enQueue(){

//create new node

nodeQueue *newnode;

newnode = new nodeQueue;

cout<<"\n\t####enQueue####\n";

//assign data field for name and age cout<<"Enter Name:";

cin>>newnode->name;

cout<<"Enter Age:";

cin>>newnode->age;

newnode->next = NULL;

Continue…

Ali 29 NULL

newnode

0110 0110

(63)

enQueue Implementation Using Linked List(Linear)

//insert newnode into queue //check whether queue is empty

if((front_ptr == NULL) && (back_ptr == NULL)){

front_ptr = newnode;

back_ptr = newnode;

}else{

back_ptr->next = newnode;

back_ptr = newnode;

}

Continue…

0110 front_ptr

0110 newnode

Ali 29 NULL

age 0110

name next

Insertion to an empty queue

0110 back_ptr

(64)

enQueue Implementation Using Linked List(Linear)

Continue…

Insertion to a non empty queue

back_ptr->next = newnode;

back_ptr=newnode;

Tina 30 NULL

0111

name age next

newnode 0111

0110 back_ptr 0110

front_ptr

Ali 29 NULL

age 0110

name next

(65)

enQueue Implementation Using Linked List(Linear)

Continue…

Insertion to a non empty queue

Tina 30 NULL

0111

name age next

0111 back_ptr 0110

front_ptr

Ali 29 0111

age 0110

name next

(66)

Continue…

deQueue Implementation Using Linked List(Linear)

(67)

void deQueue(){

cout<<"\n\t####deQueue####\n";

//check whether queue is empty

if((front_ptr == NULL) && (back_ptr == NULL)){

cout<<"\n\tQueue Is Empty!!!\n";

}else{

nodeQueue *temp;

temp = front_ptr;

if(front_ptr->next == NULL){

front_ptr = NULL;

back_ptr = NULL;

delete temp;

}else{

front_ptr = front_ptr->next;

delete temp; } } }

Continue…

If the queue contains one item only

(68)

Continue…

deQueue Implementation Using Linked List(Linear)

If the queue contains one item only to be deleted

nodeQueue *temp;

temp = front_ptr;

0110 front_ptr

Ali 29 NULL

age 0110

name next

0110 back_ptr

0110 temp

if(front_ptr->next == NULL){

front_ptr = NULL;

back_ptr = NULL;

delete temp;

}else{

…}

NULL front_ptr

NULL back_ptr

(69)

deQueue Implementation Using Linked List(Linear)

Continue…

If the queue contains more than one item

nodeQueue *temp;

temp = front_ptr;

Tina 30 NULL

0111

name age next

0111 back_ptr 0110

front_ptr

Ali 29 0111

age 0110

name next

0110 temp

(70)

Continue…

…}else{

front_ptr = front_ptr->next;

delete temp; }

Tina 30 NULL

0111

name age next

0111 back_ptr 0111

front_ptr

Ali 29 0111

age 0110

name next

0110 temp

Tina 30 NULL

0111

name age next

0111 back_ptr 0111

front_ptr

(71)

Continue…

displayQueue Implementation Using Linked List(Linear)

void displayQueue(){

cout<<"\n\t####Display Queue####\n";

if((front_ptr == NULL) && (back_ptr == NULL)){

cout<<"\n\tQueue Is Empty!!!\n";

cout<<"\n\tfront_ptr :"<<front_ptr<<"\tback_ptr :"<<back_ptr<<endl;

}else{

nodeQueue *cursor;

cursor=front_ptr;

cout<<"\n\tThe Elements In Queue Are\n";

cout<<"\n\tfront_ptr :"<<front_ptr<<"\tback_ptr :"<<back_ptr<<endl;

int node=1;

while(cursor){

cout<<"\n\tNode :"<<node++<<"\tName :"<<cursor->name<<"\tAge :"<<cursor-

>age<<"\tcursor-next:"<<cursor->next<<endl;

cursor=cursor->next; } }

(72)

Continue…

Queue Implementation Using Linked List(Linear)

int main() {

int selection;

menu:

cout<<"\n\nMenu Selection\n";

cout<<"\n1\tenQueue\n";

cout<<"\n2\tdeQueue\n";

cout<<"\n3\tDisplay Queue\n";

cout<<"\n\tSelection is:";

cin>>selection;

(73)

Continue…

Queue Implementation Using Linked List(Linear)

switch(selection){

case 1: enQueue();

displayQueue();

goto menu;

break;

case 2: deQueue();

displayQueue();

goto menu;

break;

case 3: displayQueue();

goto menu;

break;

default:cout<<"\n\tWrong Selection\n"; } return 0;

}

References

Related documents

Dalam hal ini perjanjian internasional yang menjadi objek kajian adalah Mutual Commitments antara Pemerintah Liberia dan United Nations Peacebuilding Commision dalam

Requirements Management Change Management Release Management Data Management Records Management Document Control Library Management Configuration Management (business

An analysis of the economic contribution of the software industry examined the effect of software activity on the Lebanese economy by measuring it in terms of output and value

The clumsy number systems used by the Romans retarded the development of mathematics.. It was not until cen- turies after the decline of Rome that a new awakening

Polycystic ovary syndrome (PCOS) is a very common endocrine disorder, affecting approximately 10% of females in the re- productive age worldwide. PCOS is not only associated

UNESCO World Heritage Site’s indicators of success and the tools used to measure this mainly focus on the maintenance of the site’s Outstanding Universal Value, its

Trumm et al (2013) assessed the impact of the advanced technology of the new ExAblate 2100 system (Insightec Ltd, Haifa, Israel) for MRI-guided focused ultrasound surgery on

Conversely, 43.7% of all respondents who misused prescription drugs met criteria for alcohol dependence, problem gambling, and (or) had used illicit drugs in the past year..