SlideShare a Scribd company logo
http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm Copyright © tutorialspoint.com
DATA STRUCTURE - QUEUEDATA STRUCTURE - QUEUE
Queue is an abstract data structure, somewhat similar to stack. In contrast to stack, queue is
opened at both end. One end is always used to insert data enqueue and the other is used to remove
data dequeue. Queue follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
A real world example of queue can be a single-lane one-way road, where the vehicle enters first,
exits first. More real-world example can be seen as queues at ticket windows & bus-stops.
Queue Representation
As we now understand that in queue, we access both ends for different reasons, a diagram given
below tries to explain queue representation as data structure −
Same as stack, queue can also be implemented using Array, Linked-list, Pointer and Structures. For
the sake of simplicity we shall implement queue using one-dimensional array.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it and then completing
erasing it from memory. Here we shall try to understand basic operations associated with queues
−
enqueue − add store an item to the queue.
dequeue − remove access an item from the queue.
Few more functions are required to make above mentioned queue operation efficient. These are
−
peek − get the element at front of the queue without removing it.
isfull − checks if queue is full.
isempty − checks if queue is empty.
In queue, we always dequeue oraccess data, pointed by front pointer and while enqueing orstoring
data in queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek
Like stacks, this function helps to see the data at the front of the queue. Algorithm of peek
function −
begin procedure peek
return queue[front]
end procedure
Implementation of peek function in C programming language −
int peek() {
return queue[front];
}
isfull
As we are using single dimension array to implement queue, we just check for the rear pointer to
reach at MAXSIZE to determine that queue is full. In case we maintain queue in a circular linked-
list, the algorithm will differ. Algorithm of isfull function −
begin procedure isfull
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of isfull function in C programming language −
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty
Algorithm of isempty function −
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
If value of front is less than MIN or 0, it tells that queue is not yet initialized, hence empty.
Here's the C programming code −
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
As queue maintains two data pointers, front and rear, its operations are comparatively more
difficult to implement than stack.
The following steps should be taken to enqueue insert data into a queue −
Step 1 − Check if queue is full.
Step 2 − If queue is full, produce overflow error and exit.
Step 3 − If queue is not full, increment rear pointer to point next empty space.
Step 4 − Add data element to the queue location, where rear is pointing.
Step 5 − return success.
Sometimes, we also check that if queue is initialized or not to handle any unforeseen situations.
Algorithm for enqueue operation
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Implementation of enqueue in C programming language −
int enqueue(int data)
if(isfull())
return 0;
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
Dequeue Operation
Accessing data from queue is a process of two tasks − access the data 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.
Step 3 − If queue is not empty, access data where front is pointing.
Step 3 − Increment front pointer to point next available data element.
Step 5 − return success.
Algorithm for dequeue operation −
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front - 1
return true
end procedure
Implementation of dequeue in C programming language −
int dequeue() {
if(isempty())
return 0;
int data = queue[front];
front = front + 1;
return data;
}
For a complete stack program in C programming language, please click here.
Loading [MathJax]/jax/output/HTML-CSS/jax.js

More Related Content

PPTX
Data structure Stack
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
Queue
PPTX
Stack in Sata Structure
PPT
Queue data structure
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PDF
Algorithm and Data Structure - Queue
Data structure Stack
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Queue
Stack in Sata Structure
Queue data structure
STACK ( LIFO STRUCTURE) - Data Structure
Algorithm and Data Structure - Queue

What's hot (20)

PPSX
Data structure stack&queue basics
PPT
Queue in Data Structure
PPTX
Stacks and Queue - Data Structures
PDF
Algorithm and Data Structure - Stack
PPT
PPTX
Queues in C++
PDF
PPTX
My lecture stack_queue_operation
PPT
Queue AS an ADT (Abstract Data Type)
PPTX
PPTX
The Stack And Recursion
PPSX
Data Structure (Queue)
PDF
Queues-handouts
PPTX
stacks and queues
PPT
Stack & queue
PDF
Data Visualization With R: Learn To Combine Multiple Graphs
PPTX
PPT
Queue Data Structure
Data structure stack&queue basics
Queue in Data Structure
Stacks and Queue - Data Structures
Algorithm and Data Structure - Stack
Queues in C++
My lecture stack_queue_operation
Queue AS an ADT (Abstract Data Type)
The Stack And Recursion
Data Structure (Queue)
Queues-handouts
stacks and queues
Stack & queue
Data Visualization With R: Learn To Combine Multiple Graphs
Queue Data Structure
Ad

Viewers also liked (14)

DOCX
Planeación didáctica
PDF
Lab work servlets and jsp
PDF
Poscat seminar 6
PDF
What Every Client Should Do On Their Oracle SOA Projects (article)
PPTX
Network analysis for shortest optimum path
PPTX
formación para empresas
PPTX
kafkaのデータをRedshiftへ入れるパイプライン作ってみた
PDF
Phonons & Phonopy: Pro Tips (2014)
PDF
KINECT WITH ROS
PPT
Concept of rationality
PPTX
Dynamic programming
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PDF
Shortest Path Problem
PPTX
12. Heaps - Data Structures using C++ by Varsha Patil
Planeación didáctica
Lab work servlets and jsp
Poscat seminar 6
What Every Client Should Do On Their Oracle SOA Projects (article)
Network analysis for shortest optimum path
formación para empresas
kafkaのデータをRedshiftへ入れるパイプライン作ってみた
Phonons & Phonopy: Pro Tips (2014)
KINECT WITH ROS
Concept of rationality
Dynamic programming
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
Shortest Path Problem
12. Heaps - Data Structures using C++ by Varsha Patil
Ad

Similar to Queues (20)

