ngc training programme
TRANSCRIPT
-
8/10/2019 NGC Training Programme
1/73
QUEUE
-
8/10/2019 NGC Training Programme
2/73
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.
-
8/10/2019 NGC Training Programme
3/73
1.0 Introduction to Queue
-
8/10/2019 NGC Training Programme
4/73
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
-
8/10/2019 NGC Training Programme
5/73
Introduction to Queue
Important in simulation & analyzing the
behavior of complex systems
-
8/10/2019 NGC Training Programme
6/73
Queue Applications
Real-World Applications
Cashier lines in any store
Check out at a bookstore
Bank / ATM
Call an airline
-
8/10/2019 NGC Training Programme
7/73
Queue Applications
Computer Science Applications
Print lines of a document
Printer sharing between computers
Recognizing palindromes
Shared resource usage (CPU, memory
access, )
-
8/10/2019 NGC Training Programme
8/73
Queue Applications
Simulation
A study to see how to reduce the wait
involved in an application
-
8/10/2019 NGC Training Programme
9/73
Queue implementation
Add/
Enqueue
Remove/
Dequeue
Back/RearFront/Head
A B C
Basic Structure of a Queue:
Data structure that hold the queue
head
rear
-
8/10/2019 NGC Training Programme
10/73
Queue implementation
Add/
Enqueue
RearHead
A B C D
Insert D into Queue (enQueue) : D is inserted at rear
RearHead
B C D
Remove/
Dequeue
A
Delete from Queue (deQueue) : A is removed
-
8/10/2019 NGC Training Programme
11/73
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)
-
8/10/2019 NGC Training Programme
12/73
Queue Implementation
Implementation:
Array-based (Linear or Circular)
Pointer-based : Link list (Linear or
Circular)
-
8/10/2019 NGC Training Programme
13/73
2.0 Queue Implementation Using
Array(Linear)
-
8/10/2019 NGC Training Programme
14/73
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.
-
8/10/2019 NGC Training Programme
15/73
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
-
8/10/2019 NGC Training Programme
16/73
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
-
8/10/2019 NGC Training Programme
17/73
Create Queue operation
Example Code 1
#include
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
-
8/10/2019 NGC Training Programme
18/73
enQueue operation
void enQueue(){
cout
-
8/10/2019 NGC Training Programme
19/73
enQueue operation
Continue
0
front
1
back
0 1 2 3 4
A B
item
back = 0 +1
back = 1
From back/rearitem[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
-
8/10/2019 NGC Training Programme
20/73
enQueue operation
Continue
0
front
3
back
0 1 2 3 4
A B C D
item
back = 2 +1
back = 3
From back/rearitem[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
-
8/10/2019 NGC Training Programme
21/73
deQueue operation
void deQueue(){
cout
-
8/10/2019 NGC Training Programme
22/73
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 = 1From 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
-
8/10/2019 NGC Training Programme
23/73
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
-
8/10/2019 NGC Training Programme
24/73
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/headitem[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
-
8/10/2019 NGC Training Programme
25/73
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
-
8/10/2019 NGC Training Programme
26/73
Retrieve at front(getFront) operation
void getFront(){
cout
-
8/10/2019 NGC Training Programme
27/73
Retrieve at back(getRear) operation
void getRear(){
cout
-
8/10/2019 NGC Training Programme
28/73
destroyQueue operation
void destroyQueue(){
delete [] item;
}
Continue
-
8/10/2019 NGC Training Programme
29/73
displayQueue operation
void displayQueue(){
cout
-
8/10/2019 NGC Training Programme
30/73
Queue Implementation Using Array(Linear)
int main()
{int selection;
menu:
cout
-
8/10/2019 NGC Training Programme
31/73
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;
-
8/10/2019 NGC Training Programme
32/73
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
-
8/10/2019 NGC Training Programme
33/73
-
8/10/2019 NGC Training Programme
34/73
Queue Implementation Using Array(Linear)
Rightward drifting solutions
Shift array elements after each deletion
Shifting dominates the cost of the
implementation
-
8/10/2019 NGC Training Programme
35/73
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
-
8/10/2019 NGC Training Programme
36/73
Queue Implementation Using Array(Linear)
Solution:
Use a counter
Count == 0 means empty queue
Count == MAX_QUEUE means full
queue
-
8/10/2019 NGC Training Programme
37/73
3.0 Queue Implementation Using
Array(Circular)
-
8/10/2019 NGC Training Programme
38/73
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.
-
8/10/2019 NGC Training Programme
39/73
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
-
8/10/2019 NGC Training Programme
40/73
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
-
8/10/2019 NGC Training Programme
41/73
Queue Implementation Using Array(Circular)
The Wrap-around effect is obtained by using
modulo arithmetic (%-operator)
front = 0
back = -1
0
1
2
34
5
6
7
count = 0
-
8/10/2019 NGC Training Programme
42/73
Queue Implementation Using Array(Circular)
enQueue
Increment back, using modulo arithmetic
Insert item
Increment count
deQueue
Increment frontusing modulo arithmetic
Decrement count
Disadvantage
Overhead of maintaining a counter or
flag
-
8/10/2019 NGC Training Programme
43/73
Queue Implementation Using Array(Circular)
front = 0
back = -1
0
1
2
34
5
6
7
count = 0
Example Code 2:
#include
using namespace std;
#define max 8
char queue[max], newitem;
int front = 0, back = -1, count = 0;
Continue
queue
-
8/10/2019 NGC Training Programme
44/73
Queue Implementation Using Array(Circular)
void enQueue(){
cout
-
8/10/2019 NGC Training Programme
45/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 1
0
1
2
34
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
-
8/10/2019 NGC Training Programme
46/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 2
0
1
2
34
5
6
7
count = 3
A
back = (1 + 1) % 8
back = 2 % 8back = 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
-
8/10/2019 NGC Training Programme
47/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 3
0
1
2
34
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
-
8/10/2019 NGC Training Programme
48/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 4
0
1
2
34
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
DE
From previous slide: front = 0, back = 3, count = 4 queue
-
8/10/2019 NGC Training Programme
49/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 5
0
1
2
34
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
DE
From previous slide: front = 0, back = 4, count = 5 queue
F
-
8/10/2019 NGC Training Programme
50/73
enQueue Implementation Using Array(Circular)
Continue
front = 0
back = 6 0
1
2
34
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
DE
From previous slide: front = 0, back = 5, count = 6 queue
F
G
-
8/10/2019 NGC Training Programme
51/73
enQueue Implementation Using Array(Circular)
Continue
front = 0back = 7
0
1
2
34
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
DE
From previous slide: front = 0, back = 6, count = 7queue
F
G
H
-
8/10/2019 NGC Training Programme
52/73
deQueue Implementation Using Array(Circular)
void deQueue(){
cout
-
8/10/2019 NGC Training Programme
53/73
deQueue Implementation Using Array(Circular)
Continue
From previous slide: front = 1, back = 7 , count = 7
front = 2
back = 7
0
1
2
34
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
DE
queue
F
GH
-
8/10/2019 NGC Training Programme
54/73
Queue Implementation Using Array(Circular)
void displayQueue(){
cout
-
8/10/2019 NGC Training Programme
55/73
Queue Implementation Using Array(Circular)
int main(){
int selection;
menu:
cout
-
8/10/2019 NGC Training Programme
56/73
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
-
8/10/2019 NGC Training Programme
57/73
Queue Implementation Using Array(Circular)
default:cout
-
8/10/2019 NGC Training Programme
58/73
4.0 Queue Implementation Using
Linked List(Linear)
-
8/10/2019 NGC Training Programme
59/73
Queue Implementation Using Linked List(Linear)
Pointer-Based Implementation More straightforward than array-based
Need Two external pointer (Front & Back) which frontto
trace deQueue operation and backto trace deQueueoperation.
C t Q I l t ti U i Li k d
-
8/10/2019 NGC Training Programme
60/73
Create Queue Implementation Using Linked
List(Linear)
Example Code 1:
#include
using namespace std;
struct nodeQueue{
char name;
int age;
nodeQueue *next;
};
name age next
Compiler get the initial illustrated structure of node
Continue
Create Queue Implementation Using Linked
-
8/10/2019 NGC Training Programme
61/73
Create Queue Implementation Using Linked
List(Linear)
nodeQueue *back_ptr = NULL;
nodeQueue *front_ptr=NULL;
Continue
NULL
NULL
back_ptr
front_ptr
enQueue Implementation Using Linked
-
8/10/2019 NGC Training Programme
62/73
enQueue Implementation Using Linked
List(Linear)
void enQueue(){
//create new node
nodeQueue *newnode;
newnode = new nodeQueue;
coutage;
newnode->next = NULL;
Continue
Ali 29 NULL
newnode
0110
0110
enQueue Implementation Using Linked
-
8/10/2019 NGC Training Programme
63/73
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
0110newnode
Ali 29 NULL
age
0110
name next
Insertion to an empty queue
0110
back_ptr
enQueue Implementation Using Linked
-
8/10/2019 NGC Training Programme
64/73
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 nextnewnode
0111
0110
back_ptr
0110
front_ptr
Ali 29 NULL
age
0110
name next
enQueue Implementation Using Linked
-
8/10/2019 NGC Training Programme
65/73
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
-
8/10/2019 NGC Training Programme
66/73
Continue
deQueue Implementation Using Linked List(Linear)
void deQueue(){
-
8/10/2019 NGC Training Programme
67/73
cout
-
8/10/2019 NGC Training Programme
68/73
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
-
8/10/2019 NGC Training Programme
69/73
-
8/10/2019 NGC Training Programme
70/73
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
displayQueue Implementation Using Linked
-
8/10/2019 NGC Training Programme
71/73
Continue
displayQueue Implementation Using LinkedList(Linear)
void displayQueue(){
cout
-
8/10/2019 NGC Training Programme
72/73
Continue
Queue Implementation Using Linked List(Linear)
int main()
{
int selection;
menu:
cout
-
8/10/2019 NGC Training Programme
73/73
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