返回信息流实现链表式队列,出现的问题。是在初始化函数中。其他函数都没有错(应该是这样的)
Queue::Queue(const Queue& aQueue)
{
// 一下是出错的程序,加了断点,错误出现在:backPtr->next=curPtr;这行
// 我的想法是在遍历过程中每次设一个curPtr,假如队列没有初始化,就是frontPtr和
// backPtr都为空,则把frontPtr和backPtr都指向curPtr;假如都不为空,则进行else里的
// 步骤。但是是错误的,运行不通过。
/*
for(QueueNode *tempPtr=aQueue.frontPtr;tempPtr!=NULL;tempPtr=tempPtr->next)
{
QueueNode *curPtr=new QueueNode;
curPtr->item=tempPtr->item;
curPtr->next=NULL;
if (frontPtr==NULL)
{
frontPtr=curPtr;
backPtr=curPtr;
}
else
{
backPtr->next=curPtr;
backPtr=curPtr;
}
}
*/
/*一下是正确的代码,是模仿书上链表部分写的。真的不知道为什么上面错了*/
if (aQueue.frontPtr==NULL)
{
frontPtr=NULL;
backPtr=NULL;
}
else
{
frontPtr=new QueueNode;
frontPtr->item=aQueue.frontPtr->item;
QueueNode *newPtr=frontPtr;
for (QueueNode *origPtr=aQueue.frontPtr->next;
origPtr!=NULL;
origPtr=origPtr->next)
{
newPtr->next=new QueueNode;
newPtr=newPtr->next;
newPtr->item=origPtr->item;
}
newPtr->next=NULL;
backPtr=newPtr;
}
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #29013同步于 2009/9/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
求助,队列初始化函数出现问题,不解。
zcsky12345
2009/9/23镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
完整代码:
QueueException.h:
#include "stdexcept"
#include "string"
using namespace std;
class QueueException:public logic_error
{
public:
QueueException(const string& message=""):logic_error(message.c_str())
{
}
}; //end ListIndexOutOfRangeException
QueueP.h:
#include "QueueException.h"
typedef int QueueItemType;
class Queue
{
public:
Queue();
Queue(const Queue & aQueue);
~Queue();
bool isEmpty() const;
void enqueue(QueueItemType newItem)
throw (QueueException);
void dequeue()
throw (QueueException);
void dequeue(QueueItemType& newItem)
throw (QueueException);
void getFront(QueueItemType& newItem) const
throw (QueueException);
void display();
private:
struct QueueNode
{
QueueItemType item;
QueueNode *next;
};
QueueNode* backPtr;
QueueNode* frontPtr;
};
QueueP.cpp:
#include "QueueP.h"
#include "iostream"
#include "cstddef" //for NULL
#include "cassert"
Queue::Queue():backPtr(NULL),frontPtr(NULL)
{}
Queue::Queue(const Queue& aQueue)
{
// 一下是出错的程序,加了断点,错误出现在:backPtr->next=curPtr;这行
// 我的想法是在遍历过程中每次设一个curPtr,假如队列没有初始化,就是frontPtr和
// backPtr都为空,则把frontPtr和backPtr都指向curPtr;假如都不为空,则进行else里的
// 步骤。但是是错误的,运行不通过。
/*
for(QueueNode *tempPtr=aQueue.frontPtr;tempPtr!=NULL;tempPtr=tempPtr->next)
{
QueueNode *curPtr=new QueueNode;
curPtr->item=tempPtr->item;
curPtr->next=NULL;
if (frontPtr==NULL)
{
frontPtr=curPtr;
backPtr=curPtr;
}
else
{
backPtr->next=curPtr;
backPtr=curPtr;
}
}
*/
/*一下是正确的代码,是模仿书上链表部分写的。真的不知道为什么上面错了*/
if (aQueue.frontPtr==NULL)
{
frontPtr=NULL;
backPtr=NULL;
}
else
{
frontPtr=new QueueNode;
frontPtr->item=aQueue.frontPtr->item;
QueueNode *newPtr=frontPtr;
for (QueueNode *origPtr=aQueue.frontPtr->next;
origPtr!=NULL;
origPtr=origPtr->next)
{
newPtr->next=new QueueNode;
newPtr=newPtr->next;
newPtr->item=origPtr->item;
}
newPtr->next=NULL;
backPtr=newPtr;
}
}
Queue::~Queue()
{
while(!isEmpty())
{
dequeue();
}
assert(backPtr==NULL&&frontPtr==NULL);
}
bool Queue::isEmpty() const
{
return backPtr==NULL&&frontPtr==NULL;
}
void Queue::enqueue(QueueItemType newItem)throw (QueueException)
{
QueueNode* newPtr=new QueueNode;
if (newPtr==NULL)
{
throw QueueException("QueueException:enqueue cannot allocate memory");
}
else
{
newPtr->item=newItem;
newPtr->next=NULL;
if (isEmpty())
{
frontPtr=newPtr;
}
else
{
backPtr->next=newPtr;
// backPtr=newPtr;
}
backPtr=newPtr;
}
}//end enqueue
void Queue::dequeue() throw (QueueException)
{
if (isEmpty())
{
throw QueueException("QueueException:empty queue,cannot dequeue");
}
else
{
if (backPtr==frontPtr)
{
backPtr=NULL;
// frontPtr=NULL; //假如不写呢?
}
else
{
QueueNode *tempPtr=frontPtr;
frontPtr=frontPtr->next;
tempPtr->next=NULL; //defensive strategy
delete tempPtr;
}
}
}
void Queue::dequeue(QueueItemType& newItem)throw (QueueException)
{
if (isEmpty())
{
throw QueueException("QueueException:empty queue,cannot dequeue");
}
else
{
newItem=frontPtr->item;
dequeue();
}
}
void Queue::getFront(QueueItemType& newItem) const throw (QueueException)
{
if (isEmpty())
{
throw QueueException("QueueException:empty queue,cannot get the front");
}
else
{
newItem=frontPtr->item;
}
}
void Queue::display()
{
if (isEmpty())
{
cout<<"空队列"<<endl;
}
else
{
for(QueueNode* tempPtr=frontPtr;tempPtr!=NULL;tempPtr=tempPtr->next)
{
cout<<tempPtr->item<<endl;
}
}
}
main.cpp:
#include "QueueP.h"
#include "iostream"
using namespace std;
int main()
{
Queue Q;
Q.enqueue(1);
Q.enqueue(2);
Q.enqueue(3);
Q.display();
Q.dequeue();
cout<<endl;
Q.display();
cout<<endl;
Queue Q2(Q);
// Q2(Q);
Q2.display();
return 0;
}