SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
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         Guava

                                     Front                                       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                      Apple        Orange         Banana         Guava
          Rear                       Front

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      1. The circular queue if the rear pointer
In first out) data structure, much like          reaches the maximum size of the
a line in real life.                             Queue, then it is shifted to the first
                                                 position.

2. Linear queues require much less            2. Circular queues provide much more
programming logic.                               flexibility than a linear queue

7. Differentiate between a queue and a dequeue.
Ans:
          Queue                                         Dequeue

1. In a queue insertion takes place at rear   1. In a dequeue the elements can be
   end and deletion at front end only.           added and removed at either end
                                                 but not in the middle.




                        Prepared By Sumit Kumar Gupta, PGT Computer Science

Contenu connexe

Tendances

Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queuesBuxoo Abdullah
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation Anil Kumar Prajapati
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
 
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxChapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxSovannDoeur
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked listKeval Bhogayata
 

Tendances (20)

Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Lists
ListsLists
Lists
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Linked list
Linked listLinked list
Linked list
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 
Circular queue
Circular queueCircular queue
Circular queue
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptxChapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Data structures chapter 1
Data structures chapter  1Data structures chapter  1
Data structures chapter 1
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Circular Queue data structure
Circular Queue data structureCircular Queue data structure
Circular Queue data structure
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 

Similaire à Queue (20)

Queue
QueueQueue
Queue
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Queue
QueueQueue
Queue
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Queue and its operations
Queue and its operationsQueue and its operations
Queue and its operations
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
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
 
Queue - Operations and Implementations
Queue - Operations and ImplementationsQueue - Operations and Implementations
Queue - Operations and Implementations
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
cp264_lecture18_queue.ppt
cp264_lecture18_queue.pptcp264_lecture18_queue.ppt
cp264_lecture18_queue.ppt
 
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)
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue
QueueQueue
Queue
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Algorithm and Data Structure - Queue
Algorithm and Data Structure - QueueAlgorithm and Data Structure - Queue
Algorithm and Data Structure - Queue
 

Plus de Swarup Kumar Boro (20)

c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
c++ program for Canteen management
c++ program for Canteen managementc++ program for Canteen management
c++ program for Canteen management
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Pointers
PointersPointers
Pointers
 
File handling
File handlingFile handling
File handling
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Stack
StackStack
Stack
 
Functions
FunctionsFunctions
Functions
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
2-D array
2-D array2-D array
2-D array
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Unit 3
Unit  3Unit  3
Unit 3
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
01 computer communication and networks v
01 computer communication and networks v01 computer communication and networks v
01 computer communication and networks v
 
1-D array
1-D array1-D array
1-D array
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
computer science sample papers 3
computer science sample papers 3computer science sample papers 3
computer science sample papers 3
 
computer science sample papers 1
computer science sample papers 1computer science sample papers 1
computer science sample papers 1
 

Dernier

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 

Dernier (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.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 Guava Front 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 Apple Orange Banana Guava Rear Front 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 1. The circular queue if the rear pointer In first out) data structure, much like reaches the maximum size of the a line in real life. Queue, then it is shifted to the first position. 2. Linear queues require much less 2. Circular queues provide much more programming logic. flexibility than a linear queue 7. Differentiate between a queue and a dequeue. Ans: Queue Dequeue 1. In a queue insertion takes place at rear 1. In a dequeue the elements can be end and deletion at front end only. added and removed at either end but not in the middle. Prepared By Sumit Kumar Gupta, PGT Computer Science