0% found this document useful (0 votes)
27 views

Queue: by Khuram Shahzad

This document discusses queues and their implementation using arrays. It covers: 1) Queues follow FIFO (first in, first out) ordering and have applications in computer systems like job scheduling and mouse clicks. 2) Queues can be implemented using arrays by tracking the front and rear indices, with enqueue adding to the rear and dequeue removing from the front. Overflow is checked by comparing rear to array size. 3) Another implementation shifts elements left on enqueue if rear reaches the end, and circular queues treat the array as circular by using modulo when incrementing rear.

Uploaded by

Michael Cole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Queue: by Khuram Shahzad

This document discusses queues and their implementation using arrays. It covers: 1) Queues follow FIFO (first in, first out) ordering and have applications in computer systems like job scheduling and mouse clicks. 2) Queues can be implemented using arrays by tracking the front and rear indices, with enqueue adding to the rear and dequeue removing from the front. Overflow is checked by comparing rear to array size. 3) Another implementation shifts elements left on enqueue if rear reaches the end, and circular queues treat the array as circular by using modulo when incrementing rear.

Uploaded by

Michael Cole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Queue

by
Khuram Shahzad
Queue
FIFO (First In First Out)
Applications of Queue
In real world
The line at the supermarket
People line up or queue up for getting on a bus
Waiting at the bank

In Computer world
A List of Jobs Waiting to be Printed
Mouse Clicks
Key Stroke Buffer
Operating system process scheduling

Queue
FIFO
enqueue (5) enqueue (7) enqueue (9) enqueue (3) dequeue () dequeue ()
5 7 3 9
enqueue (6)
6
0 1 2 3 4
rear
enqueue (5) enqueue (7) enqueue (9) enqueue (3) dequeue () dequeue ()
5 7 3 9
Queue implementation
using arrays
bool isFull( ){
if(rear == size-1)
return true;
return false;
}
bool isEmpty( ){
if(rear == -1)
return true;
return false;
}
bool enqueue ( int a){
if(isFull()){
cout<<Error: Overflow;
return false;
}
arr[++rear] = a;
return true;
}
bool dequeue (){
if(isEmpty()){
cout<<Error: Underflow;
return false;
}
for(int i=0; i<rear; i++){
arr[i]=arr[i+1];}
rear--;
return true;
}
const int size = 5;
int arr[size];
int rear = -1;
0 1 2 3 4
rear
enqueue (5) enqueue (7) enqueue (9) enqueue (3) dequeue () dequeue ()
5 7 3 9
0 1 2 3 4
rear
enqueue (5) enqueue (7) enqueue (9) enqueue (3) dequeue () dequeue ()
5 7 3 9
Another Queue implementation
using arrays
enqueue (6)
6
dequeue ()
front
enqueue (4)
4
0 1 2 3 4
rear
enqueue (5) enqueue (7) enqueue (9) enqueue (3) dequeue () dequeue ()
5 7
3
9
enqueue (6)
6
dequeue ()
front
enqueue (4)
4 8
enqueue (8)
bool isFull( ){
if(front == -1 && rear == size-1)
return true;
return false;
}
bool isEmpty( ){
if(front == rear)
return true;
return false;
}
bool enqueue ( int a){
if(isFull()){
cout<<Error: Overflow;
return false;
}
if(rear == size-1 && front != -1)
shiftLeft();
arr[++rear] = a;
return true;
}
int dequeue (){
if(isEmpty()){
cout<<Error: Underflow;
exit(0);
}
return arr[++front];
}
const int size = 5;
int arr[size];
int front = rear = -1;
shiftLeft( );
0
1
2
3
4
5
7
3
6
8
enque(5)
9
4
2
enque(7) enque(3) enque(8) deque() enque(6) deque() enque(9) deque() enque(4) enque(2)
Circular Queue
Circular Arrays

rear
front
Problem.
rear = (rear + 1) % size
and front?

You might also like