SlideShare a Scribd company logo
QUEUES
Unit 1.2
Intro
2
Movie Hall Ticketing
One Way Traffic
Intro
• Queue is a linear list of elements in which deletion of an element can
take place at one end, called the front and insertion can take place at
the other end, called the rear.
• The first element in a queue will be the first one to be removed from
the list.
• Queues are also called FIFO (First In First Out) i.e. the data
item stored first will be accessed first
3
Queue Representation
• In queue, we access both ends for different reasons
4
Applications of queue
• Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.
• In real life, Call Center phone systems will use Queues, to hold
people calling them in an order, until a service representative is free.
• Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive, First come first served.
5
The queue as an ADT
• A queue q of type T is a finite sequence of elements with the
operations
• MakeEmpty(q): To make q as an empty queue
• IsEmpty(q): To check whether the queue q is empty. Return true if q is
empty, return false otherwise.
• IsFull(q): To check whether the queue q is full. Return true in q is full,
return false otherwise.
• Enqueue(q, x): To insert an item x at the rear of the queue, if and only if
q is not full.
• Dequeue(q): To delete an item from the front of the queue q if and only if
q is not empty.
• Traverse (q): To read entire queue that is display the content of the queue.
6
Enqueue Operation
• Queue maintains two data pointers, front and rear
• The following steps should be taken to enqueue(insert) data into
queue –
• Step 1 – Check if queue is full
• Step 2 – if queue is full
produce overflow error and exit
else
increment rear pointer to point next empty space
and add data element to the queue location, where rear is
pointing
• Step 3 - return success
7
8
Implementation of enqueue()
int enqueue(int data) {
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
}
9
Dequeue Operation
• Accessing data from queue is a process of two steps
• Access the data from where front is pointing
• And remove the data after access
• The following steps are taken to perform dequeue operation
• Step 1 – Check if queue is empty
• Step 2 – if queue is empty
produce underflow error and exit
else
access data where front is pointing, increment front pointer
to point next available data element
• Step 3 – return success.
10
11
Implementation of dequeue()
int dequeue() {
if(isempty()) {
return 0;
}
int data = queue[front];
front = front + 1;
return data;
}
12
Implementation of queue
• There are two techniques for implementing the queue:
• Array implementation of queue (static memory allocation)
• Linear array implementation (Linear Queue)
• Circular array Implementation (Circular queue)
• Linked list implementation of queue (dynamic memory allocation)
13
Array implementation of queue
• The easies way of implementing a queue is by using an Array.
• Initially head(FRONT) and the tail(REAR) of the queue points at the
first index of the array.
• As we add elements to the queue, we can either move tail before
adding another item or we can move the tail after adding the item,
while the head remains at the first index.
14
Linear Queue
15
Linear Queue
Insertion of an item in queue Deletion of an item from queue
1. Initialize front=0 and rear=-1
if rear>=MAXSIZE-1
print “queue overflow” and return
else
set rear=rear+1
queue[rear]=item
2. end
1. if rear<front
print “queue is empty” and return
else
item=queue[front++]
2. end
16
Problems with Linear queue implementation
• Both rear and front indices are increased but never decreased.
• As items are removed from the queue, the storage space at the
beginning of the array is discarded and never used again.
• Wastage of the space is the main problem with linear queue which is
illustrated by the following example.
17
Circular queue
• A queue where the start and end of the queue are joined together.
• A circular queue is one in which the insertion of a new element is
done at very first location of the queue if the last location of the queue
is full.
18
19
Circular queue
isFull() IsEmpty
- If HEAD == (Tail % MAX) + 1
Then Full <- True;
Else Full <- False;
- If they have caught up to each other, then it’s full
- If Head ==Tail
Then Full <- True;
Else Full <- False;
20
Circular queue
Enqueue operation Dequeue operation
Step 1. start
Step 2. if (front == (rear+1)%max)
Print error “circular queue overflow “
Step 3. else{
rear = (rear+1)%max
Q[rear] = element;
}
Step 4. stop
Step 1. start
Step 2. if isEmpty() == True
Print error “Queue is Empty“
Step 3. else{
element = Q[front]
front = (front + 1) % max
}
Step 4. stop
21
References
• https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queu
• http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm
• http://www.studytonight.com/data-structures/queue-data-structure
• https://www.youtube.com/watch?v=g9su-lnW2Ks
• https://www.youtube.com/watch?v=ia__kyuwGag
• https://www.quora.com/What-is-a-circular-queue
• http://basicdatastructures.blogspot.com/2007/12/circular-queue-data-structure.html
• http://btechsmartclass.com/DS/U2_T10.html
• http://scanftree.com/Data_Structure/circular-queue
• http://btechsmartclass.com/DS/U2_T10.html
22

