SlideShare una empresa de Scribd logo
1 de 7
Descargar para leer sin conexión
Page 1 of 7

QUEUE
A queue is a data structure in which insertion is done at one end and deletion is done
from the other end.
A queue has two ends. They are:


Front End
Rear End

Addition of entities is done to the rear terminal position and removal of entities is done
from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data
structure. In a FIFO data structure, the first element added to the queue will be the first
one to be removed. This is equivalent to the requirement that whenever an element is
added, all elements that were added before have to be removed before the new element
can be invoked. A queue is an example of a linear data structure.
A queue can be represented in two ways:A. Array Representation of Queue.
B. Linked List Representation of Queue.
A. ARRAY REPRESENTATION OF QUEUE:Basic Operations on Queue:The two basic operations on queue are:

Insert or Enqueue.



Delete or Dequeue.

1. Insert Operation on Queue:In a queue, insert operation takes place at rear end. An “Enqueue” operation adds an
item to the “rear” of the queue.

Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 2 of 7

Steps for inserting an Element in a Queue:a. Initialize both the front and rear as -1, which means that the queue is empty.
b. When the first element will be inserted then the rear and front both will be
incremented by 1. But for the second element onwards only the rear will be
incremented.
c. The value of rear can be maximum up to Max-1, where Max is the maximum
number of elements a queue can hold.
d. If the rear reaches Max-1, then display a message that “The queue is full or Queue
Overflow”.
Algorithm for Insert Operation:If rear=MAX
Print “Queue is full”
Else
rear=rear+1
Queue[rear]=value
END

2. Delete Operation on Queue:In a queue, delete operation takes place at front end. The “Dequeue”operation removes
the item at the “front” of the queue and returns it.
Steps for deleting an Element in a Queue:a. When an element will be deleted from the queue the value of front will be
decremented by 1.
b. The value of front can be minimum up to 0.
c. If the front reaches -1, then display a message that “The queue is empty or Queue
Underflow”.
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 3 of 7

Algorithm for Delete Operation:If front=rear
Print “Queue is Empty” and Exit from the Program
Else
front=front+1
value=Queue[front]
END
B. LINKED LIST REPRESENTATION OF QUEUE:As we know if we implement the queue in array, sometimes number of memory location
remains unused. To overcome this drawback, it is better to implement queue using linked
list.

Performing one Dequeue/ Delete Operation:Here

Temp=New node.
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 4 of 7
Val= Value of the node.
Front= First Node of the queue.
Rear= Last Node of the queue.
Algorithm:-

If Front=NULL
Print “Queue is empty”
Else
Temp=Front
Front=Front->Next
Delete Temp
END

Performing one Enqueue/ Insert Operation for inserting an element 3:Algorithm:Temp= new node
Read (val)
Temp->data=Val
Temp->Link=NULL
If (Rear=NULL)
Rear=Temp
Front=Rear
Else
Rear->Link=Temp
Rear=Temp
END

Circular Queue:


Rear of the queue is somewhere clockwise from the front
To enqueue an element, we move rear one position clockwise and write the
element in that position
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 5 of 7





To dequeue, we simply move front one position clockwise
Queue migrates in a clockwise direction as we enqueue and dequeue
Emptiness and fullness to be checked carefully.
Apple

Orange

Banana

Front




Guava
Rear

In the above situation queue overflow will occur though the free spaces are also
available since the rear reaches the end. So, queue is full, but still there are two
free cells are available in queue. To avoid this problem, the queue can be arranged
in a circular way called circular queue.
In a circular queue as soon as the rear reaches the Max value, it should be reset to
1. So, if we have to insert Pineapple in the above queue we can insert it into the
first place.
Pineapple
Rear

Apple
Front

Orange

Banana

Guava

Algorithm to Insert an element in a Circular Queue:Note:- Let initially Rear=1 and Front=1.
If (rear+1) Mod Max=Front)
Print “Queue is full”
Else if Rear=Max
Rear=1
Else
Rear=Rear+1
End If
Queue[Rear]=Val
END

Algorithm to Delete an element in a Circular Queue:Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 6 of 7

Note:- Let initially Rear=1 and Front=1.
If Front=Rear
Print “Queue is empty”
Else if Front=Max
Front=1
Else
Front=Front+1
End if
Val=Queue[Front]
END

