如何从FIFO队列阵列打印



我写了一个FIFO队列,但一直收到调试错误;MS VS2008说:

"此应用程序已请求运行时以异常方式终止它"在调试器中:fifoQueue.exe中0x7c81eb33处未处理的异常:内存位置0x0012f340处的Microsoft C++异常:std::bad_alloc..

问题似乎来自程序的最后一部分,我试图将存储在FIFO队列阵列中的信息打印出来。它只打印第一个项目,然后崩溃。

#include <iostream>
#include <fstream>
#include <string>
#include "QueType.h"
#include <queue>
using namespace std;
struct movieType
{ 
string name;
char genre;
};
int main()
{
movieType movie[3];
movie[0].name="snatch"; movie[0].genre='A';
movie[1].name="layer cake "; movie[1].genre='A';
movie[2].name="rasing twins "; movie[2].genre='C';
queue<movieType> PQqueue;
for (int i=0;i<3;++i)
    PQqueue.push(movie[i]);
QueType<movieType> fifoQueue[3];
string Genre[3]={"Action", "Comedy", "Drama"};
movieType it;
while (!PQqueue.empty())
{
    it=PQqueue.front();
    PQqueue.pop();
    if (it.genre == 'A')
        fifoQueue[0].Enqueue(it);
    else if (it.genre == 'C' )
        fifoQueue[1].Enqueue(it);
    else if (it.genre == 'D')
        fifoQueue[2].Enqueue(it);
}
//!!! problem seems to come from here
movieType ij;
for (int i=0; i<3; ++i)
{
    while(!fifoQueue[i].IsEmpty())
    {
        fifoQueue[i].Dequeue(ij);
        cout<<ij.name<<endl;
    }
}
return 0;
}

以及QueType.h 的实现文件

#include <cstddef> //for NULL
#include <new> // for bad_alloc
//definition of NodeType
template <class ItemType>
struct NodeType
{
ItemType info; // store data
NodeType* next; // sotre location of data
};
//exception class used when queue is full
class FullQueue
{};
//Exception class used when queue is empty
class EmptyQueue
{};
//templated queue class
template<class ItemType>
class QueType
{
public:
    QueType();
    //Function: class constructor
    //Precondition: none
    //Postcondition: it initializes the pointers, front and rear to null
    ~QueType();
    //Function:class destructor
    //Precondition: queue has been initialized
    //Postcondition: deallocate allocated memory
    void MakeEmpty();
    //Function: determines whether the queue is empty
    //Precondition: queue has been initialized
    //Postcondition:queue is empty
    bool IsEmpty() const;
    //Function:determines whether the queue is empty
    //Precondition:queue has been initialized
    //Postcondition:Function value = (queue is empty)
    bool IsFull() const;
    //Function:determines whether the queue is full
    //Precondition:queue has been initialized
    //Postcondition:Function value = (queue is full)
    void Enqueue(ItemType newItem);
    //Function:Adds newItem to the rear of the queue
    //Precondition:queue has been initialized
    //Postcondition:if (queue is full), FullQueue exception is thrown,
    //else newItem is at rear of queue
    void Dequeue(ItemType& item);
    //Function:removes front item from the queue and returns it in item
    //Precondition:queue has been initialized
    //Postcondition:if (queue is empty), EmptyQueue exception is thrown
    //and item is undefines, else front element has been removed from
    //queue and item is a copy of removed element
private:
    NodeType<ItemType>* front; //pointer points to the front to the queue
    NodeType<ItemType>* rear; // pointer points to the rear of the queue
};

template<class ItemType>
QueType<ItemType>::QueType()
{
front = NULL;
rear = NULL;
}
template <class ItemType>
QueType<ItemType>::~QueType()
{
MakeEmpty();
}
template <class ItemType>
void QueType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;//temporary pointer
while(front != NULL)
{
    tempPtr=front;
    front = front->next;
    delete tempPtr;
}
rear = NULL;
} 
template <class ItemType>
bool QueType<ItemType>::IsEmpty() const
{
return (front == NULL);
}
template <class ItemType>
bool QueType<ItemType>::IsFull() const
{
NodeType<ItemType>* location;
try
{
    location = new NodeType<ItemType>;
    delete location;
    return false;
}
catch(std::bad_alloc exception)
{
    return true;
}
}
template <class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem)
{
if (IsFull())
        throw FullQueue();
else
{
    NodeType<ItemType>* newNode;
    newNode = new NodeType<ItemType>;
    newNode ->info=newItem;
    newNode->next=NULL;
    if(rear== NULL)
            front= newNode;
    else
            rear->next=newNode;
    rear=newNode;
}
}
template <class ItemType>
void QueType<ItemType>::Dequeue(ItemType &item)
{
if(IsEmpty())
    throw EmptyQueue();
else
{
    NodeType<ItemType>* tempPtr;
    tempPtr = front;
    item= front->info;
    if(front== NULL)
            rear=NULL;
    delete tempPtr;
}
}

Dequeue函数中,您删除了front指向的对象,但从未将front设置为NULL,因此IsEmpty()方法在不应该设置时返回false。

NodeType<ItemType>* tempPtr;
tempPtr = front;
item= front->info;
if(front== NULL)
        rear=NULL;
delete tempPtr; // deleted object pointed at by front, but front not set to NULL

来自Dequeue函数:

NodeType<ItemType>* tempPtr;
tempPtr = front;
item= front->info;
if(front== NULL)
        rear=NULL;
delete tempPtr;

实际上,你从来没有出列任何东西,你只是释放了前面的指针。因此,当您循环时,IsEmpty将返回false,因此您将再次调用Dequeue,并再次访问和释放您已经释放的指针。

最新更新