0

问题:我在删除节点时遇到问题,让我告诉你我是如何插入它的,我有一个学生链接列表,每个节点都有一个 ID(Int)和学生姓名(字符串),以及插入节点的代码清单是这样的:

void FileHandling::readLine(char filePath[], StudentList* sl1){

ifstream read_file;
string line;
read_file.open(filePath, std::ifstream::in);

if(read_file.is_open()){
        while(read_file.good()){
            getline(read_file,  line);
            if(line.substr(0,1)!= "#" && !line.empty() ){
                //cout<<line<<endl;

                std::stringstream ss(line);
                int id;
                std::string first_name, surname;
                ss >> id >> first_name >> surname;
                std::string name = first_name + " " + surname;
                //cout<< id <<", "<<name<<endl;
                sl1->insertSort(id,name); // insert node in an alphabetic order
            }
        }
}

}

这就是我需要通过从用户获取名称来删除的方式:请注意,当我得到像“abc xyz”这样的名称时,我的string name;变量只包含 xyz 并跳过初始名称。

void StudentList::deleteWN(StudentList* sl1){
//Deleting from list by getting name from user
string name;
while(true){
    cout << "Deleting: Student Name 0r press 'q' to quit"<<endl;
    cin >> name;
    cout<<name<<endl;
    if(name=="q")
        break;
    sl1->deleteWithNmae(name);
    sl1->printList();
}

}

void StudentList::deleteWithNmae(const string& name1){

// CASE 1: Deleting from an empty list
if(head == 0){
    cout<< "Node can not be deleted from an Empty List"<<endl;
}else{
// Traversing the list to find Exact Node
    Student* curr = head;
    Student* prev = 0;
    while(curr != 0){
        if(curr->name == name1){
            // node found, break the while loop
            // Never compare the name of curr node with the user input
            // i also tried curr->name.length() == name1.length() 
            //and comparing the strings but i m sure i m doing something
            // wrong here and cant find my node in any case.

            break;
        }else{
            prev = curr;
            curr = curr->next;
        }
    }
    // CASE 2: Node not found
    if(curr == 0){
        // always execute this code, i never able to find my node
        cout<< "Student "<<name1<<" not Found .."<<endl;
    }else{
            //CASE 3: Deleting node from the head
        if(curr == head){
            head = head->next;
        }else{
            //CASE 4: Deleteing node beyond the head
            prev->next = curr->next;
        }
        delete curr;
        size--;
    }

}

}

帮我解决这个问题提前谢谢。

4

2 回答 2

1

您已经在插入代码中做了正确的事情。这里唯一的区别是你必须在读完名字后检查“q”。如果不是“q”,那么您可以读取姓氏。像这样

void StudentList::deleteWN(StudentList* sl1){
//Deleting from list by getting name from user
string firstName, lastName;
while(true){
    cout << "Deleting: Student Name 0r press 'q' to quit"<<endl;
    cin >> firstName;
    if(firstName=="q")
        break;
    cin >> lastName;
    std::string name = first_name + " " + surname;
    sl1->deleteWithNmae(name);
    sl1->printList();
}
于 2013-09-17T09:51:30.073 回答
0

您需要使用std::getline(std::cin, name)来阅读整行。cin >> name将在空格之前读取文本,而不是整行。

这里的例子:

#include <iostream>
#include <string>

int main ()
{
  std::string name;

  std::cout << "Please, enter your full name: ";
  std::getline (std::cin,name);
  std::cout << "Hello, " << name << "!\n";

  return 0;
}
于 2013-09-17T09:49:15.647 回答