SlideShare a Scribd company logo
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout << "The queue is full.n";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
int IntQueue::dequeue(void)
{
if (isEmpty())
cout << "The queue is empty.n";
else
{
// Retrieve the front item
int num = queueArray[front];
// Move front
front = (front + 1) % queueSize;
// Update item count
numItems--;
}
return num;
}
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
if (numItems < queueSize)
return false;
else
return true;
}
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}
//Program demonstrating the IntQueue class
void main(void)
{
IntQueue iQueue(5);
cout << "Enqueuing 5 items...n";
// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);
// Attempt to enqueue a 6th item.
cout << "Now attempting to enqueue again...n";
iQueue.enqueue(5);
// Deqeue and retrieve all items in the queue
cout << "The values in the queue were:n";
while (!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
}
Program Output
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0
1
2
3
4

More Related Content

PPT
Algo>Queues
PPTX
Data Structures - Lecture 6 [queues]
PDF
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
PPTX
Queues_0748555555555555555555555526.pptx
PPT
Lec-07 Queues.ppt queues introduction to queue
DOCX
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
PPTX
Queue oop
PDF
1- The design of a singly-linked list below is a picture of the functi (1).pdf
Algo>Queues
Data Structures - Lecture 6 [queues]
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Queues_0748555555555555555555555526.pptx
Lec-07 Queues.ppt queues introduction to queue
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Queue oop
1- The design of a singly-linked list below is a picture of the functi (1).pdf

Similar to Function enqueue inserts the value in num (20)

PPTX
PPTX
Queues in C++
PDF
Polynomialmotilalanehrunationalinstitute.pdf
PPT
Notes DATA STRUCTURE - queue
PPTX
Queue Implementation Using Array & Linked List
PPTX
Basic Queue Operation in DataStructure.pptx
PDF
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
PPT
Queues in C++ detailed explanation and examples .ppt
PPT
Lecture 2d queues
PPT
03 stacks and_queues_using_arrays
PPTX
The presention is about the queue data structure
PPT
computer notes - Data Structures - 9
PPT
Ch03_stacks_and_queues.ppt
PDF
Queues-and-CQueue-Implementation
DOCX
Create a Queue class that implements a queue abstraction. A queue is.docx
PPT
Computer notes data structures - 9
PPSX
Queue by rajanikanth
PPT
StacksandQueues.pptnajaiananaajaoakanabjana
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPT
Lec-12, 13 Quees Array Implementation IN
Queues in C++
Polynomialmotilalanehrunationalinstitute.pdf
Notes DATA STRUCTURE - queue
Queue Implementation Using Array & Linked List
Basic Queue Operation in DataStructure.pptx
CHAPTER 4 - DATA STRUCTURES QUEUES CHAPTER
Queues in C++ detailed explanation and examples .ppt
Lecture 2d queues
03 stacks and_queues_using_arrays
The presention is about the queue data structure
computer notes - Data Structures - 9
Ch03_stacks_and_queues.ppt
Queues-and-CQueue-Implementation
Create a Queue class that implements a queue abstraction. A queue is.docx
Computer notes data structures - 9
Queue by rajanikanth
StacksandQueues.pptnajaiananaajaoakanabjana
Mca ii dfs u-3 linklist,stack,queue
Lec-12, 13 Quees Array Implementation IN
Ad

More from Anil Yadav (20)

PPTX
Link List : Introduction to List and Linked Lists
PPTX
Link List REPRESENTATION OF DOUBLY LINKED LIST
PPTX
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
PPTX
Link List STACK and Queue USING LINKED LIST
PPTX
Link List Programming Linked List in Cpp
PPTX
Link List & ALGORITHM FOR DELETING A NODE
PPTX
Link List ALGORITHM FOR INSERTING A NODE
PPTX
Presentations Linked Lists Data Structure
PPT
Lec-12, 13 Quees First In First Out (FIFO)
PPT
Lec-12, 13 Quee s Applications of Queues
PPT
Lec-12, 13 Quees In Queue IntQueue(int s)
PPT
Lec-12, 13 Quees A class for Dynamic Queue implementation
PPT
Lec-12, 13 Quees -How to determine empty and full Queues?
PDF
Unit2-BIS Business Information system Data
PPT
Lec-12, 13 Queues - IntQueue IntQueue(int s) //constructor
PPT
Lec-12, 13 Quees Another implementation of Queues using Arrays
PPT
Lec-12, 13 Quees - Circular Queues and Implementation with Array
PPT
Lec-32 Recursion - Divide and Conquer in Queue
PPT
Lec-32 Recursion -Recursion in Computer Science
PPT
Lec-32 What is recursion? A mathematical look
Link List : Introduction to List and Linked Lists
Link List REPRESENTATION OF DOUBLY LINKED LIST
ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
Link List STACK and Queue USING LINKED LIST
Link List Programming Linked List in Cpp
Link List & ALGORITHM FOR DELETING A NODE
Link List ALGORITHM FOR INSERTING A NODE
Presentations Linked Lists Data Structure
Lec-12, 13 Quees First In First Out (FIFO)
Lec-12, 13 Quee s Applications of Queues
Lec-12, 13 Quees In Queue IntQueue(int s)
Lec-12, 13 Quees A class for Dynamic Queue implementation
Lec-12, 13 Quees -How to determine empty and full Queues?
Unit2-BIS Business Information system Data
Lec-12, 13 Queues - IntQueue IntQueue(int s) //constructor
Lec-12, 13 Quees Another implementation of Queues using Arrays
Lec-12, 13 Quees - Circular Queues and Implementation with Array
Lec-32 Recursion - Divide and Conquer in Queue
Lec-32 Recursion -Recursion in Computer Science
Lec-32 What is recursion? A mathematical look
Ad

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Presentation on HIE in infants and its manifestations
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
Pharma ospi slides which help in ospi learning
Chinmaya Tiranga quiz Grand Finale.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Anesthesia in Laparoscopic Surgery in India
Presentation on HIE in infants and its manifestations
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Function enqueue inserts the value in num

  • 1. IntQueue::IntQueue(int s) //constructor { queueArray = new int[s]; queueSize = s; front = -1; rear = -1; numItems = 0; } IntQueue::~IntQueue(void) //destructor { delete [] queueArray; }
  • 2. //******************************************** // Function enqueue inserts the value in num * // at the rear of the queue. * //******************************************** void IntQueue::enqueue(int num) { if (isFull()) cout << "The queue is full.n"; else { // Calculate the new rear position rear = (rear + 1) % queueSize; // Insert new item queueArray[rear] = num; // Update item count numItems++; } }
  • 3. //********************************************* // Function dequeue removes the value at the * // front of the queue, and copies t into num. * //********************************************* int IntQueue::dequeue(void) { if (isEmpty()) cout << "The queue is empty.n"; else { // Retrieve the front item int num = queueArray[front]; // Move front front = (front + 1) % queueSize; // Update item count numItems--; } return num; }
  • 4. //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * //********************************************* bool IntQueue::isEmpty(void) { if (numItems) return false; else return true; }
  • 5. //******************************************** // Function isFull returns true if the queue * // is full, and false otherwise. * //******************************************** bool IntQueue::isFull(void) { if (numItems < queueSize) return false; else return true; }
  • 6. //******************************************* // Function clear resets the front and rear * // indices, and sets numItems to 0. * //******************************************* void IntQueue::clear(void) { front = - 1; rear = - 1; numItems = 0; }
  • 7. //Program demonstrating the IntQueue class void main(void) { IntQueue iQueue(5); cout << "Enqueuing 5 items...n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Attempt to enqueue a 6th item. cout << "Now attempting to enqueue again...n"; iQueue.enqueue(5); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:n"; while (!iQueue.isEmpty()) { int value; iQueue.dequeue(value); cout << value << endl; } }
  • 8. Program Output Enqueuing 5 items... Now attempting to enqueue again... The queue is full. The values in the queue were: 0 1 2 3 4

Editor's Notes

  • #1: Visit: tshahab.blogspot.com
  • #4: Visit: tshahab.blogspot.com