C++ で配列を使用して単純な循環キューを実装しようとしています。以下は私のコードです。
#include <iostream>
int pop();
void push(int );
const int arrayLength = 8;
int inputArray[arrayLength] = {0};
int queueFront=0,queueBack=0;
void push(int theElement)
{
//Check if the push causes queue to overflow
if (((queueBack + 1 ) % arrayLength) == queueFront)
{
std::cout<<"Queue is full."<<std::endl;
return ;
}
inputArray[queueBack] = theElement;
queueBack = (queueBack + 1) % arrayLength;
}
int pop()
{
//Check if queue is already empty
if ( queueFront == queueBack )
{
std::cout<<"Queue is empty."<<std::endl;
}
std::cout<<inputArray[queueFront]<<" removed."<<std::endl;
queueFront = (queueFront + 1 ) % arrayLength;
}
int main()
{
for ( int i =0; i < arrayLength; ++i)
{
std::cout<<inputArray[i]<<std::endl;
}
push(1);
push(2);
push(3);
pop();
push(5);
//printing arrayelements
for ( int i =0; i < arrayLength; ++i)
{
std::cout<<inputArray[i]<<std::endl;
}
}
実行すると、次の出力が得られます。
0 0 0 0 0 0 0 0 1 削除しました。1 2 3 5 0 0 0 0
質問 1: 1. pop() 操作でアイテムを実際に削除するにはどうすればよいですか? 2. 私の実装は正しいですか?
ありがとう