bool queueIsFull(int rearPointer)
{
if(rearPointer==9)
return 1;
else
return 0;
}
void PUSH (int queue[], int value, int *frontPointer, int *rearPointer)
{
*rearPointer++;
if(*frontPointer==-1)
*frontPointer=0;
queue[*rearPointer] = value;
}
bool queueIsEmpty (int frontPointer, int rearPointer)
{
if(rearPointer==(-1) && frontPointer==(-1))
return true;
else
return false;
}
void POP (int* frontPointer, int* rearPointer, int queue[])
{
cout << "\n Deleted element is: " << queue[*frontPointer];
if(*rearPointer==*frontPointer)
{
*rearPointer=-1;
*frontPointer=-1;
}
else
*frontPointer++;
}
int main()
{
int option, value, queue[10];
int rearPointer=-1, frontPointer=-1;
while(1)
{
cout << "\n----------[STATIC QUEUE]--------\nEnter a choice: \n1. PUSH AN ELEMENT\n2. POP AN ELEMENT\n3. EXIT\n";
cin >> option;
switch(option)
{
case 1: if(!queueIsFull(rearPointer))
{
cout << "\nEnter a value to push in queue: ";
cin >> value;
PUSH(queue, value, &frontPointer, &rearPointer);
}
else
cout << "\nqueue is full. Empty the queue elements and try again!";
break;
case 2: if(!queueIsEmpty(frontPointer, rearPointer)) // either of the parameters would do.
POP(&frontPointer, &rearPointer, queue); // queue is optional. You only need it to display thte poped value.
else
cout << "\nqueue is already empty. Enter some elements";
break;
case 3: return 0;
break;
default: cout << "\nWrong choice! Try again.";
break;
}
}
}
キューに値を入力するとプログラムが突然終了するため、どこにも行けず、問題をデバッグすることもできません! そのため、根本的な問題がどこにあるのかを理解することが難しくなっています。なぜそれが起こっているのですか?
また、今後このような問題を解決するためのヒントを教えていただけますか?