SlideShare a Scribd company logo
1 of 36
GROUP MEMBERS
Names Roll No.
Fatima Saqib 39
Bilal Younus 10
Areesha Butt 58
Muhammad Fahim 42
Poshma Kiyani 02
Abdur Rehman 41
Presentation Topic
Queue
QUEUE
A queue is two ended data structure in which
items can be inserted from one end and taken
out from the other end. Therefore , the first
item inserted into queue is the first item to be
taken out from the queue. This property is
called First in First out [FIFO].
e.g: the picture given below:
Queue Example
● A queue of people in the bank who are trying to
submit utility bills.
● A line of vehicle on the motorway toll plaza.
● Consider a shared printer in a computer
laboratory. Student can send their print request to
the printer and each print request goto queue and
serve one by one.
● Computer input operations performing through
mouse and keyboard are also recorded into queue
and serve on the basis of FIFO.
QUEUE example
Deque stands for double-end queue
A data structure in which elements can be
added or deleted at either the front or rear.
But no changes can be made in the list
Deque is generalization of both stack and
queue.
Deque
Variations In Deque:
There are two variations of a deque.
 Input Restricted Deque
 Output Restricted Deque
An input restricted deque restricts the
insertion of elements at one end only, but
the deletion of elements can perform at
both the ends.
Input Restricted Deque
An output restricted queue, restricts the
deletion of elements at one end only, and
allows insertion to be done at both the ends
of deque
Output Restricted Deque
Possibilities
The two possibilities that must be considered
while inserting or deleting elements into the
queue are :
When an attempt is made to insert an element
into a deque which is already full, an overflow
occurs.
When an attempt is made to delete an
element from a deque which is empty,
underflow occurs
Deque
The Queue Operations
• A queue is like a line of people
waiting for a bank teller. The queue
has a front and a rear.
$ $
FrontRear
The Queue Operations
• New people must enter the queue at the rear.
• The C++ queue class calls this a push,
although it is usually called an enqueue
operation.
$ $
Front
Rear
The Queue Operations
• When an item is taken from the queue, it
always comes from the front. The C++ queue
calls this a pop, although it is usually called a
dequeue operation.
$ $
Front
Rear
Function of Queue
 REAR
 FRONT
 APPEND
 SERVE
 IsEmpty
 IsFull
