Queue (queue) features:
first-in first-out
Array implementation:
array: store element
i: the index of the position of the next enqueue to store the element
j: the following table of the position of the next dequeue to delete the element
size: the maximum length of the queue
num: the current number of elements in the queue
#include <iostream>
using namespace std;
template<typename T>
class queue{
public:
queue(int size=0){
array = new T[size];
i = 0;
j = 0;
num = 0;
this->size = size;
}
void push(T val){
if(num < size && i < size - 1){
array[i] = val;
i++;
num++;
}else if(num < size && i == size - 1){
array[0] = val;
i = 1;
}
}
void pop(){
if(j == size - 1){
j = 0;
}else{
j++;
}
num--;
}
int size(){
return num;
}
int capacity(){
return size;
}
T front(){
return array[j];
}
~queue(){
delete[] array;
}
protected:
T *array;
int i;
int j;
int size;
int num;
};
int main(){
queue<int> q(20);
q.push(21);
cout << q.front() << endl;
q.push(31);
cout << q.front() << endl;
q.push(41);
q.pop();
cout << q.front() << endl;
return 0;
}