0

我必须从数据文件创建一个队列类,它基本上是一个两列 .dat 文件,第一列中有一个字符,第二列中有一个相应的数字。在这个程序中有两个队列,高优先级和低优先级。

* 星号和相关数字表示从队列中删除它们的次数,交替从高队列和低队列中删除。

我可以毫无问题地填充队列(高优先级只会达到高等) - 但是当我从队列中删除字符时,它会从队列中删除它不应该访问的字符。

main.cpp 的片段:

  //This is the main.cpp where characters are sent into the queue.

    if (number1 == 1 && character != '*')  //if number is 1, sends to high priority queue
    { 
        myHigh.addToQueue(character);  
    } 

    else if (number1 == 2 || number1 == 3 ||number1 == 4 || number1 == 5 && character != '*') 
    {
        myLow.addToQueue(character);
    }

    for (int i=1; i<=number1; i++) 
    {
        if (character == '*') 
        {      
            myHigh.takeAway();                
            myLow.takeAway();
            number1/=i;
            i=1;
        }         
    }

arrayQueue 类的片段:

//this is in the arrayQueue class, it should remove values from the queue
int arrayQueue::takeAway()
{
    char letter;

    if (empty())
        return -1;
    else 
    {
        letter = queueArray[remove];    
        remove = (remove+1)%maxSize;
        count--;
        size--;
        cout << letter << " has been serviced and removed from the queue" << endl;
        return letter;        
    }
}

现在,如果你只使用 myHigh.takeAway(); 这一切都很好。- 值被正常删除。但是,如果您使用 myLow.takeAway(); -- 即使没有 myHigh.takeAway() 它也会删除应该只在高优先级队列中的值。

这是仅运行 myLow.takeAway() 的输出的一部分,W 在高优先级队列中,但是在运行 myLow.takeAway() 时它被删除了吗?

//OUTPUT
R has been added to the queue and is assigned number 1
T has been added to the queue and is assigned number 2
W has been added to the queue and is assigned number 1
A has been added to the queue and is assigned number 3
W has been serviced and removed from the queue
T has been serviced and removed from the queue

我希望我包含的内容足以让任何人了解正在发生的事情,非常感谢任何帮助!

4

0 回答 0