QUESTION BANK
1. What does ‘front’ signify in a queue?
Ans: Removal of elements from a queue is done through the front terminal.
2. What does ‘rear’ signify in a queue? If in a queue, the ‘rear’ points to the ‘front’,
what is the queue called?
Ans: Insertion of element in a queue is done in the ‘rear’ end. If in a queue, the rear
points to the front, then the queue is considered to be empty.
3. What is the full form of a dequeue?
Ans: It is Double-ended queue.
4. What is the dequeue called that allows insertion at only one end but deletion at
both the ends of the list? What is the dequeue called that allows deletion at only
one end but insertion at both the ends of the list?
Ans:
i. Input restricted dequeue.
ii. Output restricted dequeue.
5. Consider the following circular queue, where CQUEUE is allocated N=6 memory
cells: CQUEUE: AAA, DDD, EEE, FFF, GGG, __________
Describe the queue as the following operation take place:
(a) Insert (CQUEUE, KKK) (b) POP (CQUEUE, ITEM)
(c) Insert (CQUEUE, LLL) (d) Insert (CQUEUE, SSS)
(e) POP (CQUEUE, ITEM) (f) Insert (CQUEUE, TTT)
Ans:
(a) AAA, DDD, EEE, FFF, GGG, KKK
Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 7 of 7
(b) DDD, EEE, FFF, GGG, KKK
(c) DDD, EEE, FFF, GGG, KKK, LLL
(d) Queue Overflow.
(e) EEE, FFF, GGG, KKK, LLL
(f) EEE, FFF, GGG, KKK, LLL, TTT.
6. Differentiate between a linear queue and a Circular queue.
Ans:
Linear Queue
Circular Queue
1. A linear queue models the FIFO (first
In first out) data structure, much like
a line in real life.

1. The circular queue if the rear pointer
reaches the maximum size of the
Queue, then it is shifted to the first
position.

2. Linear queues require much less
programming logic.

2. Circular queues provide much more
flexibility than a linear queue

7. Differentiate between a queue and a dequeue.
Ans:
Queue
1. In a queue insertion takes place at rear
end and deletion at front end only.

Dequeue

1. In a dequeue the elements can be
added and removed at either end
but not in the middle.

Prepared By Sumit Kumar Gupta, PGT Computer Science

Más contenido relacionado

La actualidad más candente (20)

Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Queue
QueueQueue
Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
QUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING CQUEUE IN DATA STRUCTURE USING C
QUEUE IN DATA STRUCTURE USING C
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Team 6
Team 6Team 6
Team 6
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Queue
QueueQueue
Queue
 
Stack
StackStack
Stack
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 

Destacado

linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structureMarcus Biel
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queuesurya pandian
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Adam Mukharil Bachtiar
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arorakulachihansraj
 

Destacado (7)

linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Linked List data structure
Linked List data structureLinked List data structure
Linked List data structure
 
Queues
QueuesQueues
Queues
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Linked list
Linked listLinked list
Linked list
 

Similar a Queue

Similar a Queue (20)

Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
stack & queue
stack & queuestack & queue
stack & queue
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Queue
QueueQueue
Queue
 
Queue - Operations and Implementations
Queue - Operations and ImplementationsQueue - Operations and Implementations
Queue - Operations and Implementations
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
queues.pptx
queues.pptxqueues.pptx
queues.pptx
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 

Más de Swarup Boro

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Swarup Boro
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsSwarup Boro
 
Array using recursion
Array using recursionArray using recursion
Array using recursionSwarup Boro
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++Swarup Boro
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids Swarup Boro
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloadingSwarup Boro
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sortSwarup Boro
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbersSwarup Boro
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a numberSwarup Boro
 
Canteen management program
Canteen management programCanteen management program
Canteen management programSwarup Boro
 
C++ program using class
C++ program using classC++ program using class
C++ program using classSwarup Boro
 
Railway reservation
Railway reservationRailway reservation
Railway reservationSwarup Boro
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 

Más de Swarup Boro (20)

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
 
Array using recursion
Array using recursionArray using recursion
Array using recursion
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloading
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sort
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbers
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a number
 
Canteen management program
Canteen management programCanteen management program
Canteen management program
 
C++ program using class
C++ program using classC++ program using class
C++ program using class
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
 
Boolean
BooleanBoolean
Boolean
 
Classes
ClassesClasses
Classes
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Pointers
PointersPointers
Pointers
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Stack
StackStack
Stack
 
Functions
FunctionsFunctions
Functions
 

Último

DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICESayali Powar
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 

Último (20)

DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICE
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 

