Help! C++: Generic Queue Template

Bluga

Banned
Nov 28, 2000
4,315
0
0
I'm having trouble implementing generic queue template using circular array. The size of array is specified as a constructor parameter. I need to keep track of the index of the first and last elements in the array. When removing an element from the front of the array, instead of shifting the rest of the elements towards the front, i increment the index of the first element so that it points to the next element.I'm just trying to enqueue and dequeue now. My code is below, any help is appreciated.

#include <iostream>

template<class T>
class Queue{

public:

Queue();
Queue(int size);
virtual void enqueue(T ob);
virtual T dequeue();

protected:

T* current;
int first;
int last;

template <class T>Queue<t>::Queue(){
return;
}

template <class T>Queue<t>::Queue(int size){

current = new T[size];
}


template <class T>Queue<t>::enqueue(T a){


*current = a;
++current;

if (current ==last){
current = first;
}


}

template <class T>Queue<t>::Queue(){

T output;
output = *current;
--current;

if (current == first-1){
current = last;
--current;
}

return output;

}


int main(int argc, char* argv[]){

Queue <int> myQueue (3);
for (int i=0; i<3; i++){
myQueue.enqueue(i);
}

for (int j=0; j<3; j++){
cout<<myQueue.dequeue()<<endl;
}

 

Squibby

Senior member
Mar 19, 2001
256
0
0
"template<class T>" should be "template <typename T>"

...and T should be renamed, since T doesn't tell you anything about what it does...
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
First thing not sure if this is just a mistake but template<class T> should have a space between it and Queue.
I usually put it on its own line.

Not sure what Squibby is talking about but class T is fine.
Sorry if I cannot be more help on your code. Its always difficult to read on pages like this
and I don't have that much time. However

This is a link to Sartaj Sahni's source code from his book
Data Structures, Algorithms, and Applications in C++
Just download all the chapters There is alot of code so unzip it in its own folder.
queue.h and queue.cpp work. queue.h contains a circular array class and the cpp
is a driver file.
Should give you some insight on what might be wrong.

Sahni's Algorithms

Hope this helps.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |