需要左键作为赋值的左操作数?? 在链表中添加Add_End、删除和Delete_Front?

lvalue required as left operand of assignment?? add Add_End, Delete, and Delete_Front in a linked list?

本文关键字:添加 End Add 删除 Front Delete 赋值 操作数 链表      更新时间:2023-10-16

我看过一堆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 值(const 临时对象(,不能在赋值中用作 l 值。

这就是为什么它将错误抛出为lvalue required as left operand of assignment

要实现此目的,您可以使用Node类的SetNext()方法,如下所示:

prev->SetNext(temp->Next());

我希望这会消除您的问题。

谢谢