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;
}
}
}
我无法到达任何地方,我无法调试问题,因为当我在队列中输入值时,程序会突然终止!这就是为什么很难理解根本问题在哪里!为什么会这样?
另外,你能否给我一些如何在将来解决这些问题的技巧?
答案 0 :(得分:2)
PUSH
函数实际上是递增指针rearPointer
而不是它指向的值。
更改
*rearPointer++;
到
(*rearPointer)++;
答案 1 :(得分:0)
要解决此问题和其他类似问题,您可以在调试器下运行应用程序,我认为调试器将捕获异常并且您可以获得有关错误的一些线索。
如果您使用的是IDE,则可以找到与“使用调试器运行”类似的文本的菜单项,或者您可以先运行该应用程序,然后附加调试器。您可以添加睡眠以延迟,以确保您有时间附加调试器。