Home > Software engineering >  Redefine a Node of linked list: std::string
Redefine a Node of linked list: std::string

Time:12-01

I am currently working on a school project, the material is new to me at the moment, basically, we are creating a Robot Guider that tracks their movement, distance, speed, etc... one of the functions that we are required to make is renaming a robot, however, they are stored in Node.

I have spent some time looking around for a quick solution and I am a little confused by the examples online. If someone could please help but also explain their logic that would be greatly appreciated.

we are using two different classes to track all of the information

-----CLASS #1:

#ifndef RobotList_hpp
#define RobotList_hpp

#include "Robot.hpp"
#include <stdio.h>
#include <iostream>

class RobotList{

private:
    class Node{
    public:
        Robot* val;
        Node* next = nullptr;
        Node(std::string aName) {
            val = new Robot;
            val->setName(aName);
        }
    };
    Node* head = nullptr;
    Node* tail = nullptr;
public:
    RobotList() = default;
        ~RobotList();
    void display() const;
    bool isEmpty();
    Robot* find_nth();
    void updateList();
    void addNode(std::string name);
    void deleteNode(std::string name);
    void rename();
    void robotDist() const;
};

#endif /* RobotList_hpp */

---CLASS #2:

#ifndef Robot_hpp
#define Robot_hpp
#include <stdio.h>
#include <iostream>
#include <algorithm>

class Robot{
private:

    int x, y, curSpeed, totDist;
    std::string name; char lastCommand;
    bool stop_; int off_or_on;
public:
    std::string getName() { return name; }
    void setName(std::string a) {
        this->name = a;
    }
    int getTotDist() { return totDist; }
    void moveRobot();
    int findRobot();
};
#endif /* Robot_hpp */

void RobotList::rename(){
    std::string  new_name; 
    std::cout << "Which robot do you want to rename?"<< std::endl;
    std::cin >> new_name;
    
    Node* temp = head;
    while(!head){
        if(temp->val->getName() == new_name){
            // update list with user input new_name
            // reassign a node that holds a string value
        }
            }
    temp = temp->next; // rest of list til nullptr
}

This is what I tried to do but it was not operating properly. 
I wrote out two comments on what I am trying to do. Thanks.

CodePudding user response:

The problem is the while loop.

Head is a pointer to the first element so !head is true only when the list is empty, which is not what you want. Head should not be modified because we will lose the start of the list, that's why we have the temp.

The loop should stop at the end of the list, we know we reached the end when temp is nullptr. This is convenient because it makes sure we never dereference a null pointer. temp = temp->next; should be placed inside the loop so that it doesn't get stuck at the first element.

std::string  old_name, new_name; 
std::cout << "Which robot do you want to rename?"<< std::endl;
std::cin >> old_name; // name to search for
std::cout << "Enter new name:"<< std::endl;
std::cin >> new_name; // new name for the robot with old_name

Node* temp = head;   // temp = first element(node) of the list
while(temp){         // while we haven't reached the end of the list
    if(temp->val->getName() == old_name){
        temp->val->setName(new_name);
        break; // break if you only want to modify the first occurrence 
    }
    temp = temp->next; // move to the next node
}

Also try to use const references for passing objects whenever possible, otherwise you create a lot of unwanted copies.

  • Related