SlideShare a Scribd company logo
5
Most read
7
Most read
15
Most read
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

PPTX
Queue - Data Structure - Notes
PPTX
The Stack And Recursion
PPSX
Data Structure (Queue)
PPT
Circular linked list
PPTX
Queue and its operations
PPTX
Deque and its applications
PDF
Applications of stack
PPTX
Queues in C++
Queue - Data Structure - Notes
The Stack And Recursion
Data Structure (Queue)
Circular linked list
Queue and its operations
Deque and its applications
Applications of stack
Queues in C++

What's hot (20)

PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PPT
Stacks
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPT
PPSX
PPTX
Stack and Queue
PPT
Queue data structure
PPTX
Presentation on queue
PPTX
Introduction to stack
PPTX
Priority Queue in Data Structure
PPTX
Queue in Data Structure
PPTX
Stack - Data Structure - Notes
PPTX
Doubly Linked List
PPTX
Tree traversal techniques
PPT
Linked list
PPTX
Binary Tree Traversal
PPTX
Graphs data structures
PDF
PPTX
Linked list
PDF
Expression trees
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Stacks
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Stack and Queue
Queue data structure
Presentation on queue
Introduction to stack
Priority Queue in Data Structure
Queue in Data Structure
Stack - Data Structure - Notes
Doubly Linked List
Tree traversal techniques
Linked list
Binary Tree Traversal
Graphs data structures
Linked list
Expression trees
Ad

Viewers also liked (20)

PPTX
Searching
PPTX
Linked List
PPTX
PPTX
Introduction to data_structure
PPTX
Unit 11. Graphics
PPTX
Algorithm big o
PPTX
Algorithm Introduction
PPTX
Unit 9. Structure and Unions
PPT
Stacks in algorithems & data structure
PPSX
Stacks Implementation and Examples
PPTX
Stack data structure
PPTX
Friedman two way analysis of variance by
PPTX
Tree in data structure
PDF
Tree and binary tree
PPTX
STACKS IN DATASTRUCTURE
PDF
Polish nootation
PPTX
Lecture 10 data structures and algorithms
PDF
Stacks and queues
PPTX
Unit 8. Pointers
PDF
Dsa circular queue
Searching
Linked List
Introduction to data_structure
Unit 11. Graphics
Algorithm big o
Algorithm Introduction
Unit 9. Structure and Unions
Stacks in algorithems & data structure
Stacks Implementation and Examples
Stack data structure
Friedman two way analysis of variance by
Tree in data structure
Tree and binary tree
STACKS IN DATASTRUCTURE
Polish nootation
Lecture 10 data structures and algorithms
Stacks and queues
Unit 8. Pointers
Dsa circular queue
Ad

Similar to Queues (20)

PPT
Queue AS an ADT (Abstract Data Type)
PPTX
queue.pptx
PPTX
Unit 4 queue
PPT
Stacks and Queues concept in Data Structures
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
Queue data structures and operation on data structures
PPTX
RPT_02_B_Queue presentation for FE students
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PPT
10994103.ppt
PDF
LEC4-DS ALGO.pdf
PPT
Data Structures by Maneesh Boddu
PPTX
Queue
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPTX
Unit – iv queue
PPTX
Queues
PPTX
@Chapter 4 DSA Part II.pptx
PPTX
queue & its applications
PPT
cp264_lecture18_queue.ppt
Queue AS an ADT (Abstract Data Type)
queue.pptx
Unit 4 queue
Stacks and Queues concept in Data Structures
Lec-07 Queues.ppt queues introduction to queue
Queue data structures and operation on data structures
RPT_02_B_Queue presentation for FE students
DS ppt1.pptx.c programing. Engineering. Data structure
10994103.ppt
LEC4-DS ALGO.pdf
Data Structures by Maneesh Boddu
Queue
Polynomialmotilalanehrunationalinstitute.pdf
Unit – iv queue
Queues
@Chapter 4 DSA Part II.pptx
queue & its applications
cp264_lecture18_queue.ppt

More from Ashim Lamichhane (9)

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

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
master seminar digital applications in india
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPH.pptx obstetrics and gynecology in nursing
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Basic Mud Logging Guide for educational purpose
master seminar digital applications in india
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharma ospi slides which help in ospi learning
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

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