Queues
SOUMEN SANTRA
MCA, M.Tech, SCJP, MCP
DEFINITION OF QUEUE
• A Queue is an ordered collection ADT(Abstract Data Type) of items
from which items may be deleted at one end (FRONT) and into
which items may be inserted at the other end (REAR).
• The first element inserted into the queue is the first element to be
removed. The queue is sometimes called a FIFO (first-in first-out) list
as opposed to the stack, which is a LIFO (last-in first-out).
Queue
• Stores a set of elements in a particular order
• Stack principle: FIRST IN FIRST OUT
• = FIFO
• It means: the first element inserted is the first one to be
removed.
• The first one in line is the first one to be served
• Example
Queue Applications
• Real life examples
• Waiting in line for reservation
• Waiting on hold for tech support
• Applications related to Computer Science
• Threads scheduling & synchronization
• Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
A
B
A
C
B
A
D
C
B
A
D
C
Brear
front
rear
front
rear
front
rear
front
rear
front
First In First Out
front rear Q[0] Q[1] Q[2] Q[3] Comments
-1
-1
-1
-1
0
1
-1
0
1
2
2
2
J1
J1 J2
J1 J2 J3
J2 J3
J3
queue is empty
Job 1 is added
Job 2 is added
Job 3 is added
Job 1 is deleted
Job 2 is deleted
Applications: Job Scheduling
Queue
items[MAX-1]
. .
. .
. .
items[2] C
items[1] B
items[0] A Front=0
Rear=2
Declaration of a Queue
# define MAX 500 /* size of the queue items*/
typedef struct {
int front;
int rear;
int items[MAX];
}QUEUE;
QUEUE OPERATIONS
• Initialize the queue
• Insert to the rear of the queue
• Remove (Delete) from the front of the queue
• Is the Queue Empty
• Is the Queue Full
• What is the size of the Queue
Queue Example: Class Diagram Concept
+Queue()
+insert() : void
+remove() : long
+peekFront() : long
+isEmpty() : bool
+isFull() : bool
+size() : int
-maxSize : int
-queueArray [] : long
-front : int
-rear : int
-nItems : int
Queue
QueueApp
Interface1
INITIALIZE THE QUEUE
items[MAX-1]
. .
. .
.
items[1]
items[0] front=0
rear=-1
•The queue is initialized by having the rear set to -1, and front set to 0. Let us
assume that maximum number of the element we have in a queue is MAX size
elements as shown below.
insert(&Queue, ‘A’) Method
• an item (A) is inserted at the Rear of the queue
items[MAX-1]
. .
. .
items[3]
items[2]
items[1]
items[0] A Front=0,
Rear=0
insert(&Queue, ‘B’) Method
• A new item (B) is inserted at the Rear of the queue
items[MAX-1]
. .
. .
items[3]
items[2]
items[1] B Rear=1
items[0] A Front=0
insert(&Queue, ‘C’) Method
• A new item (C) is inserted at the Rear of the queue
items[MAX-1]
. .
. .
items[3]
items[2] C Rear=2
items[1] B
items[0] A Front=0
insert(&Queue, ‘D’) Method
• A new item (D) is inserted at the Rear of the queue
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B
items[0] A Front=0
Array-based Queue Implementation
• As with the array-based stack implementation, the array is
of fixed size
• A queue of maximum N elements
• Slightly more complicated
• Need to maintain CONTROL of both front and rear
Implementation 1
Implementation 2
Insert Operation of Queue using pointer
void insert(QUEUE *qptr, char x)
{
if(qptr->rear == MAX-1)
{
printf("Queue is full!");
exit(1);
}
else
{
qptr->rear++;
qptr->items[qptr->rear]=x;
}
}
char remove(&Queue) Method
• an item (A) is removed (deleted) from the Front of
the queue & return it as per datatype.
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] A
char remove(&Queue) Method
• Remove two items from the front of the queue.
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C Front=2
items[1] B
items[0] A
char remove(&Queue) Method
• Remove two items from the front of the queue.
items[MAX-1]
. .
. .
items[3] D Front=Rear=3
items[2] C
items[1] B
items[0] A
char remove(&Queue) Method
• Remove one more item from the front of the queue.
items[MAX-1]
. .
items[4] Front=4
items[3] D Rear=3
items[2] C
items[1] B
items[0] A
Remove Operation implementation in c
char remove(struct queue *qptr)
{
char p;
if(qptr->front > qptr->rear){
printf("Queue is empty");
exit(1);
}
else
{p=qptr->items[qptr->front];
qptr->front++;
return p;
}
}
INSERT / REMOVE ITEMS : Drawback of Linear Queue
• Assume that the rear= MAX-1
•What happens if we want to insert a new
item into the queue?
items[MAX-1] X rear=MAX-1
. .
. .
items[3] D front=3
items[2] C
items[1] B
items[0] A
INSERT / REMOVE ITEMS
• What happens if we want to insert a new item F into the
queue?
• Although there is some empty space, the queue is full.
• One of the methods to overcome this problem is to shift all
the items to occupy the location of deleted item.
REMOVE ITEM
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] A
REMOVE ITEM : Example
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B Front=1
items[0] B
REMOVE ITEM : Example
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] C
items[0] B
REMOVE ITEM : Example
items[MAX-1]
. .
. .
items[3] D Rear=3
items[2] D
items[1] C
items[0] B
REMOVE ITEM : Example
items[MAX-1]
. .
. .
items[3] D
items[2] D Rear=2
items[1] C
items[0] B
Modified Remove Operation implementation
in C
char remove(struct queue *qptr)
{
char p;
int i;
if(qptr->front > qptr->rear){
printf("Queue is empty");
exit(1);
}
else
{p=qptr->items[qptr->front];
for(i=1;i<=qptr->rear;i++)
qptr->items[i-1]=qptr->items[i];
qptr->rear--
return p;
}
}
INSERT / REMOVE ITEMS
• Since all the items in the queue are required to shift when an
item is deleted, this method is not preferred.
• The other method is circular queue.
• When rear = MAX-1, the next element is entered at items[0] in
case that spot is free.
How to Initialize the queue
items[6] front=rear=6
items[5]
items[4]
items[3]
items[2]
items[1]
items[0]
Insert operation in Circular Queue
items[6] front=6
items[5]
items[4]
items[3]
items[2]
items[1]
items[0] 1 rear=0
 Insert 1,2,3 to the rear of the queue.
