我正在完成一项家庭作业,并且几乎完成了所有工作.我在代码中的某个部分遇到问题.我尝试了一些东西,似乎无法弄清楚我做错了什么或者我需要做些什么来让它发挥作用.
我正在尝试实现一个队列,该队列接收一个名为QueueData的特定对象.当我执行程序时,它将一直运行到P2Queue.cpp文件中的第44行.此时代码中的意图是将新的QueueData对象添加到尾部的队列中,然后将temp的指针设置为null,表示队列的结束.程序停止在文件P2Queue.cpp中的第44行运行.我做错了什么,我需要改变什么,这就是杀了我.
提前致谢!如果您需要进一步说明,请告诉我.
P2Queue.cpp
#include"P2Queue.h"
#include<iostream>
#include<string.h>
using namespace std;
P2Queue::P2Queue() //: head(NULL), tail(NULL), count(0)
{
head=NULL;
tail=NULL;
count=0;
}
P2Queue::~P2Queue()
{
QueueData* temp;
while(head!=NULL)
{
temp=head;
head=temp->next;
delete temp;
}
head=tail=NULL;
}
void P2Queue::Enqueue(int x, char *y)
{
cout<<"I'm HERE in the function call"<<endl;
QueueData* temp;
temp=new QueueData();
temp->num=x;
cout<<temp->num<<endl;
strcpy(temp->data, y);
cout<<temp->data<<endl;
temp->next=NULL;
cout<<"LALALA"<<endl;
tail->next=temp;
cout<<"I'm here11!!"<<endl;
tail=temp;
cout<<"I'm here!!"<<endl;
}
void P2Queue::Enqueue(QueueData* ptr)
{
tail->next=ptr;
tail=ptr;
tail->next=NULL;
}
QueueData* P2Queue::Dequeue()
{
QueueData* temp;
temp=new QueueData();
temp=head;
head=temp->next;
return temp;
}
int P2Queue::QueueSize()
{
QueueData* temp;
temp=head;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
delete temp;
return count;
}
QueueData* P2Queue::getHead()
{
return head;
}
QueueData* P2Queue::getTail()
{
return tail;
}
P2Queue.h
#ifndef P2QUEUE_H
#define P2QUEUE_H
struct QueueData
{
int num;
char data[128];
QueueData* next;
};
class P2Queue
{
private:
int count;
QueueData* head;
QueueData* tail;
public:
QueueData* getHead();
QueueData* getTail();
P2Queue();
~P2Queue();
void Enqueue(int x, char* y);
void Enqueue(QueueData* ptr);
QueueData* Dequeue();
int QueueSize();
};
#endif
BSSim.cpp
#include"BSSim.h"
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <time.h>
using namespace std;
BSSim::BSSim()
{
A = new P2Queue();
B = new P2Queue();
C = new P2Queue();
}
BSSim::~BSSim()
{
delete A;
delete B;
delete C;
}
bool BSSim::getNextLine(char *line, int lineLen)
{
bool done = false;
while(!done)
{
inFile.getline(line, lineLen); // Read a line from the file
// Note: inFile is a private class variable
if(inFile.good()) // If a line was successfully read
{
if(strlen(line) == 0) // Skip any blank lines
continue;
else if(line[0] == '#') // Skip any comment lines
continue;
else done = true; // Got a valid data line so return with this line
}
else
{
strcpy(line, ""); // Clear the buffer array
return false; // Flag end of file
}
} // end while
return true; // Flag a successful read
}
bool BSSim::runSimulation(char *cmdFile)
{
inFile.open(cmdFile, ifstream::in);
int x;
char ch;
char ch2;
char cArray[32];
char str[128];
struct _timeb tStruct;
double currenttime;
double nexttime=0;
bool done = false;
if(!inFile.is_open()) // If the file was not opened successfully, bail out.
{
// inFile.is_open() returns false if the file could not be found or
// if for some other reason the open failed.
cout << "Unable to open command file.\nProgram terminating.\n";
return 0;
}
else
{
while(!done)
{
_ftime(&tStruct);
currenttime = (tStruct.time*1000) + tStruct.millitm;
if(currenttime>=nexttime)
{
getNextLine(line, 128);
sscanf(line, "%s", cArray);
cout<<cArray<<endl;
cout<<"I'm here!"<<endl;
if(strcmp(cArray, "ENQUEUE")==0)
{
cout<<"I'm in the Enqueue if"<<endl;
sscanf(line, "%s %d %s", cArray, &x, str);
cout<<"HERE!"<<endl;
cout<<x<<" "<<str<<endl;
A->Enqueue(x, str);
cout<<"Enqueue A - ID="<<x<<", Data="<<str<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
else if(strcmp("DEQUEUE", cArray)==0)
{
sscanf(line, "%s %c", cArray, &ch);
if(ch=='A')
{
if(A->QueueSize()==0)
{
cout<<"Queue A is empty"<<endl;
}
else
{
sscanf(line, "%s %c %s %c", cArray, &ch, str, &ch2);
if(ch2=='B')
{
B->Enqueue(A->Dequeue());
cout<<"Dequeue from A into B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
else
{
C->Enqueue(A->Dequeue());
cout<<"Dequeue from A into C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
if(ch=='B')
{
if(B->QueueSize()==0)
{
cout<<"Queue B is empty"<<endl;
}
else
{
B->Dequeue();
cout<<"Dequeue from B - ID="<<B->getTail()->num<<", Data="<<B->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
else
{
if(C->QueueSize()==0)
{
cout<<"Queue C is empty"<<endl;
}
else
{
C->Dequeue();
cout<<"Dequeue from C - ID="<<C->getTail()->num<<", Data="<<C->getTail()->data<<", A="<<A->QueueSize()<<", B="<<B->QueueSize()<<", C="<<C->QueueSize()<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
}
else if(strcmp("NOACTION", cArray)==0)
{
cout<<"NO ACTION"<<endl;
nexttime=currenttime;
nexttime+=0.5;
}
}
else
{
inFile.close();
cout<<"The simulation has terminated normally."<<endl;
return done;
}
}
}
}
}
BSSim.h
#ifndef BSSIM_H
#define BSSIM_H
#include"P2Queue.h"
#include<iostream>
#include<fstream>
using namespace std;
class BSSim
{
private:
P2Queue* A;
P2Queue* B;
P2Queue* C;
ifstream inFile;
char line[128];
public:
BSSim();
~BSSim();
bool runSimulation(char* cmdFile);
bool getNextLine(char* line, int lineLen);
};
#endif
#include"BSSim.h"
#include"P2Queue.h"
using namespace std;
void main()
{
BSSim x;
x.runSimulation("SimData.txt");
}
Example data file:
ENQUEUE 1234 QueueTest_01
ENQUEUE 2345 QueueTest_02
ENQUEUE 3456 QueueTest_03
ENQUEUE 4567 QueueTest_04
ENQUEUE 5678 QueueTest_05
ENQUEUE 6789 QueueTest_06
NOACTION
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE A ENQUEUE B
DEQUEUE A ENQUEUE C
DEQUEUE B
DEQUEUE C
DEQUEUE B
DEQUEUE C
NOACTION
最佳答案 我相信你的问题就像其他人所说的那样,当队列为空时你的程序没有处理.如果队列为空,您需要做的是检查函数.如果队列为空,则需要以不同的方式处理Enqueue.我相信这会解决你的问题.下面是一些可能对您有帮助的伪代码.如果这有帮助,请告诉我! < 3
void P2Queue::Enqueue(int x, char *y)
{
QueueData* temp;
temp=new QueueData();
temp->num=x;
strcpy(temp->data, y);
temp->next=NULL;
if(queue is empty) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
}
void P2Queue::Enqueue(QueueData* ptr)
{
ptr->next = NULL
if(queue is empty) {
head = ptr;
tail = ptr;
}
else {
tail->next = ptr;
tail = ptr;
}
}