SlideShare a Scribd company logo
1 of 23
QUEUES
Unit 4
Intro
Ashim Lamichhane 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
Ashim Lamichhane 3
Queue Representation
• In queue, we access both ends for different reasons
Ashim Lamichhane 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.
Ashim Lamichhane 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.
Ashim Lamichhane 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
Ashim Lamichhane 7
Ashim Lamichhane 8
Implementation of enqueue()
int enqueue(int data) {
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
}
Ashim Lamichhane 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.
Ashim Lamichhane 10
Ashim Lamichhane 11
Implementation of dequeue()
int dequeue() {
if(isempty()) {
return 0;
}
int data = queue[front];
front = front + 1;
return data;
}
Ashim Lamichhane 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)
Ashim Lamichhane 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.
Ashim Lamichhane 14
Linear Queue
Ashim Lamichhane 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
Ashim Lamichhane 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.
Ashim Lamichhane 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.
Ashim Lamichhane 18
Ashim Lamichhane 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;
Ashim Lamichhane 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
Ashim Lamichhane 21
Assignments
• https://github.com/ashim888/dataStructureAndAlgorithm/tree/maste
r/Assignments/assignment_5
Ashim Lamichhane 22
References
• https://www.cs.cmu.edu/~adamchik/15-
121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html
• 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
Ashim Lamichhane 23

More Related Content

What's hot (20)

single linked list
single linked listsingle linked list
single linked list
 
Queue
QueueQueue
Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stack project
Stack projectStack project
Stack project
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
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
StackStack
Stack
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Queues
QueuesQueues
Queues
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
stack presentation
stack presentationstack presentation
stack presentation
 
Circular queue
Circular queueCircular queue
Circular queue
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Stack
StackStack
Stack
 
stack & queue
stack & queuestack & queue
stack & queue
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Linked list
Linked listLinked list
Linked list
 

Viewers also liked

Viewers also liked (20)

Searching
SearchingSearching
Searching
 
Linked List
Linked ListLinked List
Linked List
 
Sorting
SortingSorting
Sorting
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Friedman two way analysis of variance by
Friedman two way analysis of  variance byFriedman two way analysis of  variance by
Friedman two way analysis of variance by
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Polish nootation
Polish nootationPolish nootation
Polish nootation
 
Lecture 10 data structures and algorithms
Lecture 10 data structures and algorithmsLecture 10 data structures and algorithms
Lecture 10 data structures and algorithms
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 

Similar to Queues

Similar to Queues (20)

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
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queue
QueueQueue
Queue
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
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
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
Queues
QueuesQueues
Queues
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
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
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
10994103.ppt
10994103.ppt10994103.ppt
10994103.ppt
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 

More from Ashim Lamichhane

More from Ashim Lamichhane (9)

Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Unit 4. Operators and Expression
Unit 4. Operators and Expression  Unit 4. Operators and Expression
Unit 4. Operators and Expression
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 

Recently uploaded

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
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
 
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
 
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
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 
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
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
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
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 

Recently uploaded (20)

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
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 🔝✔️✔️
 
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
 
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
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
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Ă...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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
 
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...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
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🔝
 
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
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 

Queues

  • 2. Intro Ashim Lamichhane 2 Movie Hall Ticketing One Way Traffic
  • 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 Ashim Lamichhane 3
  • 4. Queue Representation • In queue, we access both ends for different reasons Ashim Lamichhane 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. Ashim Lamichhane 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. Ashim Lamichhane 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 Ashim Lamichhane 7
  • 9. Implementation of enqueue() int enqueue(int data) { if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; } Ashim Lamichhane 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. Ashim Lamichhane 10
  • 12. Implementation of dequeue() int dequeue() { if(isempty()) { return 0; } int data = queue[front]; front = front + 1; return data; } Ashim Lamichhane 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) Ashim Lamichhane 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. Ashim Lamichhane 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 Ashim Lamichhane 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. Ashim Lamichhane 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. Ashim Lamichhane 18
  • 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; Ashim Lamichhane 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 Ashim Lamichhane 21
  • 23. References • https://www.cs.cmu.edu/~adamchik/15- 121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html • 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 Ashim Lamichhane 23