- 2

I need a idea for this program

It's time to update your Queue management system. The previous version supports only integer numbers and we need to support more types, such as strings, to store customer names in the queue. Transform the given Queue class to a class template, which can work with different data types.

29th Dec 2020, 6:38 AM
Deepak
Deepak - avatar
18 Answers
+ 13
This code works #include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
3rd Mar 2021, 8:17 AM
Param Dudhe
Param Dudhe - avatar
+ 6
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave");
29th Dec 2020, 6:39 AM
Deepak
Deepak - avatar
+ 2
the correct code is asfollow : #include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
18th Mar 2022, 1:35 PM
TEWODROS WUBETE DESTA
+ 1
#include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; } Good Luck
19th Dec 2021, 2:17 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
Is this working sir
29th Dec 2020, 6:46 AM
Deepak
Deepak - avatar
0
Yes sir absolutely your Right
29th Dec 2020, 6:48 AM
Deepak
Deepak - avatar
0
This program is not working...
3rd Jan 2021, 6:41 AM
Parmeshwar Chavan
Parmeshwar Chavan - avatar
0
Isn't working
8th Feb 2021, 6:17 PM
SANDEEP KUMAR RAY
SANDEEP KUMAR RAY - avatar
0
Because a class name is not a template
8th Feb 2021, 6:17 PM
SANDEEP KUMAR RAY
SANDEEP KUMAR RAY - avatar
0
#include <iostream> using namespace std; //change the class to a template template <class T> class Queue { private: T *arr; int count; public: Queue(int size) { arr = new T[size]; count = 0; } void add(T elem) { arr[count] = elem; count++; } void get(int index) { cout << arr[index]<<endl; } }; int main() { Queue<string> q(4); q.add("James"); q.add("Andy"); q.add("Amy"); q.get(2); Queue <int> n(2); n.add(42); n.add(33); n.get(1); return 0; This code works for me
23rd Aug 2021, 10:51 PM
David Cabezas
David Cabezas - avatar
0
It not working
26th Nov 2021, 5:57 AM
Chandra Bhushan Saran
0
11th Dec 2021, 8:40 AM
110s017034蕭博元
110s017034蕭博元 - avatar
0
this code not working for me sir
28th Jul 2022, 8:02 AM
Tsehay Gurmessa
0
#include <iostream> using namespace std; template<class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int>q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string>q2; q2.add("Da
7th Aug 2022, 4:03 PM
Yana Jain
0
It's correct code
7th Aug 2022, 4:03 PM
Yana Jain
0
#include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; } this is the correct code worked for me
29th Aug 2022, 12:23 AM
Opanuga Stephen
Opanuga Stephen - avatar
0
#include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
18th Feb 2023, 7:41 AM
Budhabhushan Waghmare
Budhabhushan Waghmare - avatar
0
Let me know guys if it works well in your program #include <iostream> using namespace std; template <class T> class Queue { int size; T* queue; public: Queue() { size = 0; queue = new T[100]; } void add(T data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue &obj) { Queue res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; int main() { Queue<int> q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue<string> q2; q2.add("Dave"); q2.add("John"); q2.add("Amy"); q2.print(); return 0; }
7th Apr 2023, 5:33 PM
Pankaj
Pankaj - avatar