PDF
Stacks-and-Queues.pdf
PPTX
QUEUE PPT BY KULJIT SINGH.pptx
PDF
PPTX
Queue and its operations
PPTX
Data structure , stack , queue
PPTX
Stack and queue
PDF
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
DOCX
ADT STACK and Queues
PPTX
The presention is about the queue data structure
PPTX
Queue Data Structure
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
Data Structures_Linear Data Structures Queue.pptx
PDF
Hashmaps, Stacks and Queues by Chidera Anichebe.pdf
PPTX
Data structures
PPTX
DS ppt1.pptx.c programing. Engineering. Data structure
PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PDF
DS UNIT 1.pdf
PDF
DS UNIT 1.pdf
DOCX
Ds
PPTX
Unit 3 Stacks and Queues.pptx
Stacks-and-Queues.pdf
QUEUE PPT BY KULJIT SINGH.pptx
Queue and its operations
Data structure , stack , queue
Stack and queue
Lab 07 (2).pdfbdvdyve dhdysbsnjsnsvdvydbdns
ADT STACK and Queues
The presention is about the queue data structure
Queue Data Structure
Lec-07 Queues.ppt queues introduction to queue
Data Structures_Linear Data Structures Queue.pptx
Hashmaps, Stacks and Queues by Chidera Anichebe.pdf
Data structures
DS ppt1.pptx.c programing. Engineering. Data structure
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
DS UNIT 1.pdf
DS UNIT 1.pdf
Ds
Unit 3 Stacks and Queues.pptx

More from maamir farooq (20)

DOCX
Ooad lab1
PPT
Lesson 03
PPT
Lesson 02
PDF
Php client libray
PDF
Swiftmailer
PDF
PDF
PPTX
PDF
PDF
J query 1.7 cheat sheet
PDF
Assignment
PDF
Java script summary
PDF
PDF
PPTX
PPTX
Css summary
DOCX
Manual of image processing lab
PDF
Session management
PDF
Data management
PPTX
Content provider
Ooad lab1
Lesson 03
Lesson 02
Php client libray
Swiftmailer
J query 1.7 cheat sheet
Assignment
Java script summary
Css summary
Manual of image processing lab
Session management
Data management
Content provider

Recently uploaded (20)

PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Week 4 Term 3 Study Techniques revisited.pptx
RMMM.pdf make it easy to upload and study
O5-L3 Freight Transport Ops (International) V1.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Basic Mud Logging Guide for educational purpose
2.FourierTransform-ShortQuestionswithAnswers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Microbial diseases, their pathogenesis and prophylaxis
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025

Queues

  • 1. http://www.tutorialspoint.com/data_structures_algorithms/dsa_queue.htm Copyright © tutorialspoint.com DATA STRUCTURE - QUEUEDATA STRUCTURE - QUEUE Queue is an abstract data structure, somewhat similar to stack. In contrast to stack, queue is opened at both end. One end is always used to insert data enqueue and the other is used to remove data dequeue. Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. A real world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world example can be seen as queues at ticket windows & bus-stops. Queue Representation As we now understand that in queue, we access both ends for different reasons, a diagram given below tries to explain queue representation as data structure − Same as stack, queue can also be implemented using Array, Linked-list, Pointer and Structures. For the sake of simplicity we shall implement queue using one-dimensional array. Basic Operations Queue operations may involve initializing or defining the queue, utilizing it and then completing erasing it from memory. Here we shall try to understand basic operations associated with queues − enqueue − add store an item to the queue. dequeue − remove access an item from the queue. Few more functions are required to make above mentioned queue operation efficient. These are − peek − get the element at front of the queue without removing it. isfull − checks if queue is full. isempty − checks if queue is empty. In queue, we always dequeue oraccess data, pointed by front pointer and while enqueing orstoring data in queue we take help of rear pointer. Let's first learn about supportive functions of a queue −
  • 2. peek Like stacks, this function helps to see the data at the front of the queue. Algorithm of peek function − begin procedure peek return queue[front] end procedure Implementation of peek function in C programming language − int peek() { return queue[front]; } isfull As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that queue is full. In case we maintain queue in a circular linked- list, the algorithm will differ. Algorithm of isfull function − begin procedure isfull if rear equals to MAXSIZE return true else return false endif end procedure Implementation of isfull function in C programming language − bool isfull() { if(rear == MAXSIZE - 1) return true; else return false; } isempty Algorithm of isempty function − begin procedure isempty if front is less than MIN OR front is greater than rear return true else return false endif end procedure If value of front is less than MIN or 0, it tells that queue is not yet initialized, hence empty. Here's the C programming code − bool isempty() { if(front < 0 || front > rear) return true; else
  • 3. return false; } Enqueue Operation As queue maintains two data pointers, front and rear, its operations are comparatively more difficult to implement than stack. The following steps should be taken to enqueue insert data into a queue − Step 1 − Check if queue is full. Step 2 − If queue is full, produce overflow error and exit. Step 3 − If queue is not full, increment rear pointer to point next empty space. Step 4 − Add data element to the queue location, where rear is pointing. Step 5 − return success. Sometimes, we also check that if queue is initialized or not to handle any unforeseen situations. Algorithm for enqueue operation procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure Implementation of enqueue in C programming language − int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data;
  • 4. return 1; end procedure Dequeue Operation Accessing data from queue is a process of two tasks − access the data 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. Step 3 − If queue is not empty, access data where front is pointing. Step 3 − Increment front pointer to point next available data element. Step 5 − return success. Algorithm for dequeue operation − procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front - 1 return true end procedure Implementation of dequeue in C programming language − int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; }
  • 5. For a complete stack program in C programming language, please click here. Loading [MathJax]/jax/output/HTML-CSS/jax.js