找到了解决方案 - 张贴在答案中。
当我尝试编译我的 LinkedList 实现时,我收到此错误,但找不到问题所在。它指出了这一点,但我找不到错误。
LinkedList my =new LinkedList();
类和主要函数: -
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
public:
void LinkedList::init_list(struct Node *&head);
bool LinkedList::is_list_empty(struct Node *head);
Node* LinkedList::search(struct Node *head,int value);
int LinkedList::deleteNode(struct Node *head,int value);
int LinkedList::deleteat(struct Node *&head,int i);
bool LinkedList::insert(struct Node *&head,int value);
bool LinkedList::insertat(struct Node *&head,int value, int i);
int LinkedList::list_length(struct Node *head);
void LinkedList:: display(struct Node *head);
};
int main(){
LinkedList my =new LinkedList();
Node *head;
Node *&ahead = *&head;
my.init_list(ahead);
my.display(head);
my.insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my.list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my.insert(ahead,34);
my.insert(ahead,32);
my.insert(ahead,44);
my.display(head);
cout<<endl;
my.insertat(ahead,4444,1);
my.display(head);
system("Pause");
return 0;
}
完整代码:-
#include <iostream>
#include <exception>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
public:
void LinkedList::init_list(struct Node *&head);
bool LinkedList::is_list_empty(struct Node *head);
Node* LinkedList::search(struct Node *head,int value);
int LinkedList::deleteNode(struct Node *head,int value);
int LinkedList::deleteat(struct Node *&head,int i);
bool LinkedList::insert(struct Node *&head,int value);
bool LinkedList::insertat(struct Node *&head,int value, int i);
int LinkedList::list_length(struct Node *head);
void LinkedList:: display(struct Node *head);
};
void LinkedList:: init_list(struct Node*&head){
head=NULL;
}
bool LinkedList:: is_list_empty(struct Node*head){//returns true(1) if the list is empty
if (head==NULL){
return true;
}
return false;
}
Node* LinkedList:: search(struct Node*head, int value){
Node* pointer;
pointer=head;
while (pointer->next != NULL){
if (pointer->data==value){
cout<<"value found" <<endl;
return pointer;
}
pointer=pointer->next;
}
cout<<"value not found!!"<<endl;
return NULL;
}
int LinkedList:: deleteNode(struct Node *head, int value){
Node* pointer;
Node* pointer2;
pointer=head;
while (pointer != NULL){
pointer2 = pointer->next;
if(pointer2 !=NULL && pointer2->data==value){
pointer->next=pointer2->next;
return pointer2->data;
}
else if(pointer2==NULL){
cout<<"value not here - cannot delete";
return NULL;
}
pointer=pointer->next;
}
}
int LinkedList:: list_length(struct Node*head){
int count=0;
Node* p = head;
while (p != NULL)
{
++count;
p = p->next;
}
return count;
}
int LinkedList:: deleteat(struct Node *&head, int loc){
int i=2;
Node*pointer =head;
Node* pointer2;
if(list_length(head)<loc || loc<1){
cout<< "invalid location";
return NULL;
}
if (loc==1){
if(list_length(head)>1){
int val=pointer->data;
int tempval=pointer->next->data;
deleteat(head,2);
pointer->data=tempval;
return val;
}
else{
head=NULL;
return NULL;
}
}
while (pointer->next != NULL){
pointer2 = pointer->next;
if(i==loc){ //112 - 34 - 32 - 44
int val = pointer2->data;
pointer->next=pointer2->next;
return val;
}
pointer=pointer2;
i++;
}
}
bool LinkedList:: insert(struct Node *&head,int value)
{
//try{
Node *q = new Node;
q->data=value;
q->next=NULL;
if(head==NULL){
head=q;
return true;
}
Node *p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=q;
return true;
//}
//catch(NullReferanceException * e ){
// cout<< "insertion was not successful" <<endl;
// return false;
//}
}
bool LinkedList:: insertat(struct Node*&head, int value,int loc){
Node *q = new Node; //112 - 32- 44
q->data=value;
if(loc>list_length(head)){
insert(head,value);
return true;
}
else if(loc<=0){
cout<<"invalid location";
return false;
}
else if(loc==1){
q->next=head;
head=q;
return true;
}
else{
int i=1;
Node*p =head;
while( i<loc-1){
p=p->next;
i++;
}
q->next=p->next;
p->next=q;
return true;
}
}
void LinkedList:: display(struct Node *head){
Node*p = head;
while(p!=NULL){
cout << p->data<< " ";
p=p->next;
}
}
int main(){
LinkedList my =new LinkedList();
Node *head;
Node *&ahead = *&head;
my.init_list(ahead);
my.display(head);
my.insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my.list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my.insert(ahead,34);
my.insert(ahead,32);
my.insert(ahead,44);
my.display(head);
cout<<endl;
my.insertat(ahead,4444,1);
my.display(head);
system("Pause");
return 0;
}
new LinkedList();
给你一个指向LinkedList
的指针(即 LinkedList*
)。不能将LinkedList*
分配给LinkedList
,因为LinkedList*
不是LinkedList
。
您可能应该做的是LinkedList* my = new LinkedList;
.
所以我找到了故障所在,应该是这样的——
int main(){
LinkedList* my = new LinkedList;
Node *head;
my->start = head;
Node *&ahead = *&head;
my->init_list(ahead);
my->display(head);
my->insert(ahead,1111);
//cout<<"1sr time";
cout<< "length- " <<my->list_length(head)<<endl;
//cout<< "deleting--- " <<deleteat(ahead,1)<<endl;
my->insert(ahead,34);
my->insert(ahead,32);
my->insert(ahead,44);
my->display(head);
cout<<endl;
my->insertat(ahead,4444,1);
my->display(head);
system("Pause");
return 0;
}