Custom class type for queue?

I have the following circular queue code:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream> 
using namespace std; 
   
class Queue { 
    public:
    // Initialize front and rear 
    int rear, front; 
   
    // Circular Queue 
    int size; 
    int *circular_queue; 
   
    Queue(int sz) { 
       front = rear = -1; 
       size = sz; 
       circular_queue = new int[sz]; 
    } 
   void enQueue(int elem); 
    int deQueue(); 
    void displayQueue(); 
}; 
   
/* Function to create Circular queue */
void Queue::enQueue(int elem) 
{ 
    if ((front == 0 && rear == size-1) || (rear == (front-1)%(size-1)))  { 
        cout<<"\nQueue is Full"; 
        return; 
    } 
    else if (front == -1) {     /* Insert First Element */
         front = rear = 0; 
        circular_queue[rear] = elem; 
    } 
   else if (rear == size-1 && front != 0) { 
        rear = 0; 
        circular_queue[rear] = elem; 
    } 
    else {  
        rear++; 
        circular_queue[rear] = elem; 
    } 
}
// Function to delete element from Circular Queue 
int Queue::deQueue() 
{ 
    if (front == -1)  { 
        cout<<"\nQueue is Empty"; 
        return -1; 
    } 
   
    int data = circular_queue[front]; 
    circular_queue[front] = -1; 
    if (front == rear)  { 
        front = -1; 
        rear = -1; 
    } 
    else if (front == size-1) 
        front = 0; 
    else
        front++; 
   
    return data; 
} 
  
//display elements of Circular Queue 
void Queue::displayQueue() 
{ 
    if (front == -1) { 
        cout<<"\nQueue is Empty"<<endl; 
        return; 
    } 
    cout<<"\nCircular Queue elements: "; 
    if (rear >= front) { 
        for (int i = front; i <= rear; i++) 
            cout<<circular_queue[i]<<" "; 
    } 
    Else  { 
        for (int i = front; i < size; i++) 
            cout<<circular_queue[i]<<" "; 
   
        for (int i = 0; i <= rear; i++) 
            cout<<circular_queue[i]<<" "; 
    } 
} 
   
//main program
int main() 
{ 
    Queue pq(5); 
   
    // Insert elements in Circular Queue 
    pq.enQueue(2); 
    pq.enQueue(4); 
    pq.enQueue(6); 
    pq.enQueue(8); 
   
    // Display elements present in Circular Queue 
    pq.displayQueue(); 
   
    // Delete elements from Circular Queue 
    cout<<"\nElement Dequeued = "<<pq.deQueue(); 
    cout<<"\nElement Dequeued = "<<pq.deQueue(); 
   
    pq.displayQueue(); 
   
    pq.enQueue(10); 
    pq.enQueue(12); 
    pq.enQueue(14); 
   
    pq.displayQueue(); 
   
    pq.enQueue(10); 
    return 0; 
}


This works fine if I use primitive datatype int. What if I want to introduce a new class datatype, say the following

1
2
3
4
5
6
class car
{
    public:
        int ID;
        int reschedule;
};


How can I modify the above circular queue code to make it work with my car? I will want the car object to be store inside the circular queue.

Ideally, is it possible to use STL?
Last edited on
The Queue that you have does store int data. Can you tell which int in the code are type of data and which are for integers?

STL has a queue: http://www.cplusplus.com/reference/queue/queue/
and a managed vector with a couple of variables in a little wrapper is all you need for a circular one. If you use the % operator you never need to do all that conditional stuff.
basically every increment becomes rear = (rear+1)%size; and it automatically loops around the circle.

by using a vector, which is already templated, you can insert cars too.
line 16 goes away, its a vector<T> now (T being the standard letter for template types but you can use anything). All your element handlers need to become a T as well.

If being circular is unimportant, skip all that and use the stl queue or other container of your choice (vectors can work like an enhanced stack/queue with full access rights to all elements instead of locked out, not sure if that is useful in your situation or not, sometimes it is).
Last edited on
To change int into any type you want you need templates:
http://www.cplusplus.com/doc/oldtutorial/templates/

Essentially, you declare your Queue as a templated Queue:
1
2
template <typename T>
class Queue<T>{};

Then everywhere you have an int where you want any type(T), you replace the int with T.
Then when you create the Queue, you give it the type you want to use (the compiler enforces this):
 
Queue<car> pq(5); 

Last edited on
line 77: 'else' not 'Else'
Topic archived. No new replies allowed.