通过比较字符串值从c ++中的链接列表中删除节点

时间:2013-09-17 09:47:56

标签: c++ c data-structures

问题:我在删除节点时遇到问题,让我先告诉你我是如何插入的,我有一个学生LinkList,每个节点都有一个ID(Int)和学生名(String),以及代码在列表中插入节点是:

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--;
    }

}

}

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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

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

来自here的示例:

#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;
}