Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create queue class should have the following methods: void enqueue(T) - add data

ID: 3532711 • Letter: C

Question

create queue class should have the following methods:


void enqueue(T) - add data to the back of the queue

T dequeue() - return the data from the front of the queue and remove it from the queue

int count() - return how many items are in the queue

bool empty() - return true if the queue is empty otherwise return false

Here is what your main program should be:


// put your includes, and comments here

void main()

{   

Queue<string> stringQueue;

stringQueue.enqueue(string("Hello"));

Queue<int> intQueue;

Queue<double> doubleQueue;

for(int i=2;i<=22;i+=4){

intQueue.enqueue(i);

doubleQueue.enqueue(i+0.5);

}

stringQueue.enqueue(string("World"));

while (!stringQueue.empty())

cout << stringQueue.dequeue() << endl;

while (!intQueue.empty())

cout << intQueue.dequeue() << endl;

while (!doubleQueue.empty())

cout << doubleQueue.dequeue() << endl;

}



Here is the expected output:


Hello

World

2

6

10

14

18

22

2.5

6.5

10.5

14.5

18.5

22.5

Explanation / Answer

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 //queue.h #ifndef QUEUE_H_ #define QUEUE_H_   template <typename T> class Queue { private:         struct Node { T item; struct Node * next;};         const static int Q_SIZE = 10;         Node * front;                                                                                           // pointer to front of Queue         Node * rear;                                                                                            // pointer to rear of Queue         int count;                                                                                                        // current number of items in Queue         const int qsize;                                                                                    // maximum number of items in Queue          // preemptive definitions to prevent public copying         Queue(const Queue & q) : qsize(0) {};         Queue & operator=(const Queue & q) {return *this;};  public:         Queue(int qs = Q_SIZE);                                                                           // create queue with a qs limit         ~Queue();         bool isempty() const;         bool isfull() const;         int queuecount() const;         bool enqueue(const T &);                                                                        // add item to end         bool dequeue(T &);                                                                                    // remove item from front };  #endif /* QUEUE_H_ */  
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 //queue.cpp #include "queue.h"  template <typename T> Queue<T>::Queue(int qs) {         qsize = qs;         count = 0;         front = rear = nullptr; }  template <typename T> Queue<T>::~Queue() {         Node * temp;                                            // create temporary address store         while(front != (void *) 0)                  // while the queue is not empty         {                 temp = front;                 front = front->next;                 // advance the front object to the next                 delete temp;                                      // delete the temporary data         } }  template <typename T> bool Queue<T>::isempty() const {         return count == 0; }  template <typename T> bool Queue<T>::isfull() const {         return count == qsize; }  template <typename T> int Queue<T>::queuecount() const {         return count; }  template <typename T> bool Queue<T>::enqueue(const T &data)     // add item to end {         if(isfull())                                              // if queue is full halt queuing                 return false;          Node * add = new Node;                            // create node         add->item = data;                                    // set node pointers         add->next = (void *) 0;                                // or nullptr;         count++;         if (front == (void *) 0)                    // if queue is empty,                 front = add;                                    // place item at front         else                 rear->next = add;                            // else place at rear         rear = add;                                                     // have rear point to new node          return true; }  template <typename T> bool Queue<T>::dequeue(T &data)                 // remove item from front {         if(front == (void *) 0)                             // front node is empty, queue is empty                 return false;          data = front->item;                                  // set data to first item in queue         count--;                                                        // decrement item count         Node * temp = front;                            // save location of first item         front = front->next;                                 // reset front to next item         delete temp;                                              // delete former first item          if (count == 0)                                           // if the queue is now empty set rear to point to nothing                 rear = (void *)0;          return true;  } 
 1
2
3
4
5
6
7
8
9
10
11
 //main.cpp #include "queue.h" const int SIZE = 5;  int main() {         Queue<int> tempint(SIZE);          return 0; } 
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  //queue.h  #ifndef QUEUE_H_  #define QUEUE_H_      template <typename T>  class Queue  {  private:          struct Node { T item; struct Node * next;};          const static int Q_SIZE = 10;          Node * front;                                                                                           // pointer to front of Queue          Node * rear;                                                                                            // pointer to rear of Queue          int count;                                                                                                        // current number of items in Queue          const int qsize;                                                                                    // maximum number of items in Queue            // preemptive definitions to prevent public copying          Queue(const Queue & q) : qsize(0) {};          Queue & operator=(const Queue & q) {return *this;};    public:          Queue(int qs = Q_SIZE);                                                                           // create queue with a qs limit          ~Queue();          bool isempty() const;          bool isfull() const;          int queuecount() const;          bool enqueue(const T &);                                                                        // add item to end          bool dequeue(T &);                                                                                    // remove item from front  };    #endif /* QUEUE_H_ */