SlideShare a Scribd company logo
Implement the Queue ADT using array – based approach. Using C++ programming Language
#include "QueueArray.h"
template
QueueArray::QueueArray(int maxNumber)
{
}
template
QueueArray::QueueArray(const QueueArray& other)
{
}
template
QueueArray& QueueArray::operator=(const QueueArray& other)
{
}
template
QueueArray::~QueueArray()
{
}
template
void QueueArray::enqueue(const DataType& newDataItem) throw (logic_error)
{
}
template
DataType QueueArray::dequeue() throw (logic_error)
{
DataType temp;
return temp;
}
template
void QueueArray::clear()
{
}
template
bool QueueArray::isEmpty() const
{
return false;
}
template
bool QueueArray::isFull() const
{
return false;
}
template
void QueueArray::putFront(const DataType& newDataItem) throw (logic_error)
{
}
template
DataType QueueArray::getRear() throw (logic_error)
{
DataType temp;
return temp;
}
template
int QueueArray::getLength() const
{
return -1;
}
//--------------------------------------------------------------------
template
void QueueArray::showStructure() const
// Array implementation. Outputs the data items in a queue. If the
// queue is empty, outputs "Empty queue". This operation is intended
// for testing and debugging purposes only.
{
int j; // Loop counter
if ( front == -1 )
cout << "Empty queue" << endl;
else
{
cout << "Front = " << front << " Back = " << back << endl;
for ( j = 0 ; j < maxSize ; j++ )
cout << j << "t";
cout << endl;
if ( back >= front )
for ( j = 0 ; j < maxSize ; j++ )
if ( ( j >= front ) && ( j <= back ) )
cout << dataItems[j] << "t";
else
cout << " t";
else
for ( j = 0 ; j < maxSize ; j++ )
if ( ( j >= front ) || ( j <= back ) )
cout << dataItems[j] << "t";
else
cout << " t";
cout << endl;
}
}
QueueArray.h
___-----------------------------------------------------------------------------
#ifndef QUEUEARRAY_H
#define QUEUEARRAY_H
#include
#include
using namespace std;
#include "Queue.h"
template
class QueueArray : public Queue {
public:
QueueArray(int maxNumber = Queue::MAX_QUEUE_SIZE);
QueueArray(const QueueArray& other);
QueueArray& operator=(const QueueArray& other);
~QueueArray();
void enqueue(const DataType& newDataItem) throw (logic_error);
DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void putFront(const DataType& newDataItem) throw (logic_error);
DataType getRear() throw (logic_error);
int getLength() const;
void showStructure() const;
private:
int maxSize;
int front;
int back;
DataType* dataItems;
};
#endif
Solution
QueueArray.cpp
#include "QueueArray.h"
template
QueueArray::QueueArray(int maxNumber)
{
maxSize = maxNumber;
this->dataItems = new DataType[maxSize];
front = -1;
back = -1;
}
template
QueueArray::QueueArray(const QueueArray& other)
{
dataItems = new DataType[maxSize];
for (int i = 0; i <= top; i++)
{
dataItems[i] = other.dataItems[i];
}
}
template
QueueArray& QueueArray::operator=(const QueueArray& other)
{
if (maxSize < other.maxSize)
{
delete[] dataItems;
dataItems = new dataType[other.maxSize];
}
maxSize = other.maxSize;
top = other.top;
for (int i = 0; i <= top; i++)
{
dataItems[i] = other.dataItems[i];
}
return *this;
}
template
QueueArray::~QueueArray()
{
this->clear();
}
template
void QueueArray::enqueue(const DataType& newDataItem) throw (logic_error)
{
if (this->isFull())
throw logic_error("Queue is Full.");
if (this->isEmpty())
{
dataItems[back + 1] = newDataItem;
back = (++back) % maxSize;
front++;
}
else if (back == 7 && !isFull())
{
back = 0;
dataItems[back] = newDataItem;
}
else {
back = (++back) % maxSize;
dataItems[back] = newDataItem;
}
}
template
DataType QueueArray::dequeue() throw (logic_error)
{
if (this->isEmpty())
throw logic_error("Queue is empty. ");
if (getLength() == 1)
{
DataType temp = dataItems[this->front];
clear();
return temp;
}
else {
DataType temp;
temp = dataItems[this->front];
front++;
return temp;
}
}
template
void QueueArray::clear()
{
front = -1;
back = -1;
}
template
bool QueueArray::isEmpty() const
{
return (front == -1);
}
template
bool QueueArray::isFull() const
{
return (getLength() == this->maxSize);
}
template
void QueueArray::putFront(const DataType& newDataItem) throw (logic_error)
{
if (isFull())
throw logic_error("Queue is Full.");
else {
if (isEmpty())
{
front = 0;
back = 0;
}
else {
front--;
if (front < 0)
front = (maxSize - 1);
}
dataItems[front] = newDataItem;
}
}
template
DataType QueueArray::getRear() throw (logic_error)
{
int cursor = -1;
if (isEmpty()) {
throw logic_error("Queue is Empty.");
}
else {
cursor = back;
if (front == back)
{
front = -1;
back = -1;
}
else
{
--back;
if (back < 0)
back = (maxSize - 1);
}
return dataItems[cursor];
}
}
template
int QueueArray::getLength() const
{
if (front > back)
return (back - front + maxSize + 1);
else
return back - front + 1;
}
//--------------------------------------------------------------------
template
void QueueArray::showStructure() const
// Array implementation. Outputs the data items in a queue. If the
// queue is empty, outputs "Empty queue". This operation is intended
// for testing and debugging purposes only.
{
int j; // Loop counter
if (front == -1)
cout << "Empty queue" << endl;
else
{
cout << "Front = " << front << " Back = " << back << endl;
for (j = 0; j < maxSize; j++)
cout << j << "t";
cout << endl;
if (back >= front)
for (j = 0; j < maxSize; j++)
if ((j >= front) && (j <= back))
cout << dataItems[j] << "t";
else
cout << " t";
else
for (j = 0; j < maxSize; j++)
if ((j >= front) || (j <= back))
cout << dataItems[j] << "t";
else
cout << " t";
cout << endl;
}
}
QueueArray.h
// QueueArray.h
#ifndef QUEUEARRAY_H
#define QUEUEARRAY_H
#include
#include
using namespace std;
#include "Queue.h"
template
class QueueArray : public Queue {
public:
QueueArray(int maxNumber = Queue::MAX_QUEUE_SIZE);
QueueArray(const QueueArray& other);
QueueArray& operator=(const QueueArray& other);
~QueueArray();
void enqueue(const DataType& newDataItem) throw (logic_error);
DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void putFront(const DataType& newDataItem) throw (logic_error);
DataType getRear() throw (logic_error);
int getLength() const;
void showStructure() const;
private:
int maxSize;
int front;
int back;
DataType* dataItems;
};
#endif
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include
#include
using namespace std;
#pragma warning( disable : 4290 )
//--------------------------------------------------------------------
template
class Queue {
public:
static const int MAX_QUEUE_SIZE = 8;
virtual ~Queue();
virtual void enqueue(const DataType& newDataItem) throw (logic_error) = 0;
virtual DataType dequeue() throw (logic_error) = 0;
virtual void clear() = 0;
virtual bool isEmpty() const = 0;
virtual bool isFull() const = 0;
#if LAB7_TEST2
virtual void putFront(const DataType& newDataItem) throw (logic_error) = 0;
virtual DataType getRear() throw (logic_error) = 0;
#endif
#if LAB7_TEST3
virtual int getLength() const = 0;
#endif
virtual void showStructure() const = 0;
};
template
Queue::~Queue()
// Not worth having a separate class implementation file for the destuctor
{}
#endif // #ifndef QUEUE_H

