0
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;
        }
    }
}

我无法到达任何地方,也无法调试问题,因为当我在队列中输入值时程序会突然终止!这就是为什么越来越难以理解根本问题在哪里的原因!为什么会这样?

另外,您能否给我一些关于将来如何解决此类问题的提示?

4

2 回答 2

2

PUSH函数实际上是递增指针rearPointer而不是它指向的值。

改变

*rearPointer++;

(*rearPointer)++;
于 2013-07-07T16:42:19.930 回答
0

要解决此问题和其他类似问题,您可以在调试器下运行应用程序,我认为调试器会捕获异常,您可以获得一些关于问题所在的线索。

如果您使用的是 IDE,您可以找到与“使用调试器运行”类似文本的菜单项,或者您可以先运行应用程序,然后附加调试器。您可以添加 sleep for delay 以确保您有时间附加调试器。

于 2013-07-07T16:53:34.153 回答