#include <iostream>
#include <string>
using namespace std;

class Person{
public:
    string name;
    int age, height, weight;

    Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
        this->name = name;
        this->age = age;
        this->height = height;
        this->weight = weight;
    }
    Person operator = (const Person &P) {
        name = P.name;
        age = P.age;
        height = P.height;
        weight = P.weight;

        return *this;
    }

    void setAge(int a){
        age = a;
    }
    int getAge(){
        return age;
    }

    friend ostream& operator<<(ostream& os, const Person& p);
};

ostream& operator<<(ostream& os, Person& p) {
    os << "Name: " << p.name << "    " << "Age: " << p.age << "       " << "Height: " << p.height << "    " << "Weight: " << p.weight << "\n";
    return os;
};

class Node {
public:
    Person* data;
    Node* next;
    Node(Person*A) {
        data = A;
        next = nullptr;
    }
};

class LinkedList {
public:
    Node * head;
    LinkedList() {
        head = nullptr;
    }

    void InsertAtHead(Person*A) {
        Node* node = new Node(A);
        node->next = head;
        head = node;
    }
    void InsertAtEnd(Person*A) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = node;
        }
    }
    void InsertAtPosition(Person*A, int pos) {
        if (head == nullptr) {
            InsertAtHead(A);
        }
        else {
            Node* node = new Node(A);
            Node* temp = head;
            for (int i = 1; i < pos - 1; i++) { temp = temp->next; }
            node->next = temp->next;
            temp->next = node;
        }
    }
    void DeleteByValue(string search_name) {
        Node* temp = head;
        Node* prev = nullptr;
        while (temp != nullptr) {
            if (temp->data->name == search_name) {
                if (prev != nullptr) {
                    prev->next = temp->next;
                }
                else {
                    head = temp->next;
                }
                delete temp;
                temp = nullptr;
            }
            else {
                prev = temp;
                temp = temp->next;
            }
        }
    }
    void DeleteFromHead() {
        if (head != nullptr) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
    void DeleteFromEnd() {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (head->next == nullptr) { DeleteFromHead(); }
        else {
            while (temp->next != nullptr) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = nullptr;
            delete temp;
        }
    }
    void DeleteAtPosition(int pos) {
        Node* prev = nullptr;
        Node* temp = head;
        if (head == nullptr) { cout << "Nothing to delete" << endl; }
        else if (pos == 1) { DeleteFromHead(); }
        else {
            for (int i = 1; i < pos; i++) {
                prev = temp;
                temp = temp->next;
            }
            prev->next = temp->next;
            delete temp;
        }
    }
    void UpdateAtPosition(Person*A, int pos) {
        if (head == nullptr) { cout << "No element in the list"; return; }
        if (pos == 1) { head->data = A; }
        else {
            Node* temp = head;
            for (int i = 1; i < pos; i++) {
                temp = temp->next;
            }
            temp->data = A;
        }
    }
    void UpdateByValue(string name, int newAge) {
        Node* temp = head;
        Person* p = new Person();

        while(temp != nullptr){
            if(temp->data->name == name){
                p->setAge(newAge);
            }else{
                temp = temp->next;
            }
        }
    }

    void Print() {
        Node* temp = head;
        while (temp != nullptr) {
            cout << *(temp->data);
            temp = temp->next;
        }
        cout << endl;
    }
};
int main() {
    LinkedList* list = new LinkedList();
    list->InsertAtHead(new Person("Samantha", 20, 63, 115));                  list->Print();
    list->InsertAtEnd(new Person("Chris", 19, 70, 200));                      list->Print();
    list->DeleteByValue("Chris");                                             list->Print();
    list->UpdateByValue("Samantha", 21);                                      list->Print();

    return 0;
}


我是C ++的新手,所以请原谅任何写得不好的代码,但是我想使用UpdateByValue函数来更新Samantha的年龄。现在可能看起来很不对劲,但是我尝试了20种不同的方法,无法弄清楚我在做什么错。我曾经在社区学院上过学,在那里我学习了Java,因此Im赶上了所有使用C ++的人。很多地方都是相似的,但我却为这样的小事情而苦恼。谁能向我解释如何修复UpdateByValue函数,以使其更改我选择的Person对象的年龄?我希望能够将名称输入为第一个参数,并使用第二个参数更改该人的年龄。如果不清楚并需要更多说明,请告诉我,我只需要帮助。在此先感谢您,并随时提出其他任何建设性的批评。我正在努力使自己变得更好。

最佳答案

让我们来看一下UpdateByValue。我会评论。

void UpdateByValue(string name, int newAge) {
    Node* temp = head;
    Person* p = new Person();

    while(temp != nullptr){ // keep looking until end of list
        if(temp->data->name == name){ // found node with name
            p->setAge(newAge); // update a different node
            // never advance node so we can't exit function
        }else{
            temp = temp->next;
        }
    }
}


试试吧

void UpdateByValue(string name, int newAge) {
    Node* temp = head;
    // Person * p   is not needed

    while(temp != nullptr){ // keep looking until end of list
        if(temp->data->name == name){ // found node with name
            temp->data->setAge(newAge); // update the found node
            return; // done searching. Exit function
        }else{
            temp = temp->next;
        }
    }
}

关于c++ - UpdateByValue函数什么都不做?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49203800/

10-09 08:55