Rear
Rear is the tail/bottom of the queue. The
entry at rear is the most recently arrived item
and must wait until the other entries are
present in the queue.
Insert
(Enqueue)
Remove
(Dequeu)
rearfront
FRONT
Front is the position from where items can be
taken out from queue. It is also termed as
head or top of the queue. The entry at the
front position is ready to be served
Insert
(Enqueue)
Remove
(Dequeu)
rearfront
APPEND
The operation to add an item at the rear
position of the queue is termed as append or
enqueue. We can append items at the rear
position of the queue until the queue is not
full. The rear points towards the position
where new item has been appended.
17 23 97 44 333After insertion:
17 23 97 44Initial queue:
Serve The operation to remove an item from
the front position of the queue is termed as
serve or dequeue. We can serve the items in
the queue until the queue is not empty. The
front points towards the position from where
the available item can be severed
SERVE
Queue is consider empty when there is
no item on front of the queue. IsEmpty
operation checks for this condition and
return true if the queue is empty else
return false.
IsEmpty
0 1 2 3 4 5 6 7
myQueue:
If no element can be appended at rear of
the queue then queue consider as full.
When rear of the queue reaches this limit,
then queue is full and the IsFull operation
return true else return false
IsFull
44 55 66 77 88 11 22 33
0 1 2 3 4 5 6 7
myQueue:
Array Implementation of Queue
• When enqueuing, the front index is always fixed and
the rear index moves forward in the array.
• There are several different algorithms to implement
Enqueue and Dequeue
front
rear
Enqueue(3)
3
front
rear
Enqueue(6)
3 6
front
rear
Enqueue(9)
3 6 9
Queue Implementation of Array
• When enqueuing, the front index is always fixed
and the rear index moves forward in the array.
• When dequeuing, the element at the front of the
queue is removed. Move all the elements after it
by one position.
Dequeue()
front
rear
6 9
Dequeue() Dequeue()
front
rear
9
rear = -1
front
Queue Implementation of Array
• Better way
• When an item is enqueued, make the rear
index move forward.
• When an item is dequeued, the front index
moves by one element towards the back of
the queue (thus removing the front item).
Array Implementation....
• A queue can be implemented with an array.
For example, this queue contains the integers
4 (at the front), 8 and 6 (at the rear).
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . ..
An array of integers to
implement a queue of
integers
4 8 6
Array Implementation....
• The easiest implementation also
keeps track of the number of items in
the queue and the index of the first
element (at the front of the queue),
the last element (at the rear).
[ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . .
4 8 6
size3
first0
last2
..Array implementation of queues
• A queue is a first in, first out (FIFO) data structure
• This is accomplished by inserting at one end (the
rear) and deleting from the other (the front)
• To insert: put new element in location 4, and set
rear to 4
• To delete: take element from location 0, and set
front to 1
28
17 23 97 44
0 1 2 3 4 5 6 7
myQueue:
rear = 3front = 0
…Array implementation of queues
• Notice how the array contents “crawl” to the right as elements are
inserted and deleted
• This will be a problem after a while!
29
17 23 97 44 333After insertion:
23 97 44 333After deletion:
rear = 4front = 1
17 23 97 44Initial queue:
rear = 3front = 0
Representation of queue by program
• How to insert Value
• How to delete value
• How to print value
• Complete Program Example
To Insert Value
Algorithm:
R=F=-1
IF R >= SIZE THEN
PRINT ‘’Overflow’’
RETURN
ELSE {
R=R+1
[Insert Item]
Q[R]=‘’item’’
IF F=-1 THEN
F=0
}
END IF
Algorithm:
If ( FRONT = -1 ) then
print "Queue is empty"
Exit
Else
ITEM = Que [ FRONT ]
If ( FRONT = REAR )
REAR = -1
FRONT = -1
Else
FRONT = FRONT + 1
Stop
To Delete Value
Main body of program
Class of the program
Main body of program Output
ThanksYes, finished

More Related Content

What's hot

My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
Senthil Kumar
 

What's hot (19)

QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
 
Queue
QueueQueue
Queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack
StackStack
Stack
 
Team 6
Team 6Team 6
Team 6
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
Queues
QueuesQueues
Queues
 
Queues
QueuesQueues
Queues
 
Circular queue
Circular queueCircular queue
Circular queue
 
Queue
QueueQueue
Queue
 

Viewers also liked

Approaches to Model Transformation Reuse: from Concepts to A-posteriori typing
Approaches to Model Transformation Reuse: from Concepts to A-posteriori typingApproaches to Model Transformation Reuse: from Concepts to A-posteriori typing
Approaches to Model Transformation Reuse: from Concepts to A-posteriori typing
miso_uam
 
Choosing a Persuasive Essay Topic
Choosing a Persuasive Essay TopicChoosing a Persuasive Essay Topic
Choosing a Persuasive Essay Topic
morag
 

Viewers also liked (13)

2nd International Conference on Inclusive Innovation and Innovative managemen...
2nd International Conference on Inclusive Innovation and Innovative managemen...2nd International Conference on Inclusive Innovation and Innovative managemen...
2nd International Conference on Inclusive Innovation and Innovative managemen...
 
Hombre
HombreHombre
Hombre
 
Domin ocartel
Domin ocartelDomin ocartel
Domin ocartel
 
01.03.2013, NEWSWIRE, Issue 263
01.03.2013, NEWSWIRE, Issue 26301.03.2013, NEWSWIRE, Issue 263
01.03.2013, NEWSWIRE, Issue 263
 
Approaches to Model Transformation Reuse: from Concepts to A-posteriori typing
Approaches to Model Transformation Reuse: from Concepts to A-posteriori typingApproaches to Model Transformation Reuse: from Concepts to A-posteriori typing
Approaches to Model Transformation Reuse: from Concepts to A-posteriori typing
 
13.05.2011, NEWSWIRE, Issue 167
13.05.2011, NEWSWIRE, Issue 16713.05.2011, NEWSWIRE, Issue 167
13.05.2011, NEWSWIRE, Issue 167
 
The Next Small Thing
The Next Small ThingThe Next Small Thing
The Next Small Thing
 
Hvetjandi samtalstækni
Hvetjandi samtalstækni Hvetjandi samtalstækni
Hvetjandi samtalstækni
 
Choosing a Persuasive Essay Topic
Choosing a Persuasive Essay TopicChoosing a Persuasive Essay Topic
Choosing a Persuasive Essay Topic
 
Ус-давсны солилцооны зохицуулгад байгалийн гаралтай эрдэст бэлдмэлийн нөлөө
Ус-давсны солилцооны зохицуулгад байгалийн гаралтай эрдэст бэлдмэлийн нөлөөУс-давсны солилцооны зохицуулгад байгалийн гаралтай эрдэст бэлдмэлийн нөлөө
Ус-давсны солилцооны зохицуулгад байгалийн гаралтай эрдэст бэлдмэлийн нөлөө
 
'What's it like to be an app?' experience prototyping workshop -- the output
'What's it like to be an app?' experience prototyping workshop -- the output'What's it like to be an app?' experience prototyping workshop -- the output
'What's it like to be an app?' experience prototyping workshop -- the output
 
Rubén Darío
Rubén DaríoRubén Darío
Rubén Darío
 
project OS
project  OSproject  OS
project OS
 

Similar to Queue

Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
SadiaSharmin40
 

Similar to Queue (20)

Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
Queues
Queues Queues
Queues
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Lect 17-18 Zaheer Abbas
Lect 17-18 Zaheer AbbasLect 17-18 Zaheer Abbas
Lect 17-18 Zaheer Abbas
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 

More from Abdur Rehman (18)

Financial accounting
Financial accountingFinancial accounting
Financial accounting
 
Dscrete structure
Dscrete  structureDscrete  structure
Dscrete structure
 
Valid & invalid arguments
Valid & invalid argumentsValid & invalid arguments
Valid & invalid arguments
 
Sets
SetsSets
Sets
 
Sequences
SequencesSequences
Sequences
 
Recursion
RecursionRecursion
Recursion
 
Quantification
QuantificationQuantification
Quantification
 
Proving existential statements
Proving existential statementsProving existential statements
Proving existential statements
 
Method of direct proof
Method of direct proofMethod of direct proof
Method of direct proof
 
Proofs by contraposition
Proofs by contrapositionProofs by contraposition
Proofs by contraposition
 
Laws in disceret
Laws in disceretLaws in disceret
Laws in disceret
 
Converse, contrapositive, inverse
Converse, contrapositive, inverseConverse, contrapositive, inverse
Converse, contrapositive, inverse
 
Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)Constructing circuits for boolean expressions(gate)
Constructing circuits for boolean expressions(gate)
 
