notes data structure - queue
Post on 23-Jan-2015
3.686 Views
Preview:
DESCRIPTION
TRANSCRIPT
QUEUE
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.
1.0 Introduction to Queue
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
Introduction to Queue
• Important in simulation & analyzing the
behavior of complex systems
Queue Applications
• Real-World Applications
– Cashier lines in any store
– Check out at a bookstore
– Bank / ATM
– Call an airline
Queue Applications
• Computer Science Applications
– Print lines of a document
– Printer sharing between computers
– Recognizing palindromes
– Shared resource usage (CPU, memory
access, …)
Queue Applications
• Simulation
– A study to see how to reduce the wait
involved in an application
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
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
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)
Queue Implementation
Implementation:
– Array-based (Linear or Circular)
– Pointer-based : Link list (Linear or
Circular)
2.0 Queue Implementation Using
Array(Linear)
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.
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
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
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
enQueue operationvoid 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+1back = 0
From back/rearitem[back] = newitem
back++
Front refer to index 0
enQueue operation
Continue…
0
front
1
back
0 1 2 3 4
A B
item
back = 0 +1back = 1
From back/rearitem[back] = newitem
0
front
2
back
0 1 2 3 4
A B C
item
back = 1 +1back = 2
From back/rearitem[back] = newitem
back++
back++
Front refer to index 0
Front refer to index 0
enQueue operation
Continue…
0
front
3
back
0 1 2 3 4
A B C D
item
back = 2 +1back = 3
From back/rearitem[back] = newitem
0
front
4
back
0 1 2 3 4
A B C D E
item
back = 3 +1back = 4
From back/rearitem[back] = newitem
back++
back++
Front refer to index 0
Front refer to index 0
deQueue operationvoid 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 + 1back = 4itemdeleted = item[front]
front = 0
From front/headitem[front] = NULL
Front refer to index 0
1
front
4
back
0 1 2 3 4
NULL B C D E
item
back = 3 + 1back = 4
front = 0 + 1front = 1
front++
Continue…
1
front
4
back
0 1 2 3 4
NULL B C D E
item
back = 3 + 1back = 4
itemdeleted = item[front]front = 1
From front/headitem[front] = NULL
2
front
4
back
0 1 2 3 4
NULL NULL C D E
item
back = 3 + 1back = 4
front = 1 + 1front = 2
front++
Front refer to index 1
Front refer to index 1
Front refer to index 2
deQueue operation
Continue…
2
front
4
back
0 1 2 3 4
NULL NULL C D E
item
back = 3 + 1back = 4
itemdeleted = item[front]front = 2
From front/headitem[front] = NULL
3
front
4
back
0 1 2 3 4
NULL NULL NULL D E
item
back = 3 + 1back = 4
front = 2 + 1front = 3
front++
Front refer to index 2
Front refer to index 3
deQueue operation
Continue…
3
front
4
back
0 1 2 3 4
NULL NULL NULL D E
item
back = 3 + 1back = 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 + 1back = 4
front = 3 + 1front = 4
front++
Front refer to index 3
Front refer to index 4
deQueue operation
Continue…
4
front
4
back
0 1 2 3 4
NULL NULL NULL NULL E
item
back = 3 + 1back = 4
itemdeleted = item[front]front = 4
From front/headitem[front] = NULL
5
front
4
back
0 1 2 3 4
NULL NULL NULL NULL NULL
item
back = 3 + 1back = 4
front = 4 + 1front = 5
front++
Front refer to index 4
deQueue operation
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…
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…
destroyQueue operation
void destroyQueue(){
delete [] item;
}
Continue…
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…
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…
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;
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;}
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 + 1back = 4
front = 4 + 1front = 5
front++
Queue Implementation Using Array(Linear)
• Rightward drifting solutions
– Shift array elements after each deletion
• Shifting dominates the cost of the
implementation
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
Queue Implementation Using Array(Linear)
• Solution:
– Use a counter
– Count == 0 means empty queue
– Count == MAX_QUEUE means full
queue
3.0 Queue Implementation Using
Array(Circular)
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.
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
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
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
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
Queue Implementation Using Array(Circular)
front = 0
back = -1
0
1
2
34
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
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 = 00
1
2
34
5
6
7
count = 1
A
back = (-1 + 1) % 8back = 0 % 8back = 0queue[0] = A
count = 0 + 1count = 1
0
0
0
8√ 0
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 1
0
1
2
34
5
6
7
count = 2
A
back = (0 + 1) % 8back = 1 % 8back = 1queue[1] = B
count = 1 + 1count = 2
0
0
1
8√ 1
B
From previous slide: front = 0, back = 0, count = 1 queue
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 2
0
1
2
34
5
6
7
count = 3
A
back = (1 + 1) % 8back = 2 % 8back = 2queue[2] = C
count = 2 + 1count = 3
0
0
2
8√ 2
B
C
From previous slide: front = 0, back = 1, count = 2
queue
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 3
0
1
2
34
5
6
7
count = 4
A
back = (2 + 1) % 8back = 3 % 8back = 3queue[3] = D
count = 3 + 1count = 4
0
0
3
8√ 3
B
C
D
From previous slide: front = 0, back = 2, count = 3queue
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 4
0
1
2
34
5
6
7
count = 5
A
back = (3 + 1) % 8back = 4 % 8back = 4queue[4] = E
count = 4 + 1count = 5
0
0
4
8√ 4
B
C
DE
From previous slide: front = 0, back = 3, count = 4queue
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 5
0
1
2
34
5
6
7
count = 6
A
back = (4 + 1) % 8back = 5 % 8back = 5queue[5] = F
count = 5 + 1count = 6
0
0
5
8√ 5
B
C
DE
From previous slide: front = 0, back = 4, count = 5queue
F
enQueue Implementation Using Array(Circular)
Continue…
front = 0
back = 6 0
1
2
34
5
6
7
count = 7
A
back = (5 + 1) % 8back = 6 % 8back = 6queue[6] = G
count = 6 + 1count = 7
0
0
6
8√ 6
B
C
DE
From previous slide: front = 0, back = 5, count = 6queue
F
G
enQueue Implementation Using Array(Circular)
Continue…
front = 0back = 7
0
1
2
34
5
6
7
count = 8
A
back = (6 + 1) % 8back = 7 % 8back = 7queue[7] = H
count = 7 + 1count = 8
0
0
7
8√ 7
B
C
DE
From previous slide: front = 0, back = 6, count = 7queue
F
G
H
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
34
5
6
7
count = 7
queue[0] = NULLfront = (0 + 1) % 8front = 1 % 8front = 1
count = 8 - 1count = 7
0
0
1
8√ 1
B
C
DE
queue
F
G
H
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] = NULLfront = (1 + 1) % 8front = 2% 8front = 2
count = 7 - 1count = 6
0
0
2
8√ 2
C
DE
queue
F
G
H
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…
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…
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…
Queue Implementation Using Array(Circular)
default:cout<<"\n\tWrong Selection\n";
}
return 0;
}
4.0 Queue Implementation Using
Linked List(Linear)
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.
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…
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
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
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
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
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
Continue…
deQueue Implementation Using Linked List(Linear)
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
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
tempif(front_ptr->next == NULL){
front_ptr = NULL;
back_ptr = NULL;
delete temp;
}else{
…}
NULL
front_ptr
NULL
back_ptr
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_ptr0110
front_ptr
Ali 29 0111
age
0110
name next
0110
temp
Continue…
…}else{
front_ptr = front_ptr->next;
delete temp; }
Tina 30 NULL
0111
name age next
0111
back_ptr0111
front_ptr
Ali 29 0111
age
0110
name next
0110
temp
Tina 30 NULL
0111
name age next
0111
back_ptr0111
front_ptr
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; } }
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;
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;
}
top related