如何从FIFO队列数组打印?

时间:2012-08-27 06:29:30

标签: c++ visual-studio queue

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

“此应用程序已请求运行时以不寻常的方式终止它” 在调试器中:fifoQueue.exe中0x7c81eb33处的未处理异常:Microsoft C ++异常:内存位置0x0012f340处的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;
}
}

2 个答案:

答案 0 :(得分:0)

来自Dequeue功能:

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

你实际上从未出列任何东西,你只需要释放前指针。因此,当您循环播放时,IsEmpty将返回false,因此您将再次调用Dequeue,然后再次释放已释放的指针。

答案 1 :(得分:0)

Dequeue函数中删除front指向的对象,但是从未将front设置为NULL,因此IsEmpty()方法会返回当它不应该是假的。

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