c++ - UpdateByValue 函数什么也没做?

标签 c++ function

#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,所以我 catch 了 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/

相关文章:

c++ - 如何在 C++11 中处理弱枚举的 `using`?

带有可选参数的 C 函数 - 最好的方法?

javascript - 注入(inject)的 JavaScript 函数未执行

c++ - 为什么 Utf-8 在 Qt 5 中无法工作?

c++ - 为什么 const 正确性特定于 C++?

c++ - 使用标准 :sort to sort locally

c++ - 解释以下C++代码部分

javascript - 访问对象 key :Value Pair in Function

PHP - 作为静态数组元素的匿名函数

c - 在 C 中使用两个名称调用同一个函数的首选方法