SlideShare a Scribd company logo
Circular Queue
Data Structure
• A circular queue is an abstract data type that contains
a collection of data which allows addition of data at
the end of the queue and removal of data at the
beginning of the queue. Circular queues have a fixed
size.
• Circular queue follows FIFO principle. Queue items
are added at the rear end and the items are deleted at
front end of the circular queue.
Abstract Data Type (ADT)
• An abstract data type (ADT) is a data types where a
data type is defined by its behavior (semantics) from
the point of view of a user of the data, specifically in
terms of possible values, possible operations on data of
this type, and the behavior of these operations.
Example of ADT
• Integers are an ADT, defined as the values …, −2, −1,
0, 1, 2, …, and by the operations of addition,
subtraction, multiplication, and division, together
with greater than, less than, etc., which behave
according to familiar mathematics
• Abstract stack, which is a last-in-first-out structure,
could be defined by three operations: push, that
inserts a data item onto the stack; pop, that removes a
data item from it; and peek or top, that accesses a data
item on top of the stack without removal.
• Abstract queue, which is a first-in-first-out structure,
would also have three operations: enqueue, that
inserts a data item into the queue; dequeue, that
removes the first data item from it; and front, that
accesses and serves the first data item in the queue.
Advantages of Circular Queues
• In Circular Queues we utilize memory efficiently.
because in queue when we delete any element only
front increment by 1, but that position is not used
later. so when we perform more add and delete
operation, memory wastage increase. But in CQ
memory is utilized, if we delete any element that
position is used later, because it is circular.
Implementation of Circular Queue
• Step 1: Include all the header files which are used in
the program and define a constant 'SIZE' with specific
value.
• Step 2: Declare all user defined functions used in
circular queue implementation.
• Step 3: Create a one dimensional array with above
defined SIZE (int cQueue[SIZE])
• Step 4: Define two integer variables 'front' and 'rear' and
initialize both with '-1'. (int front = -1, rear = -1)
• Step 5: Implement main method by displaying menu of
operations list and make suitable function calls to
perform operation selected by the user on circular
queue.
Inserting value into the Circular Queue
In a circular queue, enQueue() is a function which is used
to insert an element into the circular queue. In a circular
queue, the new element is always inserted
at rear position. The enQueue() function takes one
integer value as parameter and inserts that value into the
circular queue. We can use the following steps to insert an
element into the circular queue...
Step 1: Check whether queue is FULL.
((rear == SIZE-1 && front == 0) || (front == rear+1))
Step 2: If it is FULL, then
display "Queue is FULL!!!
Insertion is not possible!!!" and terminate the function.
Step 3: If it is NOT FULL, then check
rear == SIZE - 1 && front != 0
if it is TRUE, then set rear = -1.
Step 4: Increment rear value by one (rear++),
set queue[rear] = value and check
'front == -1' if it is TRUE, then set front = 0.
Deleting a value from Circular Queue
• In a circular queue, deQueue() is a function used to
delete an element from the circular queue. In a
circular queue, the element is always deleted
from front position. The deQueue() function doesn't
take any value as parameter. We can use the following
steps to delete an element from the circular queue...
Step 1: Check whether queue is EMPTY.
(front == -1 && rear == -1)
Step 2: If it is EMPTY, then display "Queue is
EMPTY!!! Deletion is not possible!!!" and terminate
the function.
• Step 3: If it is NOT EMPTY, then display queue[front] as
deleted element and increment the front value by one
(front ++). Then check whether front == SIZE, if it
is TRUE, then set front = 0. Then check whether
both front - 1 and rear are equal (front -1 == rear), if
it TRUE, then set both front and rear to '-1'
(front = rear = -1).
Displays elements of a Circular Queue
Step 1: Check whether queue is EMPTY. (front == -1)
Step 2: If it is EMPTY, then display "Queue is
EMPTY!!!" and terminate the function.
Step 3: If it is NOT EMPTY, then define an integer
variable 'i' and set 'i = front'.
Step 4: Check whether 'front <= rear', if it is TRUE,
then display 'queue[i]' value and increment 'i' value
by one (i++). Repeat the same until 'i <= rear'
becomes FALSE.
Step 5: If 'front <= rear' is FALSE, then display
'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until'i <= SIZE - 1' becomes FALSE.
• Step 6: Set i to 0.
• Step 7: Again display 'cQueue[i]' value and
increment i value by one (i++). Repeat the same until
'i <= rear' becomes FALSE.
Program to implement Circular Queue
using Array
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
int cQueue[SIZE], front = -1, rear = -1;
void enQueue(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
{
printf("Circular Queue is Full. Insertion not possible");
}
else
{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("nInsertion Success!!!n");
if(front == -1)
front = 0;
} }
void deQueue()
{
if(front == -1 && rear == -1)
printf("nCircular Queue is Empty! Deletion is not
possible!!!n");
else {
printf("nDeleted element %dn",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("Circular Queue is Empty.");
Else
{
int i = front;
printf("Circular Queue Elements are");
if(front <= rear)
{
while(i <= rear)
printf("%d",cQueue[i++]);
}
• else
• {
• while(i <= SIZE - 1)
• printf("%d", cQueue[i++]);
• i = 0;
• while(i <= rear)
• printf("%d",cQueue[i++]);
• } } }

More Related Content

PPTX
Circular Queue data structure
PPTX
Point To Point Protocol
PPTX
Recommendation System
PPTX
Stacks IN DATA STRUCTURES
PPTX
INDIAN KNOWLEDGE SYSTEM PPT chp-1.pptx
PPTX
Steps in data analysis.pptx
PDF
Data structure ppt
PPTX
queue & its applications
Circular Queue data structure
Point To Point Protocol
Recommendation System
Stacks IN DATA STRUCTURES
INDIAN KNOWLEDGE SYSTEM PPT chp-1.pptx
Steps in data analysis.pptx
Data structure ppt
queue & its applications

What's hot (20)

PPTX
Doubly Linked List
PDF
PPT
Queue data structure
PPSX
Data Structure (Queue)
PPTX
STACKS IN DATASTRUCTURE
PPT
Binary Search
PPTX
Queue ppt
PPTX
heap Sort Algorithm
PPTX
Priority Queue in Data Structure
PPTX
Circular link list.ppt
PPTX
Data structure - Graph
PPTX
Data structure & its types
PPTX
Bfs and Dfs
PPT
PPTX
Asymptotic notations
PPTX
linked list
PPTX
stack & queue
PPTX
Queue - Data Structure - Notes
Doubly Linked List
Queue data structure
Data Structure (Queue)
STACKS IN DATASTRUCTURE
Binary Search
Queue ppt
heap Sort Algorithm
Priority Queue in Data Structure
Circular link list.ppt
Data structure - Graph
Data structure & its types
Bfs and Dfs
Asymptotic notations
linked list
stack & queue
Queue - Data Structure - Notes
Ad

Viewers also liked (19)

PPTX
Method overloading
PPTX
My lectures circular queue
PPTX
Circular Queue
PPTX
Stacks in Data Structure
PPTX
Introduction to java
PPT
Java naming conventions
PPT
Java static keyword
PDF
Queues
PDF
PPTX
PPT
Circular queues
PDF
Data structure and algorithm.(dsa)
PPTX
Presentation on queue
PPT
Queue in Data Structure
PPTX
Queue Data Structure (w/ php egs)
PPT
Queue Data Structure
PPSX
PDF
Queue as data_structure
PDF
CBSE XII Database Concepts And MySQL Presentation
Method overloading
My lectures circular queue
Circular Queue
Stacks in Data Structure
Introduction to java
Java naming conventions
Java static keyword
Queues
Circular queues
Data structure and algorithm.(dsa)
Presentation on queue
Queue in Data Structure
Queue Data Structure (w/ php egs)
Queue Data Structure
Queue as data_structure
CBSE XII Database Concepts And MySQL Presentation
Ad

Similar to Circular queue (20)

PPTX
QUEUE.pptx
PPTX
VCE Unit 03vv.pptx
PPTX
Unit ii linear data structures
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PPTX
Unit 4 queue
PPTX
Stack.pptx
PPTX
queue.pptx
PPTX
RPT_02_B_Queue presentation for FE students
PPT
Lecture 2d queues
PPTX
Queue Data Structure
PPTX
Stack and queue
PPTX
PPTX
08_Queues.pptx showing how que works given vertex
PPTX
Unit 3 Stacks and Queues.pptx
PPTX
The presention is about the queue data structure
PPTX
PPTX
DS UNIT2QUEUES.pptx
QUEUE.pptx
VCE Unit 03vv.pptx
Unit ii linear data structures
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
DS ppt1.pptx.c programing. Engineering. Data structure
Unit 4 queue
Stack.pptx
queue.pptx
RPT_02_B_Queue presentation for FE students
Lecture 2d queues
Queue Data Structure
Stack and queue
08_Queues.pptx showing how que works given vertex
Unit 3 Stacks and Queues.pptx
The presention is about the queue data structure
DS UNIT2QUEUES.pptx

More from Lovely Professional University (20)

PPTX
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
PPTX
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
PPTX
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
PPTX
Programme Management & Project Evaluation
PPTX
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
PPTX
Introduction to Software Project Management:
PDF
The HyperText Markup Language or HTML is the standard markup language
PPTX
PPTX
PPTX
Getting Input from User
PPTX
PPTX
Transaction Processing in DBMS.pptx
PPT
web_server_browser.ppt
PPTX
PPTX
Number System.pptx
PPT
Programming Language.ppt
PPTX
Information System.pptx
PPTX
Applications of Computer Science in Pharmacy-1.pptx
PPTX
Application of Computers in Pharmacy.pptx
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
Programme Management & Project Evaluation
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
Introduction to Software Project Management:
The HyperText Markup Language or HTML is the standard markup language
Getting Input from User
Transaction Processing in DBMS.pptx
web_server_browser.ppt
Number System.pptx
Programming Language.ppt
Information System.pptx
Applications of Computer Science in Pharmacy-1.pptx
Application of Computers in Pharmacy.pptx

Recently uploaded (20)

PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
web development for engineering and engineering
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Welding lecture in detail for understanding
Internet of Things (IOT) - A guide to understanding
Mechanical Engineering MATERIALS Selection
Geodesy 1.pptx...............................................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Digital Logic Computer Design lecture notes
Lecture Notes Electrical Wiring System Components
Structs to JSON How Go Powers REST APIs.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
web development for engineering and engineering
OOP with Java - Java Introduction (Basics)
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Welding lecture in detail for understanding

Circular queue

  • 2. • A circular queue is an abstract data type that contains a collection of data which allows addition of data at the end of the queue and removal of data at the beginning of the queue. Circular queues have a fixed size. • Circular queue follows FIFO principle. Queue items are added at the rear end and the items are deleted at front end of the circular queue.
  • 3. Abstract Data Type (ADT) • An abstract data type (ADT) is a data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations.
  • 4. Example of ADT • Integers are an ADT, defined as the values …, −2, −1, 0, 1, 2, …, and by the operations of addition, subtraction, multiplication, and division, together with greater than, less than, etc., which behave according to familiar mathematics
  • 5. • Abstract stack, which is a last-in-first-out structure, could be defined by three operations: push, that inserts a data item onto the stack; pop, that removes a data item from it; and peek or top, that accesses a data item on top of the stack without removal.
  • 6. • Abstract queue, which is a first-in-first-out structure, would also have three operations: enqueue, that inserts a data item into the queue; dequeue, that removes the first data item from it; and front, that accesses and serves the first data item in the queue.
  • 7. Advantages of Circular Queues • In Circular Queues we utilize memory efficiently. because in queue when we delete any element only front increment by 1, but that position is not used later. so when we perform more add and delete operation, memory wastage increase. But in CQ memory is utilized, if we delete any element that position is used later, because it is circular.
  • 8. Implementation of Circular Queue • Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with specific value. • Step 2: Declare all user defined functions used in circular queue implementation. • Step 3: Create a one dimensional array with above defined SIZE (int cQueue[SIZE])
  • 9. • Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear = -1) • Step 5: Implement main method by displaying menu of operations list and make suitable function calls to perform operation selected by the user on circular queue.
  • 10. Inserting value into the Circular Queue In a circular queue, enQueue() is a function which is used to insert an element into the circular queue. In a circular queue, the new element is always inserted at rear position. The enQueue() function takes one integer value as parameter and inserts that value into the circular queue. We can use the following steps to insert an element into the circular queue...
  • 11. Step 1: Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front == rear+1)) Step 2: If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function.
  • 12. Step 3: If it is NOT FULL, then check rear == SIZE - 1 && front != 0 if it is TRUE, then set rear = -1. Step 4: Increment rear value by one (rear++), set queue[rear] = value and check 'front == -1' if it is TRUE, then set front = 0.
  • 13. Deleting a value from Circular Queue • In a circular queue, deQueue() is a function used to delete an element from the circular queue. In a circular queue, the element is always deleted from front position. The deQueue() function doesn't take any value as parameter. We can use the following steps to delete an element from the circular queue...
  • 14. Step 1: Check whether queue is EMPTY. (front == -1 && rear == -1) Step 2: If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function.
  • 15. • Step 3: If it is NOT EMPTY, then display queue[front] as deleted element and increment the front value by one (front ++). Then check whether front == SIZE, if it is TRUE, then set front = 0. Then check whether both front - 1 and rear are equal (front -1 == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
  • 16. Displays elements of a Circular Queue
  • 17. Step 1: Check whether queue is EMPTY. (front == -1) Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function. Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
  • 18. Step 4: Check whether 'front <= rear', if it is TRUE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i <= rear' becomes FALSE. Step 5: If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until'i <= SIZE - 1' becomes FALSE.
  • 19. • Step 6: Set i to 0. • Step 7: Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the same until 'i <= rear' becomes FALSE.
  • 20. Program to implement Circular Queue using Array
  • 21. #include<stdio.h> #include<conio.h> #define SIZE 5 void enQueue(int); void deQueue(); void display(); int cQueue[SIZE], front = -1, rear = -1;
  • 22. void enQueue(int value) { if((front == 0 && rear == SIZE - 1) || (front == rear+1)) { printf("Circular Queue is Full. Insertion not possible"); }
  • 23. else { if(rear == SIZE-1 && front != 0) rear = -1; cQueue[++rear] = value; printf("nInsertion Success!!!n"); if(front == -1) front = 0; } }
  • 24. void deQueue() { if(front == -1 && rear == -1) printf("nCircular Queue is Empty! Deletion is not possible!!!n"); else { printf("nDeleted element %dn",cQueue[front++]); if(front == SIZE) front = 0; if(front-1 == rear) front = rear = -1; } }
  • 25. void display() { if(front == -1) printf("Circular Queue is Empty."); Else { int i = front; printf("Circular Queue Elements are"); if(front <= rear) { while(i <= rear) printf("%d",cQueue[i++]); }
  • 26. • else • { • while(i <= SIZE - 1) • printf("%d", cQueue[i++]); • i = 0; • while(i <= rear) • printf("%d",cQueue[i++]); • } } }