我看过很多YouTube教程,但是他们都说相同的话。还有其他方法可以不同地链接节点,而不是“ prev-> Next()= temp-> Next();” ?每次我使用它时,它都会显示相同的错误,即需要左值作为赋值的左操作数?请帮忙!这是代码。
#include <iostream>
using namespace std;
class Node {
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
private:
int data;
Node* next;
};
class List {
public:
List() {
head = NULL;
}
void Add_End(int data);
void Delete(int data);
void Delete_Front();
void Add_Front(int data);
void Delete_End();
Node* Find(int data);
void Print();
private:
Node *head;
};
void List::Add_End(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
if(head == NULL) {
head = newNode;
return;
}
Node *temp = head;
while(temp->Next()!= NULL)
{
temp=temp->Next();
}
temp->SetNext(newNode);
return;
}
void List::Delete(int data) {
Node *prev = head;
int i;
if(data == 1){
head = prev->Next();
delete prev;
return;
}
for(i = 0; i <(data-2);i=i+1){
prev = prev->Next();
}
Node *temp = prev->Next();
prev->Next()=temp -> Next();
delete temp;
return;
}
void List::Delete_Front() {
if(head == NULL) {
cout<<"List has no member so cannot delete front"<<endl;
return;
}
delete head;
head = head-> Next();
return;
}
void List::Add_Front(int data) {
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(head);
head = newNode;
return;
}
void List::Delete_End() {
if(head == NULL) {
cout<<"List has no member so cannot delete end"<<endl;
return;
}
if(head->Next() == NULL) {
delete head;
head = NULL;
return;
}
Node *current;
Node *prev;
prev = head;
for(current = head->Next(); current->Next() != NULL; current = current- >Next()) {
prev = current;
}
prev->SetNext(NULL);
delete current;
return;
}
Node* List::Find(int data) {
Node *current;
for(current = head; current!= NULL && current->Data() != data; current = current->Next())
{}
if(current == NULL) {
cout<<"Did not find "<<data<<"."<<endl;
return NULL;
}
else {
cout<<"Found "<<data<<"."<<endl;
return current;
}
}
void List::Print() {
Node *current;
cout<<"Linked List Nodes: "<<endl;
if(head == NULL){
cout<<"List is empty." <<endl;
}
for(current = head; current != NULL; current = current->Next()){
cout<<current->Data();
cout<<endl;
}
return;
}
int main(){
List list;
Node *answer;
list.Add_End(111);
list.Print();
list.Add_End(222);
list.Print();
list.Add_End(333);
list.Print();
list.Add_End(444);
list.Print();
list.Add_End(555);
list.Print();
list.Delete(444);
list.Print();
list.Delete(333);
list.Print();
list.Delete(222);
list.Print();
list.Delete(555);
list.Print();
list.Delete(111);
list.Print();
list.Add_End(23);
list.Print();
list.Add_End(45);
list.Print();
list.Add_End(26);
list.Print();
list.Delete_Front();
list.Print();
cout<<"Testing Add_Front: and others"<<endl;
list.Add_Front(888);
list.Print();
list.Add_Front(999);
list.Print();
list.Add_Front(49);
list.Print();
cout<<"Checking find function"<<endl;
answer = list.Find(888);
cout<<"Value for node returned by find function call with 888 is "
<<answer->Data()<<"."<<endl;
cout<<"Checking find function"<<endl;
answer = list.Find(999);
cout<<"Value for node returned by find function call with 999 is "
cout<<"Checking find function"<<endl;
answer = list.Find(49);
cout<<"Value for node returned by find function call with 49 is "<<answer-
>Data()<<"."<<endl;
cout<<"Call find function with value not in list."<<endl;
answer = list.Find(7);
if(answer == NULL) {
cout<<"returned null pointer since 7 not found"<<endl;
}
else{
cout<< "in else of answer == NULL where Value for node returned by
find function call with 7 is "<<answer->Data()<<"."<<endl;
}
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
cout<<"testing delete_end: "<<endl;
list.Delete_End();
list.Print();
return 0;
}
这里的问题是Node的Next()
方法返回next
成员变量的副本,因此它成为一个r值(常量临时对象),不能在分配中用作l值。
这就是为什么它将错误抛出为lvalue required as left operand of assignment
。
为了达到这个目的,您可以如下使用SetNext()
类的Node
方法:
prev->SetNext(temp->Next());
我希望这可以解决您的问题。
谢谢