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

More Related Content

PPTX
Queues in C++
PPTX
Queue and its operations
PPTX
Queue - Data Structure - Notes
PPSX
Data Structure (Queue)
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
PPT
stack and queue array implementation in java.
PPTX
PPTX
Stack & Queue using Linked List in Data Structure
Queues in C++
Queue and its operations
Queue - Data Structure - Notes
Data Structure (Queue)
Computer Science-Data Structures :Abstract DataType (ADT)
stack and queue array implementation in java.
Stack & Queue using Linked List in Data Structure

What's hot (20)

PDF
Python list
PPTX
Quick sort
PPT
Queue Data Structure
PDF
Quick Sort , Merge Sort , Heap Sort
PPTX
Linked list
PPTX
heap Sort Algorithm
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Queue in Data Structure
PPTX
Stacks IN DATA STRUCTURES
PPTX
Functions in C
PPTX
Linked List
PPTX
Ppt on Linked list,stack,queue
PPTX
Presentation on queue
PPTX
Conversion of Infix to Prefix and Postfix with Stack
PPTX
Linked list
PPTX
Priority Queue in Data Structure
PDF
Queue as data_structure
PPT
Queue in Data Structure
PPTX
Merge sort algorithm
PPT
Binary search tree(bst)
Python list
Quick sort
Queue Data Structure
Quick Sort , Merge Sort , Heap Sort
Linked list
heap Sort Algorithm
Binary Search - Design & Analysis of Algorithms
Queue in Data Structure
Stacks IN DATA STRUCTURES
Functions in C
Linked List
Ppt on Linked list,stack,queue
Presentation on queue
Conversion of Infix to Prefix and Postfix with Stack
Linked list
Priority Queue in Data Structure
Queue as data_structure
Queue in Data Structure
Merge sort algorithm
Binary search tree(bst)
Ad

Similar to Queue AS an ADT (Abstract Data Type) (20)

PPTX
Unit 4 queue
PPTX
PPTX
PPTX
Unit – iv queue
PPTX
Queue data structures and operation on data structures
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
Queues_0748555555555555555555555526.pptx
PPTX
Queue types of queue and algorithms and queue
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPTX
@Chapter 4 DSA Part II.pptx
PPT
Queues & ITS TYPES
PPTX
queue.pptx
PPTX
Queues
PPTX
Data Structures and Agorithm: DS 09 Queue.pptx
PPTX
DS10-QUEUE0000000000000000000000000000000000000.pptx
PPTX
7-DSA- Queue.pptx in detail for students
PDF
Queue ADT for data structure for computer
Unit 4 queue
Unit – iv queue
Queue data structures and operation on data structures
Lec-07 Queues.ppt queues introduction to queue
Queues_0748555555555555555555555526.pptx
Queue types of queue and algorithms and queue
Polynomialmotilalanehrunationalinstitute.pdf
@Chapter 4 DSA Part II.pptx
Queues & ITS TYPES
queue.pptx
Queues
Data Structures and Agorithm: DS 09 Queue.pptx
DS10-QUEUE0000000000000000000000000000000000000.pptx
7-DSA- Queue.pptx in detail for students
Queue ADT for data structure for computer
Ad

More from Self-Employed (7)

PPT
Infix prefix postfix
PPTX
Ds lec 5_chap4
PPT
Discrete mathematics counting and logic relation
PPTX
Algorithm and C code related to data structure
PPT
Abstract data types (adt) intro to data structure part 2
PPTX
2.2 inverse of a matrix
PPTX
8086 architecture
Infix prefix postfix
Ds lec 5_chap4
Discrete mathematics counting and logic relation
Algorithm and C code related to data structure
Abstract data types (adt) intro to data structure part 2
2.2 inverse of a matrix
8086 architecture

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Business Ethics Teaching Materials for college
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
RMMM.pdf make it easy to upload and study
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
01-Introduction-to-Information-Management.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Business Ethics Teaching Materials for college
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPH.pptx obstetrics and gynecology in nursing
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
102 student loan defaulters named and shamed – Is someone you know on the list?
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
01-Introduction-to-Information-Management.pdf
Basic Mud Logging Guide for educational purpose
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

Queue AS an ADT (Abstract Data Type)

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