Queue
Just like stack I hope you can go ahead and implement queue like you did stack with STL in c++. For reference, just use your favorite search engine.
So here's an implementation of queue with class,
6.11 Insertion of queue
In the insertion method, suppose you have total 3 space. And there's an element on 3 but not 1 and 2. So if you just simply use a linked list then, you will have overflow error. But you can actually utilize that 1 and 2 space. Here's the implementation,
int insert(T item) {
if ((front == 0 && rear == MAX_NUM - 1) || (front == rear + 1)) {
cout << "OVERFLOW!\n";
return -1;
}
if (front == -1) {
front = 0;
rear = 0;
} else if (rear == MAX_NUM - 1) {
rear = 0;
} else {
rear++;
}
arr[rear] = item;
return rear;
}6.12 Deletion of queue
And finally the full queue template
Queue (full code)
Last updated
Was this helpful?