我创建了一个链接列表:结构节点列表类,我和我的主要方法使用它以外,从列表*无效转换成int
#include "Lists.cpp"
#include <cstdlib>
using namespace std;
int main(){
Lists l = new Lists(1);
l.add(2);
l.add(3);
l.add(4);
l.add(5);
system("pause");
return 0;
}
,但它产生的,上面写着“从列表中无效,转换时发生错误*到int“。我使用的是外部课堂吗?我有点困惑,我将如何解决这个问题。
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
Node(int i){
data = i;
next = NULL;
}
};
class List{
Node *head;
public:
List(int i){
head = new Node(i);
}
void addToHead(int i){
Node *temp = new Node(i);
temp->next = head;
head = temp;
}
void add(int i){
Node *currNode = head;
while(currNode!= NULL){
if(currNode->next == NULL){
currNode->next = new Node(i);
break;
}
else{
currNode = currNode-> next;
}
}
}
void deleteNode(int i){
Node *currNode = head;
Node *prevNode = NULL;
while(currNode!= NULL){
if(currNode->data == i) {
if(prevNode== NULL){
head = head->next;
}
else{
prevNode->next = currNode->next;
}
}
prevNode = currNode;
currNode = currNode -> next;
}
}
void insert(int position, Node *n){
Node *currNode= head;
Node *prevNode = NULL;
for(int counter = 0; counter>= position && currNode!= NULL; counter++){
if(counter==position){
Node *temp = currNode;
n->next = currNode;
prevNode->next= n;
}
prevNode = currNode;
currNode = currNode-> next;
}
}
void traverse(Node *node){
if(node!=NULL){
cout<< node-> data <<endl;
traverse(node->next);
}
}
};
通常包含* .cpp文件是一个坏主意。 –
或换句话说,如果你打算包含一个文件,通常把它称为'.cpp'是一个坏主意。 –
然后生病只是包括它包括“列表”? – jko