More Related Content

What's hot

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
NalinNishant3
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
Tirthika Bandi
 
Queues
QueuesQueues
Queue
QueueQueue
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
stack presentation
stack presentationstack presentation
Data structures using c
Data structures using cData structures using c
Data structures using c
Prof. Dr. K. Adisesha
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Rashmi R Upadhya
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Pranay Neema
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
Circular queue
Circular queueCircular queue

What's hot (20)

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Queues
QueuesQueues
Queues
 
Queue
QueueQueue
Queue
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
stack presentation
stack presentationstack presentation
stack presentation
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Circular queue
Circular queueCircular queue
Circular queue
 

Similar to Queue AS an ADT (Abstract Data Type)

@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
Ddushb
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
Dr.Shweta
 
Queue
QueueQueue
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Queues
Queues Queues
Queues
nidhisatija1
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
Queue
QueueQueue
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Poulami Das Akuli
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
Dr.Umadevi V
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
Muhammad Hammad Waseem
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
iqbalphy1
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptx
LakshmiSamivel
 
Queues
QueuesQueues

Similar to Queue AS an ADT (Abstract Data Type) (20)

@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Queue
QueueQueue
Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queues
Queues Queues
Queues
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queue
QueueQueue
Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Basic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptxBasic Queue Operation in DataStructure.pptx
Basic Queue Operation in DataStructure.pptx
 
Queues
QueuesQueues
Queues
 

More from Self-Employed

Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Ds lec 5_chap4
Ds lec 5_chap4Ds lec 5_chap4
Ds lec 5_chap4
Self-Employed
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
Self-Employed
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
Self-Employed
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
Self-Employed
 
8086 architecture
8086 architecture8086 architecture
8086 architecture
Self-Employed
 

More from Self-Employed (7)

Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Ds lec 5_chap4
Ds lec 5_chap4Ds lec 5_chap4
Ds lec 5_chap4
 
Discrete mathematics counting and logic relation
Discrete mathematics counting and logic relationDiscrete mathematics counting and logic relation
Discrete mathematics counting and logic relation
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
2.2 inverse of a matrix
2.2 inverse of a matrix2.2 inverse of a matrix
2.2 inverse of a matrix
 
8086 architecture
8086 architecture8086 architecture
8086 architecture
 

Recently uploaded

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