More Related Content

PPTX
Data Structures asdasdasdasdasdasdasdasdasdasd
PPTX
C++11 - A Change in Style - v2.0
PDF
Alexey Tsoy Meta Programming in C++ 16.11.17
PPTX
Templates in C++
PPT
Whats new in_csharp4
PDF
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
PPT
LinkedQueues.ppt
PPT
LinkedQueues.ppt
Data Structures asdasdasdasdasdasdasdasdasdasd
C++11 - A Change in Style - v2.0
Alexey Tsoy Meta Programming in C++ 16.11.17
Templates in C++
Whats new in_csharp4
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
LinkedQueues.ppt
LinkedQueues.ppt

Similar to Implement the Queue ADT using array – based approach. Using C++ prog.pdf (20)

PPTX
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
PPTX
Queue Implementation Using Array & Linked List
PDF
Ugly code
PPTX
Lecture 26 - OOPSFAST NUCES ISB.ppsx.pptx
PPTX
Anti patterns
PPT
Евгений Крутько, Многопоточные вычисления, современный подход.
PPTX
The presention is about the queue data structure
PDF
DSC program.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PPSX
Scala @ TomTom
PPTX
Structured data type
PDF
Modern c++ Memory Management
PDF
DSU C&C++ Practical File Diploma
PDF
DATA STRUCTURE USING C & C++
DOCX
ADA FILE
PPT
2012 JDays Bad Tests Good Tests
ODP
Scala 2 + 2 > 4
PDF
Apache Commons - Don\'t re-invent the wheel
PDF
Please teach me how to fix the errors and where should be modified. .pdf
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Queue Implementation Using Array & Linked List
Ugly code
Lecture 26 - OOPSFAST NUCES ISB.ppsx.pptx
Anti patterns
Евгений Крутько, Многопоточные вычисления, современный подход.
The presention is about the queue data structure
DSC program.pdf
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
Scala @ TomTom
Structured data type
Modern c++ Memory Management
DSU C&C++ Practical File Diploma
DATA STRUCTURE USING C & C++
ADA FILE
2012 JDays Bad Tests Good Tests
Scala 2 + 2 > 4
Apache Commons - Don\'t re-invent the wheel
Please teach me how to fix the errors and where should be modified. .pdf
Ad

