我正在做家庭作业,一切都快完成了。我在处理代码中的某个部分时遇到问题。我已经尝试了一些事情,但似乎无法弄清楚我做错了什么或我需要做什么才能让它工作。
我正在尝试实现一个接收名为 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
最佳答案
最初队列是空的,head == tail == NULL。
您将第一个项目排入队列会发生什么?
关于c++ - 队列的指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6577281/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我有一个涉及多台机器、消息队列和事务的问题。因此,例如用户点击网页,点击将消息发送到另一台机器,该机器将付款添加到用户的帐户。每秒可能有数千次点击。事务的所有方面都应该是容错的。我以前从未遇到过这样的事情,但一些阅读表明这是一个众所周知的问题。所以我的问题。我假设安全的方法是使用两阶段提交,但协议(protocol)是阻塞的,所以我不会获得所需的性能,我是否正确?我通常写Ruby,但似乎Redis之类的数据库和Rescue、RabbitMQ等消息队列系统对我的帮助不大——即使我实现某种两阶段提交,如果Redis崩溃,数据也会丢失,因为它本质上只是内存。所有这些让我开始关注erlang和
首先回顾一下拉格朗日定理的内容:函数f(x)是在闭区间[a,b]上连续、开区间(a,b)上可导的函数,那么至少存在一个,使得:通过这个表达式我们可以知道,f(x)是函数的主体,a和b可以看作是主体函数f(x)中所取的两个值。那么可以有, 也就意味着我们可以用来替换 这种替换可以用在求某些多项式差的极限中。方法: 外层函数f(x)是一致的,并且h(x)和g(x)是等价无穷小。此时,利用拉格朗日定理,将原式替换为 ,再进行求解,往往会省去复合函数求极限的很多麻烦。使用要注意:1.要先找到主体函数f(x),即外层函数必须相同。2.f(x)找到后,复合部分是等价无穷小。3.要满足作差的形式。如果是加