Insert operation in Circular Queue
items[6] front=6
items[5]
items[4]
items[3]
items[2]
items[1] 2 rear=1
items[0] 1
 Insert 1,2,3 to the rear of the queue.
Insert operation in Circular Queue
• Insert 1,2,3 to the rear of the queue.
items[6] front=6
items[5]
items[4]
items[3]
items[2] 3 rear=2
items[1] 2
items[0] 1
Remove items from circular queue
• Remove two items from the queue.
items[6]
items[5]
items[4]
items[3]
items[2] 3 rear=2
items[1] 2
items[0] 1 front=0
Remove items from circular queue
• Remove two items from the queue.
items[6]
items[5]
items[4]
items[3]
items[2] 3 rear=2
items[1] 2 front=1
items[0] 1
Remove items from circular queue
• Remove one more item from the queue.
items[6]
items[5]
items[4]
items[3]
items[2] 3 rear=front=2
items[1] 2
items[0] 1
Insert operation in Circular Queue:
Overcome Drawback of Linear Queue
• Insert 4,5,6,7 to the queue.
items[6] 7 rear=6
items[5] 6
items[4] 5
items[3] 4
items[2] 3 front=2
items[1] 2
items[0] 1
Insert operation in Circular Queue:
Overcome Drawback of Linear Queue
• Insert 8 and 9 to the queue.
items[6] 7
items[5] 6
items[4] 5
items[3] 4
items[2] 3 front=2
items[1] 2
items[0] 8 rear=0
Insert operation in Circular Queue:
Overcome Drawback of Linear Queue
• Insert 8 and 9 to the queue.
items[6] 7
items[5] 6
items[4] 5
items[3] 4
items[2] 3 front=2
items[1] 9
items[0] 8 rear=0
Insert operation in Circular Queue:
Overcome Drawback of Linear Queue
• Insert 10 to the queue.
items[6] 7
items[5] 6
items[4] 5
items[3] 4
items[2] ?? front=rear=2
items[1] 9
items[0] 8
Implementation of a Circular Queue in C
#define MAX 10 /* size of the queue items*/
typedef struct {
int front;
int rear;
int items[MAX];
}QUEUE;
QUEUE Q;
Q.front = MAX-1;
Q.rear= MAX-1;
EMPTY QUEUE
[2] [3] [2] [3]
[1] [4] [1] [4]
[0] [5] [0] [5]
front = 0 front = 0
rear = 0 rear = 3
B
A
C
Implementation 2:Cyclic View
Can be seen as a circular queue
FULL QUEUE FULL QUEUE
[2] [3] [2] [3]
[1] [4][1] [4]
[0] [5] [0] [5]
front =0
rear = 5
front =4
rear =3
B C
A D
E F E
G
H I
Advantage of Circular Queue:
How to test when queue is UNDERFLOW?
How to test when queue is OVERFLOW?
Implementation of Insert Operation for
Circular Queue in C
void insert(QUEUE *qptr, char x)
{
if(qptr->rear == MAX-1)
qptr->rear=0;
else
qptr->rear++;
/* or qptr->rear=(qptr->rear+1)%MAX) */
if(qptr->rear == qptr->front){
printf("Queue overflow");
exit(1);
}
qptr->items[qptr->rear]=x;
}
Implementation of Remove Operation
for Circular Queue in C
char remove(struct queue *qptr)
{
if(qptr->front == qptr->rear){
printf("Queue underflow");
exit(1);
}
if(qptr->front == MAX-1)
qptr->front=0;
else
qptr->front++;
return qptr->items[qptr->front];
}
#include <stdlib.h>
#include <stdio.h>
#define MAX 50
#define TRUE 1
#define FALSE 0
typedef struct
{
int items[MAX];
int front , rear ;
} Q;
void queinsert( Q * , int);
int quedelete(Q *);
int isempty(Q *);
EXAMPLE in C
int main()
{
char operation;
int x;
Q q;
q.front = q.rear = MAX - 1;
do
{
printf("%sn",“Queue Operation type I(Insert) D(Delete) or E(Exit) ");
scanf("n%c",&choice);
switch (choice) {
case 'I’: printf("%sn","Insert an element");
scanf("n%d",&x);
qinsert(&q , x);
break;
case 'D’: x=qdelete(&q);
printf("n %d is deleted n",x);
break;
default: printf(“Incorrect Operation typen”);
break;
}
}
while (choice != 'E’&&choice!='e');
return 0;
}
int isempty(Q *qptr)
{
return((qptr->front == qptr->rear) ? TRUE : FALSE);
}
int qdelete(Q *qptr)
{
if (empty(qptr)) {
printf("Queue underflow ");
exit(1);
}
qptr->front=(qptr->front+1)%(MAX)
return(qptr->items[qptr->front]);
}
void qinsert(Q *qptr , int x)
{
/* make room for new element */
printf("n %d is inserted n",x);
qptr->rear=(qptr->rear+1)%(MAX)
if (qptr->rear == qptr->front) {
printf("Queue overflow");
exit(1);
}
qptr->items[qptr->rear] = x;
return;
}
#include<stdio.h>
#include<stdlib.h>
#define MAX 5 /* size of the queue items*/
typedef struct{
int front;
int rear;
float items[MAX];
}Q;
void qinsert(Q *, float);
float qdelete(Q *);
void display( Q *);
void clear( Q *);
void menu();
int main()
{
int choice;
float x;
Q q;
q.front = M-1;
q.rear= MAX-1;
do{
menu();
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the item to be inserted:");
scanf("%f",&x);
insert(&q,x);
break;
case 2: x=remove(&q); printf("nRemoved item:%f",x);
break;
case 3: display(&q);
break;
case 4: clear(&q);
break;
default: printf("nWrong entry, Try again!!");
break;
}
}while(choice != 5);
return 0;
}
void menu()
{
printf("Press 1 to insert n");
printf("Press 2 to remove n");
printf("Press 3 to display n");
printf("Press 4 to clear n");
printf("Press 5 to quitn");
printf("nnEnter your choice:n");
}
void insert(q *qptr, float x)
{
if(qptr->rear == MAXQ-1)
qptr->rear=0;
else
qptr->rear++;
if(qptr->rear == qptr->front){
printf("Queue overflow");
exit(1);
}
qptr->items[qptr->rear]=x;
}
float remove(q *qptr)
{
if(qptr->front == qptr->rear){
printf("Queue underflow");
exit(1);
}
if(qptr->front == MAX-1)
qptr->front=0;
else
qptr->front++;
return qptr->items[qptr->front];
}
void clear(q *qptr)
{
qptr->front=MAX-1;
qptr->rear =MAX-1;
printf("Now the Queue is Emptyn");
}
void display(q *qptr)
{
int f,r;
f=qptr->front;
r=qptr->rear;
while(qptr->front !=qptr->rear){
if(qptr->front == MAX-1)
qptr->front =0;
else
qptr->front++;
printf("%5.2f",qptr->items[qptr->front]);
}
printf("n");
qptr->front=f;
qptr->rear=r;
}
typedef struct{
int front;
int rear;
int items[MAX]; /* Assume that MAX SIZE is
defined*/
}Q;
Case Study for Queue Search Element
int QSearch(Q *qptr, int search)
{
int pos = -1,f;
f=qptr->front;
while(qptr->front !=qptr->rear){
if(qptr->front == MAX-1)
qptr->front =0;
else
qptr->front++;
if(qptr->items[qptr->front] == search){
pos = qptr->front;
qptr->front = f;
return pos;
}
}
qptr->front=f;
return pos;
}
Implementation in C:
Case Study in Queue: Copy Reverse
typedef struct{
int front;
int rear;
int items[MAX]; /* Assume that MAX SIZE is
defined*/
}Q;
void QCopyReverse(Q *qptr1, Q *qptr2)
{
int r,item;
r=qptr1->rear;
do{
item = qptr1->items[qptr1->rear];
insert(qptr2,item);
if(qptr1->rear ==0)
qptr1->rear = MAX-1;
else
qptr1->rear --;
}while(qptr1->rear != qptr1->front);
qptr1->rear = r;
}
void insert(Q *qptr, int item)
{
if(qptr->rear == MAX-1)
qptr->rear=0;
else
qptr->rear++;
if(qptr->rear == qptr->front){
printf("Queue overflow");
exit(1);
}
qptr->items[qptr->rear]=item;
}
PRIORITY QUEUES
• The priority queue is a data structure in which internally
ordering of the elements determines the results of its basic
operations.
• An ascending priority queue is a combination of items with
priority into which items can be inserted arbitrarily and from
which only the smallest item can be removed. On the other
hand a descending priority queue allows only the largest item to
be removed.
• We use process.h for operation.
Priority QUEUE Operations
• Insertion
• The insertion in Priority queues is the same as in non-priority
queues.
• Deletion
• Deletion requires a search for the element of highest priority
and deletes the element with highest priority. The following
methods can be used for deletion/removal from a given
Priority Queue:
• An empty indicator replaces deleted elements.
• After each deletion elements can be moved up in the array
decrementing the rear.
• The array in the queue can be maintained as an ordered
circular array
Priority Queues Features
• Specialized data structure.
• Same as Queue, having front and rear.
• Items are deleted from the front.
• Items are ordered by priority value so that the item
with the lowest key (or highest) is always at the
front.
• Items are inserted in proper position to maintain
the order of ascending or descending.
Priority Queue : Class Diagram
+Queue()
+insert() : void
+remove() : long
+peekMin() : long
+isEmpty() : bool
+isFull() : bool
-maxSize : int
-queueArray [] : long
-nItems : int
PrioityQ
PriorityQApp
Interface1
Priority Queue Implementation in C
#define MAX 10 /* size of the queue items*/
typedef struct {
int front, rear;
int items[MAX];
}PQ;
int PQremove(Q *qptr)
{
int small, loc, f, i;
f=qptr->front;
if(qptr->front == qptr->rear){
printf("Queue underflow");
exit(1);
}
small = qptr->items[(qptr->front+1)%MAX];
loc = (qptr->front+1)%MAX;
(qptr->front++)%MAX; /* Circular increment*/
while(qptr->front != qptr->rear){
if(qptr->items[(qptr->front+1)%MAX] <small){
small = qptr->items[(qptr->front+1)%MAX];
loc = (qptr->front+1)%MAX;
}
qptr->front =(qptr->front+1)%MAX;
}
while(loc != qptr->rear){
qptr->items[loc] = qptr->items[(loc+1)%MAX];
(loc++)%MAX;
}
qptr->front=f;
if(qptr->rear == 0) /*Decrement rear after removing one
item*/
qptr->rear = MAX -1;
else
qptr->rear--;
return smallest;
}
Insert Operation of Priority Queue implemented in C
void insert(struct q *qptr, int item)
{
qptr->rear = (qptr->rear++)%MAX; /*Circular
increment*/
if(qptr->rear == qptr->front){
printf("Queue overflow");
exit(1);
}
qptr->items[qptr->rear]=item;
}
void enqueue(pqnode front, pqnode rear, element x)
{
pqnode temp =(pqnode) malloc(sizeof (queue));
if (IS_FULL(temp))
{
printf(“ The memory is fulln”);
exit(1);
}
temp->data = x;
temp->next= NULL;
if (front) { (rear) -> next= temp;}
else front = temp;
rear = temp;
Case Study: List-based Queue Implementation in C: insertion
int dequeue(pqnode front)
{
pqnode temp = front;
int x;
if (IS_EMPTY(front))
{
printf(“The queue is emptyn”);
exit(1);
}
x = temp->data;
front = temp->next;
free(temp);
return x;
}
Case Study: List-based Queue
Implementation in C: Deletion
Stack vs. Queue vs. Array
• Stack & Queue vs. Array
• Arrays are homogeneous data storage structures while
stacks and queues are derived ADT data storage
structures.
• Stack – ADT that allows push, peep and pop.
• Queue - ADT that allows enqueue and dequeue.
• In an array any item can be accessed through
index, while in these data structures access is
restricted.
Real world example of Queue
<?xml version = "1.0"?>
<!-- An author -->
<author>
<name gender = "male">
<first> Art </first>
<last> Gentleman </last>
</name>
</author>
THANK YOU
GIVE FEEDBACK

More Related Content

PPSX
Modules and packages in python
PPTX
Unit 3 stack
PPTX
Queue in Data Structure
PPTX
Class, object and inheritance in python
PDF
Datatypes in python
PPTX
Queues in C++
PPTX
Vector class in C++
PDF
Introduction to NumPy
Modules and packages in python
Unit 3 stack
Queue in Data Structure
Class, object and inheritance in python
Datatypes in python
Queues in C++
Vector class in C++
Introduction to NumPy

What's hot (20)

PDF
Infix to Prefix (Conversion, Evaluation, Code)
PPTX
Python-FileHandling.pptx
PPTX
Python Libraries and Modules
PPSX
Data Structure (Queue)
PPTX
Functions in python slide share
PDF
Python GUI
PPTX
Inheritance in java
PPTX
scope of python
PPTX
Python Functions
PDF
Python NumPy Tutorial | NumPy Array | Edureka
PPT
DATA STRUCTURES
PPTX
Introduction about Python by JanBask Training
PPTX
Map, Filter and Reduce In Python
PPTX
WHAT IS ABSTRACTION IN JAVA
PPTX
Context free grammar
PDF
PDF
Python libraries
PPSX
PPT
Queue data structure
Infix to Prefix (Conversion, Evaluation, Code)
Python-FileHandling.pptx
Python Libraries and Modules
Data Structure (Queue)
Functions in python slide share
Python GUI
Inheritance in java
scope of python
Python Functions
Python NumPy Tutorial | NumPy Array | Edureka
DATA STRUCTURES
Introduction about Python by JanBask Training
Map, Filter and Reduce In Python
WHAT IS ABSTRACTION IN JAVA
Context free grammar
Python libraries
Queue data structure
Ad

Similar to Queues & ITS TYPES (20)

PPTX
Unit 4 queue
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
PPTX
Unit – iv queue
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPTX
Queue(lecture8).pptx
PDF
05 queues
PPT
Lec-07 Queues.ppt queues introduction to queue
PPT
Queues in C++ detailed explanation and examples .ppt
PPTX
RPT_02_B_Queue presentation for FE students
PPT
Queue AS an ADT (Abstract Data Type)
PPT
10994103.ppt
PPTX
Queue and its operations
PPTX
4. Queues in Data Structure
PPTX
apllicationsof queffffffffffffffffffffffffffffue.pptx
PPTX
Queue - Data Structure - Notes
PPTX
Module 2 ppt.pptx
PPTX
My lectures circular queue
PPTX
queue_final.pptx
PPTX
Unit 4 queue
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
Unit – iv queue
Polynomialmotilalanehrunationalinstitute.pdf
Queue(lecture8).pptx
05 queues
Lec-07 Queues.ppt queues introduction to queue
Queues in C++ detailed explanation and examples .ppt
RPT_02_B_Queue presentation for FE students
Queue AS an ADT (Abstract Data Type)
10994103.ppt
Queue and its operations
4. Queues in Data Structure
apllicationsof queffffffffffffffffffffffffffffue.pptx
Queue - Data Structure - Notes
Module 2 ppt.pptx
My lectures circular queue
queue_final.pptx
Ad

More from Soumen Santra (20)

PDF
Basic and advance idea of Sed and Awk script with examples
PPT
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
PPTX
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
PPTX
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
PPT
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
DOC
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
PPT
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
PPT
Quick Sort
PPT
Merge sort
PPTX
A Novel Real Time Home Automation System with Google Assistance Technology
PPTX
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
PPTX
Java Basic PART I
PPT
Threads Advance in System Administration with Linux
PPTX
Frequency Division Multiplexing Access (FDMA)
PPTX
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
PPTX
Code-Division Multiple Access (CDMA)
PPTX
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
PPTX
Carrier-sense multiple access with collision avoidance CSMA/CA
PPTX
RFID (RADIO FREQUENCY IDENTIFICATION)
PPTX
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION
Basic and advance idea of Sed and Awk script with examples
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Cell hole identification in carcinogenic segment using Geodesic Methodology: ...
PPT_PAPERID 31_SOUMEN_SANTRA - ICCET23.pptx
Basic networking hardware: Switch : Router : Hub : Bridge : Gateway : Bus : C...
Traveling salesman problem: Game Scheduling Problem Solution: Ant Colony Opti...
Optimization techniques: Ant Colony Optimization: Bee Colony Optimization: Tr...
Quick Sort
Merge sort
A Novel Real Time Home Automation System with Google Assistance Technology
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java Basic PART I
Threads Advance in System Administration with Linux
Frequency Division Multiplexing Access (FDMA)
Carrier Sense Multiple Access With Collision Detection (CSMA/CD) Details : Me...
Code-Division Multiple Access (CDMA)
PURE ALOHA : MEDIUM ACCESS CONTROL PROTOCOL (MAC): Definition : Types : Details
Carrier-sense multiple access with collision avoidance CSMA/CA
RFID (RADIO FREQUENCY IDENTIFICATION)
SPACE DIVISION MULTIPLE ACCESS (SDMA) SATELLITE COMMUNICATION

Recently uploaded (20)

PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PPTX
wireless networks, mobile computing.pptx
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
mechattonicsand iotwith sensor and actuator
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Feature types and data preprocessing steps
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PPTX
Software Engineering and software moduleing
PPTX
CyberSecurity Mobile and Wireless Devices
PPTX
Measurement Uncertainty and Measurement System analysis
"Array and Linked List in Data Structures with Types, Operations, Implementat...
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
August -2025_Top10 Read_Articles_ijait.pdf
wireless networks, mobile computing.pptx
Computer System Architecture 3rd Edition-M Morris Mano.pdf
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
20250617 - IR - Global Guide for HR - 51 pages.pdf
Soil Improvement Techniques Note - Rabbi
mechattonicsand iotwith sensor and actuator
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Amdahl’s law is explained in the above power point presentations
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Feature types and data preprocessing steps
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
Software Engineering and software moduleing
CyberSecurity Mobile and Wireless Devices
Measurement Uncertainty and Measurement System analysis

Queues & ITS TYPES

  • 2. DEFINITION OF QUEUE • A Queue is an ordered collection ADT(Abstract Data Type) of items from which items may be deleted at one end (FRONT) and into which items may be inserted at the other end (REAR). • The first element inserted into the queue is the first element to be removed. The queue is sometimes called a FIFO (first-in first-out) list as opposed to the stack, which is a LIFO (last-in first-out).
  • 3. Queue • Stores a set of elements in a particular order • Stack principle: FIRST IN FIRST OUT • = FIFO • It means: the first element inserted is the first one to be removed. • The first one in line is the first one to be served • Example
  • 4. Queue Applications • Real life examples • Waiting in line for reservation • Waiting on hold for tech support • Applications related to Computer Science • Threads scheduling & synchronization • Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
  • 6. front rear Q[0] Q[1] Q[2] Q[3] Comments -1 -1 -1 -1 0 1 -1 0 1 2 2 2 J1 J1 J2 J1 J2 J3 J2 J3 J3 queue is empty Job 1 is added Job 2 is added Job 3 is added Job 1 is deleted Job 2 is deleted Applications: Job Scheduling
  • 7. Queue items[MAX-1] . . . . . . items[2] C items[1] B items[0] A Front=0 Rear=2
  • 8. Declaration of a Queue # define MAX 500 /* size of the queue items*/ typedef struct { int front; int rear; int items[MAX]; }QUEUE;
  • 9. QUEUE OPERATIONS • Initialize the queue • Insert to the rear of the queue • Remove (Delete) from the front of the queue • Is the Queue Empty • Is the Queue Full • What is the size of the Queue
  • 10. Queue Example: Class Diagram Concept +Queue() +insert() : void +remove() : long +peekFront() : long +isEmpty() : bool +isFull() : bool +size() : int -maxSize : int -queueArray [] : long -front : int -rear : int -nItems : int Queue QueueApp Interface1
  • 11. INITIALIZE THE QUEUE items[MAX-1] . . . . . items[1] items[0] front=0 rear=-1 •The queue is initialized by having the rear set to -1, and front set to 0. Let us assume that maximum number of the element we have in a queue is MAX size elements as shown below.
  • 12. insert(&Queue, ‘A’) Method • an item (A) is inserted at the Rear of the queue items[MAX-1] . . . . items[3] items[2] items[1] items[0] A Front=0, Rear=0
  • 13. insert(&Queue, ‘B’) Method • A new item (B) is inserted at the Rear of the queue items[MAX-1] . . . . items[3] items[2] items[1] B Rear=1 items[0] A Front=0
  • 14. insert(&Queue, ‘C’) Method • A new item (C) is inserted at the Rear of the queue items[MAX-1] . . . . items[3] items[2] C Rear=2 items[1] B items[0] A Front=0
  • 15. insert(&Queue, ‘D’) Method • A new item (D) is inserted at the Rear of the queue items[MAX-1] . . . . items[3] D Rear=3 items[2] C items[1] B items[0] A Front=0
  • 16. Array-based Queue Implementation • As with the array-based stack implementation, the array is of fixed size • A queue of maximum N elements • Slightly more complicated • Need to maintain CONTROL of both front and rear Implementation 1 Implementation 2
  • 17. Insert Operation of Queue using pointer void insert(QUEUE *qptr, char x) { if(qptr->rear == MAX-1) { printf("Queue is full!"); exit(1); } else { qptr->rear++; qptr->items[qptr->rear]=x; } }
  • 18. char remove(&Queue) Method • an item (A) is removed (deleted) from the Front of the queue & return it as per datatype. items[MAX-1] . . . . items[3] D Rear=3 items[2] C items[1] B Front=1 items[0] A
  • 19. char remove(&Queue) Method • Remove two items from the front of the queue. items[MAX-1] . . . . items[3] D Rear=3 items[2] C Front=2 items[1] B items[0] A
  • 20. char remove(&Queue) Method • Remove two items from the front of the queue. items[MAX-1] . . . . items[3] D Front=Rear=3 items[2] C items[1] B items[0] A
  • 21. char remove(&Queue) Method • Remove one more item from the front of the queue. items[MAX-1] . . items[4] Front=4 items[3] D Rear=3 items[2] C items[1] B items[0] A
  • 22. Remove Operation implementation in c char remove(struct queue *qptr) { char p; if(qptr->front > qptr->rear){ printf("Queue is empty"); exit(1); } else {p=qptr->items[qptr->front]; qptr->front++; return p; } }
  • 23. INSERT / REMOVE ITEMS : Drawback of Linear Queue • Assume that the rear= MAX-1 •What happens if we want to insert a new item into the queue? items[MAX-1] X rear=MAX-1 . . . . items[3] D front=3 items[2] C items[1] B items[0] A
  • 24. INSERT / REMOVE ITEMS • What happens if we want to insert a new item F into the queue? • Although there is some empty space, the queue is full. • One of the methods to overcome this problem is to shift all the items to occupy the location of deleted item.
  • 25. REMOVE ITEM items[MAX-1] . . . . items[3] D Rear=3 items[2] C items[1] B Front=1 items[0] A
  • 26. REMOVE ITEM : Example items[MAX-1] . . . . items[3] D Rear=3 items[2] C items[1] B Front=1 items[0] B
  • 27. REMOVE ITEM : Example items[MAX-1] . . . . items[3] D Rear=3 items[2] C items[1] C items[0] B
  • 28. REMOVE ITEM : Example items[MAX-1] . . . . items[3] D Rear=3 items[2] D items[1] C items[0] B
  • 29. REMOVE ITEM : Example items[MAX-1] . . . . items[3] D items[2] D Rear=2 items[1] C items[0] B
  • 30. Modified Remove Operation implementation in C char remove(struct queue *qptr) { char p; int i; if(qptr->front > qptr->rear){ printf("Queue is empty"); exit(1); } else {p=qptr->items[qptr->front]; for(i=1;i<=qptr->rear;i++) qptr->items[i-1]=qptr->items[i]; qptr->rear-- return p; } }
  • 31. INSERT / REMOVE ITEMS • Since all the items in the queue are required to shift when an item is deleted, this method is not preferred. • The other method is circular queue. • When rear = MAX-1, the next element is entered at items[0] in case that spot is free.
  • 32. How to Initialize the queue items[6] front=rear=6 items[5] items[4] items[3] items[2] items[1] items[0]
  • 33. Insert operation in Circular Queue items[6] front=6 items[5] items[4] items[3] items[2] items[1] items[0] 1 rear=0  Insert 1,2,3 to the rear of the queue.
  • 34. Insert operation in Circular Queue items[6] front=6 items[5] items[4] items[3] items[2] items[1] 2 rear=1 items[0] 1  Insert 1,2,3 to the rear of the queue.
  • 35. Insert operation in Circular Queue • Insert 1,2,3 to the rear of the queue. items[6] front=6 items[5] items[4] items[3] items[2] 3 rear=2 items[1] 2 items[0] 1
  • 36. Remove items from circular queue • Remove two items from the queue. items[6] items[5] items[4] items[3] items[2] 3 rear=2 items[1] 2 items[0] 1 front=0
  • 37. Remove items from circular queue • Remove two items from the queue. items[6] items[5] items[4] items[3] items[2] 3 rear=2 items[1] 2 front=1 items[0] 1
  • 38. Remove items from circular queue • Remove one more item from the queue. items[6] items[5] items[4] items[3] items[2] 3 rear=front=2 items[1] 2 items[0] 1
  • 39. Insert operation in Circular Queue: Overcome Drawback of Linear Queue • Insert 4,5,6,7 to the queue. items[6] 7 rear=6 items[5] 6 items[4] 5 items[3] 4 items[2] 3 front=2 items[1] 2 items[0] 1
  • 40. Insert operation in Circular Queue: Overcome Drawback of Linear Queue • Insert 8 and 9 to the queue. items[6] 7 items[5] 6 items[4] 5 items[3] 4 items[2] 3 front=2 items[1] 2 items[0] 8 rear=0
  • 41. Insert operation in Circular Queue: Overcome Drawback of Linear Queue • Insert 8 and 9 to the queue. items[6] 7 items[5] 6 items[4] 5 items[3] 4 items[2] 3 front=2 items[1] 9 items[0] 8 rear=0
  • 42. Insert operation in Circular Queue: Overcome Drawback of Linear Queue • Insert 10 to the queue. items[6] 7 items[5] 6 items[4] 5 items[3] 4 items[2] ?? front=rear=2 items[1] 9 items[0] 8
  • 43. Implementation of a Circular Queue in C #define MAX 10 /* size of the queue items*/ typedef struct { int front; int rear; int items[MAX]; }QUEUE; QUEUE Q; Q.front = MAX-1; Q.rear= MAX-1;
  • 44. EMPTY QUEUE [2] [3] [2] [3] [1] [4] [1] [4] [0] [5] [0] [5] front = 0 front = 0 rear = 0 rear = 3 B A C Implementation 2:Cyclic View Can be seen as a circular queue
  • 45. FULL QUEUE FULL QUEUE [2] [3] [2] [3] [1] [4][1] [4] [0] [5] [0] [5] front =0 rear = 5 front =4 rear =3 B C A D E F E G H I Advantage of Circular Queue: How to test when queue is UNDERFLOW? How to test when queue is OVERFLOW?
  • 46. Implementation of Insert Operation for Circular Queue in C void insert(QUEUE *qptr, char x) { if(qptr->rear == MAX-1) qptr->rear=0; else qptr->rear++; /* or qptr->rear=(qptr->rear+1)%MAX) */ if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }
  • 47. Implementation of Remove Operation for Circular Queue in C char remove(struct queue *qptr) { if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } if(qptr->front == MAX-1) qptr->front=0; else qptr->front++; return qptr->items[qptr->front]; }
  • 48. #include <stdlib.h> #include <stdio.h> #define MAX 50 #define TRUE 1 #define FALSE 0 typedef struct { int items[MAX]; int front , rear ; } Q; void queinsert( Q * , int); int quedelete(Q *); int isempty(Q *); EXAMPLE in C
  • 49. int main() { char operation; int x; Q q; q.front = q.rear = MAX - 1; do { printf("%sn",“Queue Operation type I(Insert) D(Delete) or E(Exit) "); scanf("n%c",&choice); switch (choice) { case 'I’: printf("%sn","Insert an element"); scanf("n%d",&x); qinsert(&q , x); break; case 'D’: x=qdelete(&q); printf("n %d is deleted n",x); break; default: printf(“Incorrect Operation typen”); break; } } while (choice != 'E’&&choice!='e'); return 0; }
  • 50. int isempty(Q *qptr) { return((qptr->front == qptr->rear) ? TRUE : FALSE); } int qdelete(Q *qptr) { if (empty(qptr)) { printf("Queue underflow "); exit(1); } qptr->front=(qptr->front+1)%(MAX) return(qptr->items[qptr->front]); } void qinsert(Q *qptr , int x) { /* make room for new element */ printf("n %d is inserted n",x); qptr->rear=(qptr->rear+1)%(MAX) if (qptr->rear == qptr->front) { printf("Queue overflow"); exit(1); } qptr->items[qptr->rear] = x; return; }
  • 51. #include<stdio.h> #include<stdlib.h> #define MAX 5 /* size of the queue items*/ typedef struct{ int front; int rear; float items[MAX]; }Q; void qinsert(Q *, float); float qdelete(Q *); void display( Q *); void clear( Q *); void menu();
  • 52. int main() { int choice; float x; Q q; q.front = M-1; q.rear= MAX-1; do{ menu(); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the item to be inserted:"); scanf("%f",&x); insert(&q,x); break; case 2: x=remove(&q); printf("nRemoved item:%f",x); break; case 3: display(&q); break; case 4: clear(&q); break; default: printf("nWrong entry, Try again!!"); break; } }while(choice != 5); return 0; }
  • 53. void menu() { printf("Press 1 to insert n"); printf("Press 2 to remove n"); printf("Press 3 to display n"); printf("Press 4 to clear n"); printf("Press 5 to quitn"); printf("nnEnter your choice:n"); } void insert(q *qptr, float x) { if(qptr->rear == MAXQ-1) qptr->rear=0; else qptr->rear++; if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=x; }
  • 54. float remove(q *qptr) { if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } if(qptr->front == MAX-1) qptr->front=0; else qptr->front++; return qptr->items[qptr->front]; } void clear(q *qptr) { qptr->front=MAX-1; qptr->rear =MAX-1; printf("Now the Queue is Emptyn"); }
  • 55. void display(q *qptr) { int f,r; f=qptr->front; r=qptr->rear; while(qptr->front !=qptr->rear){ if(qptr->front == MAX-1) qptr->front =0; else qptr->front++; printf("%5.2f",qptr->items[qptr->front]); } printf("n"); qptr->front=f; qptr->rear=r; }
  • 56. typedef struct{ int front; int rear; int items[MAX]; /* Assume that MAX SIZE is defined*/ }Q; Case Study for Queue Search Element
  • 57. int QSearch(Q *qptr, int search) { int pos = -1,f; f=qptr->front; while(qptr->front !=qptr->rear){ if(qptr->front == MAX-1) qptr->front =0; else qptr->front++; if(qptr->items[qptr->front] == search){ pos = qptr->front; qptr->front = f; return pos; } } qptr->front=f; return pos; } Implementation in C:
  • 58. Case Study in Queue: Copy Reverse typedef struct{ int front; int rear; int items[MAX]; /* Assume that MAX SIZE is defined*/ }Q;
  • 59. void QCopyReverse(Q *qptr1, Q *qptr2) { int r,item; r=qptr1->rear; do{ item = qptr1->items[qptr1->rear]; insert(qptr2,item); if(qptr1->rear ==0) qptr1->rear = MAX-1; else qptr1->rear --; }while(qptr1->rear != qptr1->front); qptr1->rear = r; } void insert(Q *qptr, int item) { if(qptr->rear == MAX-1) qptr->rear=0; else qptr->rear++; if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=item; }
  • 60. PRIORITY QUEUES • The priority queue is a data structure in which internally ordering of the elements determines the results of its basic operations. • An ascending priority queue is a combination of items with priority into which items can be inserted arbitrarily and from which only the smallest item can be removed. On the other hand a descending priority queue allows only the largest item to be removed. • We use process.h for operation.
  • 61. Priority QUEUE Operations • Insertion • The insertion in Priority queues is the same as in non-priority queues. • Deletion • Deletion requires a search for the element of highest priority and deletes the element with highest priority. The following methods can be used for deletion/removal from a given Priority Queue: • An empty indicator replaces deleted elements. • After each deletion elements can be moved up in the array decrementing the rear. • The array in the queue can be maintained as an ordered circular array
  • 62. Priority Queues Features • Specialized data structure. • Same as Queue, having front and rear. • Items are deleted from the front. • Items are ordered by priority value so that the item with the lowest key (or highest) is always at the front. • Items are inserted in proper position to maintain the order of ascending or descending.
  • 63. Priority Queue : Class Diagram +Queue() +insert() : void +remove() : long +peekMin() : long +isEmpty() : bool +isFull() : bool -maxSize : int -queueArray [] : long -nItems : int PrioityQ PriorityQApp Interface1
  • 64. Priority Queue Implementation in C #define MAX 10 /* size of the queue items*/ typedef struct { int front, rear; int items[MAX]; }PQ;
  • 65. int PQremove(Q *qptr) { int small, loc, f, i; f=qptr->front; if(qptr->front == qptr->rear){ printf("Queue underflow"); exit(1); } small = qptr->items[(qptr->front+1)%MAX]; loc = (qptr->front+1)%MAX; (qptr->front++)%MAX; /* Circular increment*/ while(qptr->front != qptr->rear){ if(qptr->items[(qptr->front+1)%MAX] <small){ small = qptr->items[(qptr->front+1)%MAX]; loc = (qptr->front+1)%MAX; } qptr->front =(qptr->front+1)%MAX; }
  • 66. while(loc != qptr->rear){ qptr->items[loc] = qptr->items[(loc+1)%MAX]; (loc++)%MAX; } qptr->front=f; if(qptr->rear == 0) /*Decrement rear after removing one item*/ qptr->rear = MAX -1; else qptr->rear--; return smallest; }
  • 67. Insert Operation of Priority Queue implemented in C void insert(struct q *qptr, int item) { qptr->rear = (qptr->rear++)%MAX; /*Circular increment*/ if(qptr->rear == qptr->front){ printf("Queue overflow"); exit(1); } qptr->items[qptr->rear]=item; }
  • 68. void enqueue(pqnode front, pqnode rear, element x) { pqnode temp =(pqnode) malloc(sizeof (queue)); if (IS_FULL(temp)) { printf(“ The memory is fulln”); exit(1); } temp->data = x; temp->next= NULL; if (front) { (rear) -> next= temp;} else front = temp; rear = temp; Case Study: List-based Queue Implementation in C: insertion
  • 69. int dequeue(pqnode front) { pqnode temp = front; int x; if (IS_EMPTY(front)) { printf(“The queue is emptyn”); exit(1); } x = temp->data; front = temp->next; free(temp); return x; } Case Study: List-based Queue Implementation in C: Deletion
  • 70. Stack vs. Queue vs. Array • Stack & Queue vs. Array • Arrays are homogeneous data storage structures while stacks and queues are derived ADT data storage structures. • Stack – ADT that allows push, peep and pop. • Queue - ADT that allows enqueue and dequeue. • In an array any item can be accessed through index, while in these data structures access is restricted.
  • 71. Real world example of Queue <?xml version = "1.0"?> <!-- An author --> <author> <name gender = "male"> <first> Art </first> <last> Gentleman </last> </name> </author>