More from sktambifortune (20)

PDF
Your company is preparing to migrate from IPv4 to IPv6, and you are .pdf
PDF
You are burning the latest song you bought on ITunes to a disk. The .pdf
PDF
CASE 3-12 Information System Project Steering Committee The Informat.pdf
PDF
Can you date the latest financial crisis in the United States or in .pdf
PDF
B1. State the components of organization. Give good examples to justi.pdf
PDF
Assignment of SOS operating systemThe file lmemman.c has one incom.pdf
PDF
Any kind of help would gladly be appreciated. (C-programming)Probl.pdf
PDF
Which of the following solutions will turn red litmus blue pOH 1.pdf
PDF
What serves as the most reliable source of information about the .pdf
PDF
What is the difference between the terms “earnings and profits” and .pdf
PDF
what are three effects of transistor scaling on computer architectur.pdf
PDF
What are some of the motives for employee theft What are some .pdf
PDF
Twitter is a popular social media. It allows its users to exchange tw.pdf
PDF
A. State the domai and ranga. 1. y find the inverse and state the dom.pdf
PDF
The Puritan faith community shaped the New England colonies in virtu.pdf
PDF
savings account d. the value of the shares is based on the amount of .pdf
PDF
QuestionIt was reported on June 11, 1997, by NBC Nightly News that.pdf
PDF
946 LTE Labs Le.chateliers-lab.pdf Before beginning this experiment.pdf
PDF
Prove that the T_i-property is a topological property for i = 0So.pdf
PDF
4. Refer to the table of Gini coefficients in the Added Dimension box.pdf
Your company is preparing to migrate from IPv4 to IPv6, and you are .pdf
You are burning the latest song you bought on ITunes to a disk. The .pdf
CASE 3-12 Information System Project Steering Committee The Informat.pdf
Can you date the latest financial crisis in the United States or in .pdf
B1. State the components of organization. Give good examples to justi.pdf
Assignment of SOS operating systemThe file lmemman.c has one incom.pdf
Any kind of help would gladly be appreciated. (C-programming)Probl.pdf
Which of the following solutions will turn red litmus blue pOH 1.pdf
What serves as the most reliable source of information about the .pdf
What is the difference between the terms “earnings and profits” and .pdf
what are three effects of transistor scaling on computer architectur.pdf
What are some of the motives for employee theft What are some .pdf
Twitter is a popular social media. It allows its users to exchange tw.pdf
A. State the domai and ranga. 1. y find the inverse and state the dom.pdf
The Puritan faith community shaped the New England colonies in virtu.pdf
savings account d. the value of the shares is based on the amount of .pdf
QuestionIt was reported on June 11, 1997, by NBC Nightly News that.pdf
946 LTE Labs Le.chateliers-lab.pdf Before beginning this experiment.pdf
Prove that the T_i-property is a topological property for i = 0So.pdf
4. Refer to the table of Gini coefficients in the Added Dimension box.pdf
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
O5-L3 Freight Transport Ops (International) V1.pdf
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Presentation on HIE in infants and its manifestations
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra

Implement the Queue ADT using array – based approach. Using C++ prog.pdf

  • 1. Implement the Queue ADT using array – based approach. Using C++ programming Language #include "QueueArray.h" template QueueArray::QueueArray(int maxNumber) { } template QueueArray::QueueArray(const QueueArray& other) { } template QueueArray& QueueArray::operator=(const QueueArray& other) { } template QueueArray::~QueueArray() { } template void QueueArray::enqueue(const DataType& newDataItem) throw (logic_error) { } template DataType QueueArray::dequeue() throw (logic_error) { DataType temp; return temp; } template void QueueArray::clear() { } template bool QueueArray::isEmpty() const
  • 2. { return false; } template bool QueueArray::isFull() const { return false; } template void QueueArray::putFront(const DataType& newDataItem) throw (logic_error) { } template DataType QueueArray::getRear() throw (logic_error) { DataType temp; return temp; } template int QueueArray::getLength() const { return -1; } //-------------------------------------------------------------------- template void QueueArray::showStructure() const // Array implementation. Outputs the data items in a queue. If the // queue is empty, outputs "Empty queue". This operation is intended // for testing and debugging purposes only. { int j; // Loop counter if ( front == -1 ) cout << "Empty queue" << endl; else { cout << "Front = " << front << " Back = " << back << endl;
  • 3. for ( j = 0 ; j < maxSize ; j++ ) cout << j << "t"; cout << endl; if ( back >= front ) for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) && ( j <= back ) ) cout << dataItems[j] << "t"; else cout << " t"; else for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) || ( j <= back ) ) cout << dataItems[j] << "t"; else cout << " t"; cout << endl; } } QueueArray.h ___----------------------------------------------------------------------------- #ifndef QUEUEARRAY_H #define QUEUEARRAY_H #include #include using namespace std; #include "Queue.h" template class QueueArray : public Queue { public: QueueArray(int maxNumber = Queue::MAX_QUEUE_SIZE); QueueArray(const QueueArray& other); QueueArray& operator=(const QueueArray& other); ~QueueArray(); void enqueue(const DataType& newDataItem) throw (logic_error); DataType dequeue() throw (logic_error); void clear();
  • 4. bool isEmpty() const; bool isFull() const; void putFront(const DataType& newDataItem) throw (logic_error); DataType getRear() throw (logic_error); int getLength() const; void showStructure() const; private: int maxSize; int front; int back; DataType* dataItems; }; #endif Solution QueueArray.cpp #include "QueueArray.h" template QueueArray::QueueArray(int maxNumber) { maxSize = maxNumber; this->dataItems = new DataType[maxSize]; front = -1; back = -1; } template QueueArray::QueueArray(const QueueArray& other) { dataItems = new DataType[maxSize]; for (int i = 0; i <= top; i++) { dataItems[i] = other.dataItems[i]; } }
  • 5. template QueueArray& QueueArray::operator=(const QueueArray& other) { if (maxSize < other.maxSize) { delete[] dataItems; dataItems = new dataType[other.maxSize]; } maxSize = other.maxSize; top = other.top; for (int i = 0; i <= top; i++) { dataItems[i] = other.dataItems[i]; } return *this; } template QueueArray::~QueueArray() { this->clear(); } template void QueueArray::enqueue(const DataType& newDataItem) throw (logic_error) { if (this->isFull()) throw logic_error("Queue is Full."); if (this->isEmpty()) { dataItems[back + 1] = newDataItem; back = (++back) % maxSize; front++; } else if (back == 7 && !isFull()) {
  • 6. back = 0; dataItems[back] = newDataItem; } else { back = (++back) % maxSize; dataItems[back] = newDataItem; } } template DataType QueueArray::dequeue() throw (logic_error) { if (this->isEmpty()) throw logic_error("Queue is empty. "); if (getLength() == 1) { DataType temp = dataItems[this->front]; clear(); return temp; } else { DataType temp; temp = dataItems[this->front]; front++; return temp; } } template void QueueArray::clear() { front = -1; back = -1; } template bool QueueArray::isEmpty() const { return (front == -1);
  • 7. } template bool QueueArray::isFull() const { return (getLength() == this->maxSize); } template void QueueArray::putFront(const DataType& newDataItem) throw (logic_error) { if (isFull()) throw logic_error("Queue is Full."); else { if (isEmpty()) { front = 0; back = 0; } else { front--; if (front < 0) front = (maxSize - 1); } dataItems[front] = newDataItem; } } template DataType QueueArray::getRear() throw (logic_error) { int cursor = -1; if (isEmpty()) { throw logic_error("Queue is Empty."); } else { cursor = back; if (front == back) {
  • 8. front = -1; back = -1; } else { --back; if (back < 0) back = (maxSize - 1); } return dataItems[cursor]; } } template int QueueArray::getLength() const { if (front > back) return (back - front + maxSize + 1); else return back - front + 1; } //-------------------------------------------------------------------- template void QueueArray::showStructure() const // Array implementation. Outputs the data items in a queue. If the // queue is empty, outputs "Empty queue". This operation is intended // for testing and debugging purposes only. { int j; // Loop counter if (front == -1) cout << "Empty queue" << endl; else { cout << "Front = " << front << " Back = " << back << endl; for (j = 0; j < maxSize; j++) cout << j << "t";
  • 9. cout << endl; if (back >= front) for (j = 0; j < maxSize; j++) if ((j >= front) && (j <= back)) cout << dataItems[j] << "t"; else cout << " t"; else for (j = 0; j < maxSize; j++) if ((j >= front) || (j <= back)) cout << dataItems[j] << "t"; else cout << " t"; cout << endl; } } QueueArray.h // QueueArray.h #ifndef QUEUEARRAY_H #define QUEUEARRAY_H #include #include using namespace std; #include "Queue.h" template class QueueArray : public Queue { public: QueueArray(int maxNumber = Queue::MAX_QUEUE_SIZE); QueueArray(const QueueArray& other); QueueArray& operator=(const QueueArray& other); ~QueueArray(); void enqueue(const DataType& newDataItem) throw (logic_error); DataType dequeue() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const;
  • 10. void putFront(const DataType& newDataItem) throw (logic_error); DataType getRear() throw (logic_error); int getLength() const; void showStructure() const; private: int maxSize; int front; int back; DataType* dataItems; }; #endif Queue.h #ifndef QUEUE_H #define QUEUE_H #include #include using namespace std; #pragma warning( disable : 4290 ) //-------------------------------------------------------------------- template class Queue { public: static const int MAX_QUEUE_SIZE = 8; virtual ~Queue(); virtual void enqueue(const DataType& newDataItem) throw (logic_error) = 0; virtual DataType dequeue() throw (logic_error) = 0; virtual void clear() = 0; virtual bool isEmpty() const = 0; virtual bool isFull() const = 0; #if LAB7_TEST2 virtual void putFront(const DataType& newDataItem) throw (logic_error) = 0; virtual DataType getRear() throw (logic_error) = 0; #endif #if LAB7_TEST3 virtual int getLength() const = 0;
  • 11. #endif virtual void showStructure() const = 0; }; template Queue::~Queue() // Not worth having a separate class implementation file for the destuctor {} #endif // #ifndef QUEUE_H