Queue

  • 1. Page 1 of 7 QUEUE A queue is a data structure in which insertion is done at one end and deletion is done from the other end. A queue has two ends. They are:  Front End Rear End Addition of entities is done to the rear terminal position and removal of entities is done from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure. A queue can be represented in two ways:A. Array Representation of Queue. B. Linked List Representation of Queue. A. ARRAY REPRESENTATION OF QUEUE:Basic Operations on Queue:The two basic operations on queue are: Insert or Enqueue.  Delete or Dequeue. 1. Insert Operation on Queue:In a queue, insert operation takes place at rear end. An “Enqueue” operation adds an item to the “rear” of the queue. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 7 Steps for inserting an Element in a Queue:a. Initialize both the front and rear as -1, which means that the queue is empty. b. When the first element will be inserted then the rear and front both will be incremented by 1. But for the second element onwards only the rear will be incremented. c. The value of rear can be maximum up to Max-1, where Max is the maximum number of elements a queue can hold. d. If the rear reaches Max-1, then display a message that “The queue is full or Queue Overflow”. Algorithm for Insert Operation:If rear=MAX Print “Queue is full” Else rear=rear+1 Queue[rear]=value END 2. Delete Operation on Queue:In a queue, delete operation takes place at front end. The “Dequeue”operation removes the item at the “front” of the queue and returns it. Steps for deleting an Element in a Queue:a. When an element will be deleted from the queue the value of front will be decremented by 1. b. The value of front can be minimum up to 0. c. If the front reaches -1, then display a message that “The queue is empty or Queue Underflow”. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 7 Algorithm for Delete Operation:If front=rear Print “Queue is Empty” and Exit from the Program Else front=front+1 value=Queue[front] END B. LINKED LIST REPRESENTATION OF QUEUE:As we know if we implement the queue in array, sometimes number of memory location remains unused. To overcome this drawback, it is better to implement queue using linked list. Performing one Dequeue/ Delete Operation:Here Temp=New node. Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 7 Val= Value of the node. Front= First Node of the queue. Rear= Last Node of the queue. Algorithm:- If Front=NULL Print “Queue is empty” Else Temp=Front Front=Front->Next Delete Temp END Performing one Enqueue/ Insert Operation for inserting an element 3:Algorithm:Temp= new node Read (val) Temp->data=Val Temp->Link=NULL If (Rear=NULL) Rear=Temp Front=Rear Else Rear->Link=Temp Rear=Temp END Circular Queue:  Rear of the queue is somewhere clockwise from the front To enqueue an element, we move rear one position clockwise and write the element in that position Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 5. Page 5 of 7    To dequeue, we simply move front one position clockwise Queue migrates in a clockwise direction as we enqueue and dequeue Emptiness and fullness to be checked carefully. Apple Orange Banana Front   Guava Rear In the above situation queue overflow will occur though the free spaces are also available since the rear reaches the end. So, queue is full, but still there are two free cells are available in queue. To avoid this problem, the queue can be arranged in a circular way called circular queue. In a circular queue as soon as the rear reaches the Max value, it should be reset to 1. So, if we have to insert Pineapple in the above queue we can insert it into the first place. Pineapple Rear Apple Front Orange Banana Guava Algorithm to Insert an element in a Circular Queue:Note:- Let initially Rear=1 and Front=1. If (rear+1) Mod Max=Front) Print “Queue is full” Else if Rear=Max Rear=1 Else Rear=Rear+1 End If Queue[Rear]=Val END Algorithm to Delete an element in a Circular Queue:Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 6. Page 6 of 7 Note:- Let initially Rear=1 and Front=1. If Front=Rear Print “Queue is empty” Else if Front=Max Front=1 Else Front=Front+1 End if Val=Queue[Front] END QUESTION BANK 1. What does ‘front’ signify in a queue? Ans: Removal of elements from a queue is done through the front terminal. 2. What does ‘rear’ signify in a queue? If in a queue, the ‘rear’ points to the ‘front’, what is the queue called? Ans: Insertion of element in a queue is done in the ‘rear’ end. If in a queue, the rear points to the front, then the queue is considered to be empty. 3. What is the full form of a dequeue? Ans: It is Double-ended queue. 4. What is the dequeue called that allows insertion at only one end but deletion at both the ends of the list? What is the dequeue called that allows deletion at only one end but insertion at both the ends of the list? Ans: i. Input restricted dequeue. ii. Output restricted dequeue. 5. Consider the following circular queue, where CQUEUE is allocated N=6 memory cells: CQUEUE: AAA, DDD, EEE, FFF, GGG, __________ Describe the queue as the following operation take place: (a) Insert (CQUEUE, KKK) (b) POP (CQUEUE, ITEM) (c) Insert (CQUEUE, LLL) (d) Insert (CQUEUE, SSS) (e) POP (CQUEUE, ITEM) (f) Insert (CQUEUE, TTT) Ans: (a) AAA, DDD, EEE, FFF, GGG, KKK Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 7. Page 7 of 7 (b) DDD, EEE, FFF, GGG, KKK (c) DDD, EEE, FFF, GGG, KKK, LLL (d) Queue Overflow. (e) EEE, FFF, GGG, KKK, LLL (f) EEE, FFF, GGG, KKK, LLL, TTT. 6. Differentiate between a linear queue and a Circular queue. Ans: Linear Queue Circular Queue 1. A linear queue models the FIFO (first In first out) data structure, much like a line in real life. 1. The circular queue if the rear pointer reaches the maximum size of the Queue, then it is shifted to the first position. 2. Linear queues require much less programming logic. 2. Circular queues provide much more flexibility than a linear queue 7. Differentiate between a queue and a dequeue. Ans: Queue 1. In a queue insertion takes place at rear end and deletion at front end only. Dequeue 1. In a dequeue the elements can be added and removed at either end but not in the middle. Prepared By Sumit Kumar Gupta, PGT Computer Science