需要帮助,需要计算链表中负值的数量,C++; 我正在通过一个在线课程的作业,我需要一些帮助编程来计算C++中链表中的负值的数量。
以下是我现在拥有的代码,以及我的一个尝试:需要帮助,需要计算链表中负值的数量,C++; 我正在通过一个在线课程的作业,我需要一些帮助编程来计算C++中链表中的负值的数量。,c++,C++,以下是我现在拥有的代码,以及我的一个尝试: #include <iostream> #include <cstdlib> using namespace std; class IntNode { public: IntNode(int dataInit = 0, IntNode* nextLoc = nullptr); void InsertAfter(IntNode* nodePtr); IntNode* GetNext(); int GetD
#include <iostream>
#include <cstdlib>
using namespace std;
class IntNode {
public:
IntNode(int dataInit = 0, IntNode* nextLoc = nullptr);
void InsertAfter(IntNode* nodePtr);
IntNode* GetNext();
int GetDataVal();
private:
int dataVal;
IntNode* nextNodePtr;
};
// Constructor
IntNode::IntNode(int dataInit, IntNode* nextLoc) {
this->dataVal = dataInit;
this->nextNodePtr = nextLoc;
return;
}
/* Insert node after this node.
* Before: this -- next
* After: this -- node -- next
*/
void IntNode::InsertAfter(IntNode* nodeLoc) {
IntNode* tmpNext = nullptr;
tmpNext = this->nextNodePtr; // Remember next
this->nextNodePtr = nodeLoc; // this -- node -- ?
nodeLoc->nextNodePtr = tmpNext; // this -- node -- next
return;
}
// Grab location pointed by nextNodePtr
IntNode* IntNode::GetNext() {
return this->nextNodePtr;
}
int IntNode::GetDataVal() {
return this->dataVal;
}
int main() {
IntNode* headObj = nullptr; // Create intNode objects
IntNode* currObj = nullptr;
IntNode* lastObj = nullptr;
int i = 0;
int negativeCntr = 0;
headObj = new IntNode(-1); // Front of nodes list
lastObj = headObj;
for (i = 0; i < 10; ++i) { // Append 10 rand nums
currObj = new IntNode((rand() % 21) - 10);
lastObj->InsertAfter(currObj); // Append curr
lastObj = currObj; // Curr is the new last item
}
currObj = headObj; // Print the list
while (currObj != nullptr) {
cout << currObj->GetDataVal() << ", ";
currObj = currObj->GetNext();
}
cout << endl;
currObj = headObj; // Count number of negative numbers
while (currObj != nullptr) {
/* Your solution goes here */
if (currObj > 0)
{
negativeCntr++;
}
currObj = currObj->GetNext();
}
cout << "Number of negatives: " << negativeCntr << endl;
return 0;
}
#包括
#包括
使用名称空间std;
类IntNode{
公众:
IntNode(intdatainit=0,IntNode*nextLoc=nullptr);
void InsertAfter(IntNode*nodePtr);
IntNode*GetNext();
int GetDataVal();
私人:
int-dataVal;
IntNode*nextNodePtr;
};
//建造师
IntNode::IntNode(intdatainit,IntNode*nextLoc){
此->数据值=数据初始化;
此->nextNodePtr=nextLoc;
返回;
}
/*在此节点后插入节点。
*之前:这个——下一个
*之后:this--node--next
*/
void IntNode::InsertAfter(IntNode*nodeLoc){
IntNode*tmpNext=nullptr;
tmpNext=this->nextNodePtr;//还记得下一步吗
this->nextNodePtr=nodeLoc;//this--node--?
nodeLoc->nextNodePtr=tmpNext;//this--node--next
返回;
}
//由nextNodePtr指向的抓取位置
IntNode*IntNode::GetNext(){
返回此->下一个命令;
}
int IntNode::GetDataVal(){
返回此->数据值;
}
int main(){
IntNode*headObj=nullptr;//创建IntNode对象
IntNode*currObj=nullptr;
IntNode*lastObj=nullptr;
int i=0;
int negativeCntr=0;
headObj=newintnode(-1);//节点列表的前面
lastObj=头部OBJ;
对于(i=0;i<10;++i){//附加10个rand nums
currObj=newintnode((rand()%21)-10);
lastObj->InsertAfter(currObj);//追加curr
lastObj=currObj;//Curr是新的最后一项
}
currObj=headObj;//打印列表
while(currObj!=nullptr){
cout GetDataVal()GetNext();
}
cout(0)
{
negativeCntr++;
}
currObj=currObj->GetNext();
}
这似乎是最简单的方法
if(currObj->GetDataVal() < 0) negativeCntr++;
if(currObj->GetDataVal()<0)negativeCntr++;
if(currObj->GetDataVal<0)negativeCntr++;扔掉IntNode
,改用std::list
,然后执行auto const count=std::count\u if(begin(list),end(list),[](auto num){return num<0;})认真的,一个在线的C++课程不应该试图让初学者写链表类。初学者在C++中正确地写一个链表类的成功率几乎是0。YUP,我的前一次尝试很接近,我只需要让CurrBJ使用Access操作符访问数据列表,现在我知道如何去做。