C++ please leave comments CSDP250 Fall, 2018 Program 02 due: September 17, 2018
ID: 3748838 • Letter: C
Question
C++ please leave commentsCSDP250 Fall, 2018 Program 02 due: September 17, 2018 Thi s program is an exercise template functions. Pass in your completed program (.cpp file only) to albertcasavant@ gmail.com with the subject line: 250 Program 02. in using template classes and For this program you either integers, characters, or doubles. A queue is a data structure that accepts new items to be stored at the front of the queue and takes away items previously stored from the back of the queue. Thus, this is a FIFO, first in first out data structure, method of storing items. will implement a queue that can contain You will declare a template class for the queue. The queue will be implemented using an array of size 3. The array will be used as a circular array. This means that, for example, if the current frontIndex of the array is 2, and another item must be added to the queue, then has 3 elements, the index must go from 2 to 0. backIndex works the same way. t frontlndex must be incremented. Since the array This is implemented using the modulus operator The class will have 3 data members: an array of SIZE 3 called q. two indexes for use with the array frontlndex and backIndex The class will have three member functions: e a constructor a function putFront(T i that will put a new item on the front of the queue i is the item to be stored. Type T can be integer, character, or double a function getBack0 that will get a previously stored item from the back of the queue. In the constructor you will initially set frontindex and that the queue
Explanation / Answer
//C++ program
#include<iostream>
using namespace std;
template<typename T>
class queue{
T *arr;
int front,back;
int capacity;
public :
queue(int size){
capacity=size;
arr= new T[size];
front =-1;
back =-1;
}
void putFront(T i){
if(front<capacity-1)
arr[++front]=i;
else {
cout<<"Queue full ";
return;
}
}
bool isempty(){
return front==back;
}
T getBack(){
if((this->isempty())==false){
return arr[++back];
}
}
};
int main(){
queue <int > q(2);
q.putFront(10);
q.putFront(20);
q.putFront(30);
q.putFront(40);
while(!q.isempty()){
cout<<q.getBack()<<" ";
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.