Found the solution - posted in answers.
找到了解决方案——张贴在答案中。
I get this error when I try to compile my LinkedList implementation and I cannot find where the problem lies. It points to this but I cannot find the fault.
当我试图编译我的LinkedList实现时,我得到这个错误,我找不到问题所在。它指向这个,但我找不到错误。
LinkedList my =new LinkedList();
The class and the main function: -
本课程及主要功能:-。
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;
}
The complete code:-
完整的代码:-
#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;
}
2 个解决方案
#1
0
new LinkedList();
gives you a pointer to a LinkedList
(i.e. LinkedList*
). You cannot assign a LinkedList*
to a LinkedList
since a LinkedList*
is not a LinkedList
.
新LinkedList();给你一个指向LinkedList的指针(即LinkedList*)。由于LinkedList*不是LinkedList,所以不能将LinkedList*分配给LinkedList。
What you should probably do is LinkedList* my = new LinkedList;
.
你应该做的是LinkedList* my = new LinkedList;
#2
0
So I found where the fault lied, and this is how it should have been -
所以我找到了错误所在,这就是它应该是怎样的。
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;
}
#1
0
new LinkedList();
gives you a pointer to a LinkedList
(i.e. LinkedList*
). You cannot assign a LinkedList*
to a LinkedList
since a LinkedList*
is not a LinkedList
.
新LinkedList();给你一个指向LinkedList的指针(即LinkedList*)。由于LinkedList*不是LinkedList,所以不能将LinkedList*分配给LinkedList。
What you should probably do is LinkedList* my = new LinkedList;
.
你应该做的是LinkedList* my = new LinkedList;
#2
0
So I found where the fault lied, and this is how it should have been -
所以我找到了错误所在,这就是它应该是怎样的。
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;
}