Queue AS an ADT (Abstract Data Type)

  • 3. Intro • Queue is a linear list of elements in which deletion of an element can take place at one end, called the front and insertion can take place at the other end, called the rear. • The first element in a queue will be the first one to be removed from the list. • Queues are also called FIFO (First In First Out) i.e. the data item stored first will be accessed first 3
  • 4. Queue Representation • In queue, we access both ends for different reasons 4
  • 5. Applications of queue • Serving requests on a single shared resource, like a printer, CPU task scheduling etc. • In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a service representative is free. • Handling of interrupts in real-time systems. The interrupts are handled in the same order as they arrive, First come first served. 5
  • 6. The queue as an ADT • A queue q of type T is a finite sequence of elements with the operations • MakeEmpty(q): To make q as an empty queue • IsEmpty(q): To check whether the queue q is empty. Return true if q is empty, return false otherwise. • IsFull(q): To check whether the queue q is full. Return true in q is full, return false otherwise. • Enqueue(q, x): To insert an item x at the rear of the queue, if and only if q is not full. • Dequeue(q): To delete an item from the front of the queue q if and only if q is not empty. • Traverse (q): To read entire queue that is display the content of the queue. 6
  • 7. Enqueue Operation • Queue maintains two data pointers, front and rear • The following steps should be taken to enqueue(insert) data into queue – • Step 1 – Check if queue is full • Step 2 – if queue is full produce overflow error and exit else increment rear pointer to point next empty space and add data element to the queue location, where rear is pointing • Step 3 - return success 7
  • 8. 8
  • 9. Implementation of enqueue() int enqueue(int data) { if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; } 9
  • 10. Dequeue Operation • Accessing data from queue is a process of two steps • Access the data from where front is pointing • And remove the data after access • The following steps are taken to perform dequeue operation • Step 1 – Check if queue is empty • Step 2 – if queue is empty produce underflow error and exit else access data where front is pointing, increment front pointer to point next available data element • Step 3 – return success. 10
  • 11. 11
  • 12. Implementation of dequeue() int dequeue() { if(isempty()) { return 0; } int data = queue[front]; front = front + 1; return data; } 12
  • 13. Implementation of queue • There are two techniques for implementing the queue: • Array implementation of queue (static memory allocation) • Linear array implementation (Linear Queue) • Circular array Implementation (Circular queue) • Linked list implementation of queue (dynamic memory allocation) 13
  • 14. Array implementation of queue • The easies way of implementing a queue is by using an Array. • Initially head(FRONT) and the tail(REAR) of the queue points at the first index of the array. • As we add elements to the queue, we can either move tail before adding another item or we can move the tail after adding the item, while the head remains at the first index. 14
  • 16. Linear Queue Insertion of an item in queue Deletion of an item from queue 1. Initialize front=0 and rear=-1 if rear>=MAXSIZE-1 print “queue overflow” and return else set rear=rear+1 queue[rear]=item 2. end 1. if rear<front print “queue is empty” and return else item=queue[front++] 2. end 16
  • 17. Problems with Linear queue implementation • Both rear and front indices are increased but never decreased. • As items are removed from the queue, the storage space at the beginning of the array is discarded and never used again. • Wastage of the space is the main problem with linear queue which is illustrated by the following example. 17
  • 18. Circular queue • A queue where the start and end of the queue are joined together. • A circular queue is one in which the insertion of a new element is done at very first location of the queue if the last location of the queue is full. 18
  • 19. 19
  • 20. Circular queue isFull() IsEmpty - If HEAD == (Tail % MAX) + 1 Then Full <- True; Else Full <- False; - If they have caught up to each other, then it’s full - If Head ==Tail Then Full <- True; Else Full <- False; 20
  • 21. Circular queue Enqueue operation Dequeue operation Step 1. start Step 2. if (front == (rear+1)%max) Print error “circular queue overflow “ Step 3. else{ rear = (rear+1)%max Q[rear] = element; } Step 4. stop Step 1. start Step 2. if isEmpty() == True Print error “Queue is Empty“ Step 3. else{ element = Q[front] front = (front + 1) % max } Step 4. stop 21
  • 22. References • https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queu • http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm • http://www.studytonight.com/data-structures/queue-data-structure • https://www.youtube.com/watch?v=g9su-lnW2Ks • https://www.youtube.com/watch?v=ia__kyuwGag • https://www.quora.com/What-is-a-circular-queue • http://basicdatastructures.blogspot.com/2007/12/circular-queue-data-structure.html • http://btechsmartclass.com/DS/U2_T10.html • http://scanftree.com/Data_Structure/circular-queue • http://btechsmartclass.com/DS/U2_T10.html 22