Application of bases
Application of basesApplication of bases
Application of bases
 
Truth table
Truth tableTruth table
Truth table
 
Intro to disceret structure
Intro to disceret structureIntro to disceret structure
Intro to disceret structure
 
Dst lec3
Dst lec3Dst lec3
Dst lec3
 
logic, preposition etc
logic, preposition etclogic, preposition etc
logic, preposition etc
 

Recently uploaded

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 

Queue

  • 1.
  • 2.
  • 3. GROUP MEMBERS Names Roll No. Fatima Saqib 39 Bilal Younus 10 Areesha Butt 58 Muhammad Fahim 42 Poshma Kiyani 02 Abdur Rehman 41
  • 5. QUEUE A queue is two ended data structure in which items can be inserted from one end and taken out from the other end. Therefore , the first item inserted into queue is the first item to be taken out from the queue. This property is called First in First out [FIFO]. e.g: the picture given below:
  • 6. Queue Example ● A queue of people in the bank who are trying to submit utility bills. ● A line of vehicle on the motorway toll plaza. ● Consider a shared printer in a computer laboratory. Student can send their print request to the printer and each print request goto queue and serve one by one. ● Computer input operations performing through mouse and keyboard are also recorded into queue and serve on the basis of FIFO.
  • 8. Deque stands for double-end queue A data structure in which elements can be added or deleted at either the front or rear. But no changes can be made in the list Deque is generalization of both stack and queue. Deque
  • 9. Variations In Deque: There are two variations of a deque.  Input Restricted Deque  Output Restricted Deque
  • 10. An input restricted deque restricts the insertion of elements at one end only, but the deletion of elements can perform at both the ends. Input Restricted Deque
  • 11. An output restricted queue, restricts the deletion of elements at one end only, and allows insertion to be done at both the ends of deque Output Restricted Deque
  • 12. Possibilities The two possibilities that must be considered while inserting or deleting elements into the queue are : When an attempt is made to insert an element into a deque which is already full, an overflow occurs. When an attempt is made to delete an element from a deque which is empty, underflow occurs Deque
  • 13. The Queue Operations • A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ FrontRear
  • 14. The Queue Operations • New people must enter the queue at the rear. • The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Front Rear
  • 15. The Queue Operations • When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear
  • 16. Function of Queue  REAR  FRONT  APPEND  SERVE  IsEmpty  IsFull
  • 17. Rear Rear is the tail/bottom of the queue. The entry at rear is the most recently arrived item and must wait until the other entries are present in the queue. Insert (Enqueue) Remove (Dequeu) rearfront
  • 18. FRONT Front is the position from where items can be taken out from queue. It is also termed as head or top of the queue. The entry at the front position is ready to be served Insert (Enqueue) Remove (Dequeu) rearfront
  • 19. APPEND The operation to add an item at the rear position of the queue is termed as append or enqueue. We can append items at the rear position of the queue until the queue is not full. The rear points towards the position where new item has been appended. 17 23 97 44 333After insertion: 17 23 97 44Initial queue:
  • 20. Serve The operation to remove an item from the front position of the queue is termed as serve or dequeue. We can serve the items in the queue until the queue is not empty. The front points towards the position from where the available item can be severed SERVE
  • 21. Queue is consider empty when there is no item on front of the queue. IsEmpty operation checks for this condition and return true if the queue is empty else return false. IsEmpty 0 1 2 3 4 5 6 7 myQueue:
  • 22. If no element can be appended at rear of the queue then queue consider as full. When rear of the queue reaches this limit, then queue is full and the IsFull operation return true else return false IsFull 44 55 66 77 88 11 22 33 0 1 2 3 4 5 6 7 myQueue:
  • 23. Array Implementation of Queue • When enqueuing, the front index is always fixed and the rear index moves forward in the array. • There are several different algorithms to implement Enqueue and Dequeue front rear Enqueue(3) 3 front rear Enqueue(6) 3 6 front rear Enqueue(9) 3 6 9
  • 24. Queue Implementation of Array • When enqueuing, the front index is always fixed and the rear index moves forward in the array. • When dequeuing, the element at the front of the queue is removed. Move all the elements after it by one position. Dequeue() front rear 6 9 Dequeue() Dequeue() front rear 9 rear = -1 front
  • 25. Queue Implementation of Array • Better way • When an item is enqueued, make the rear index move forward. • When an item is dequeued, the front index moves by one element towards the back of the queue (thus removing the front item).
  • 26. Array Implementation.... • A queue can be implemented with an array. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . .. An array of integers to implement a queue of integers 4 8 6
  • 27. Array Implementation.... • The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6 size3 first0 last2
  • 28. ..Array implementation of queues • A queue is a first in, first out (FIFO) data structure • This is accomplished by inserting at one end (the rear) and deleting from the other (the front) • To insert: put new element in location 4, and set rear to 4 • To delete: take element from location 0, and set front to 1 28 17 23 97 44 0 1 2 3 4 5 6 7 myQueue: rear = 3front = 0
  • 29. …Array implementation of queues • Notice how the array contents “crawl” to the right as elements are inserted and deleted • This will be a problem after a while! 29 17 23 97 44 333After insertion: 23 97 44 333After deletion: rear = 4front = 1 17 23 97 44Initial queue: rear = 3front = 0
  • 30. Representation of queue by program • How to insert Value • How to delete value • How to print value • Complete Program Example
  • 31. To Insert Value Algorithm: R=F=-1 IF R >= SIZE THEN PRINT ‘’Overflow’’ RETURN ELSE { R=R+1 [Insert Item] Q[R]=‘’item’’ IF F=-1 THEN F=0 } END IF
  • 32. Algorithm: If ( FRONT = -1 ) then print "Queue is empty" Exit Else ITEM = Que [ FRONT ] If ( FRONT = REAR ) REAR = -1 FRONT = -1 Else FRONT = FRONT + 1 Stop To Delete Value
  • 33. Main body of program
  • 34. Class of the program
  • 35. Main body of program Output