Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将链表按字母顺序排序_C++ - Fatal编程技术网

C++ 将链表按字母顺序排序

C++ 将链表按字母顺序排序,c++,C++,从已排序的链接列表中删除节点时遇到问题。我从一个.txt文件中读取了73个不同的名称,这些名称必须按字母顺序排序。我有一个switch语句,它应该能够对一个链表执行5个独立的操作。目前我有1号和2号工作,但不是3号#3希望我能够从链接列表中删除一个名称。键入要删除的名称后,我的代码将不会显示任何内容。因此,我假设deleteAfter函数存在问题。谁能给我一个提示,为什么会这样 #include "stdafx.h" #include <iostream> #include <

从已排序的链接列表中删除节点时遇到问题。我从一个.txt文件中读取了73个不同的名称,这些名称必须按字母顺序排序。我有一个switch语句,它应该能够对一个链表执行5个独立的操作。目前我有1号和2号工作,但不是3号#3希望我能够从链接列表中删除一个名称。键入要删除的名称后,我的代码将不会显示任何内容。因此,我假设deleteAfter函数存在问题。谁能给我一个提示,为什么会这样

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct node{
    string name;
    node *next;
};

node *A = NULL;

void addnode(string newname){
    node *add,
         *last,
         *current;

    add = new node;
    add->name = newname;

    if (A == NULL){
        add->next = A;
        A = add;
    }else{
        current = A;
        last    = A;
        while (current && current->name < newname){
            last = current;
            current = current->next;
        }

        if (current == A){
            /* Insert before 1st node */
            add->next = A;
            A = add;
        }
        else{
            /* Insert between last and current 
               or at the end of the list */
            last->next = add;
            add->next = current;
        }
    }
}
void deleteName(string name)
{
    node *curr;
    node *nextNode;
    curr = A;
    nextNode = curr;
    while(curr){
        if(curr -> next -> name == name){
            nextNode = curr -> next;
            curr -> next = nextNode -> next;
        }

    }


}



void display()
{
    node *curr;
    curr = A;
     while(curr){
        if(A == NULL){break;}
        cout << A->name << endl;
        A = A->next;
    }

}

int main(){


    int input, count;
    count = 0;
    ifstream dataFile;
    dataFile.open("Data.txt");
    string item;
    string name;
    while(dataFile)
    {
        dataFile >> item;
        addnode(item);
        count++;
    }




    cout << "1. Display the linked list\n";
    cout << "2. Display the length of the list\n";
    cout << "3. Delete name from the list\n";
    cout << "4. display the length of a section of the list\n";
    cout << "5. Print out section of list\n";
    cin >> input;

    switch (input)
    {
    case 1:
        display();
        break;
    case 2:
        cout << "There are " << count - 1 << " names in the list\n";
        break;
    case 3:
        cout << "Type in the name that you want to be deleted: ";
        cin >> name;
        deleteName(name);
        display();
        break;
    case 4:
        break;
    case 5:
        break;
    }


    system("PAUSE");
    return 0;

}
这是txt文档的组成部分^^。如有任何建议,将不胜感激。谢谢

while(当前&&strcmp(当前->名称,新名称)下一步;
while (current && strcmp(current->name , newname) <=0){
    last = current;
    current = current->next;
}
}
试试这个。

while(current&&strcmp(current->name,newname)next;
}

尝试此操作。

您正在访问next,但没有检查它是否为空,也没有在列表中进行迭代。此外,您应该在找到它后中断访问(除非您要删除所有实例,并且应该删除节点,否则将导致内存泄漏。此外,您将无法删除第一个元素,因为您从未检查过它。您可以添加一个特定的检查,因为您需要处理根节点的更改

if (A != nullptr && A->name == name)
{
    node *toBeDeleted = A;
    A = A->next;
    delete toBeDeleted;
    return;
}

while(curr && curr->next){
    if(curr->next->name == name){
        nextNode = curr->next;
        curr->next = nextNode->next;
        delete nextNode;
        break;
    }
    curr = curr->next;
}
当然,如果要删除名称的所有实例,则需要删除return和break语句

您的显示功能也将清空列表。您需要设置curr,而不是:

void display()
{
    node *curr;
    curr = A;
    while(curr){
       cout << curr->name << endl;
       curr = curr->next;
    }
}
void display()
{
节点*curr;
curr=A;
while(curr){
下一步不能命名;
}
}

您正在访问next,但没有检查它是否为空,也没有在列表中进行迭代。此外,您应该在找到它后中断访问(除非您要删除所有实例,并且应该删除节点,否则将导致内存泄漏。此外,您将无法删除第一个元素,因为您从未检查过它。您可以添加一个特定的检查,因为您需要处理根节点的更改

if (A != nullptr && A->name == name)
{
    node *toBeDeleted = A;
    A = A->next;
    delete toBeDeleted;
    return;
}

while(curr && curr->next){
    if(curr->next->name == name){
        nextNode = curr->next;
        curr->next = nextNode->next;
        delete nextNode;
        break;
    }
    curr = curr->next;
}
当然,如果要删除名称的所有实例,则需要删除return和break语句

您的显示功能也将清空列表。您需要设置curr,而不是:

void display()
{
    node *curr;
    curr = A;
    while(curr){
       cout << curr->name << endl;
       curr = curr->next;
    }
}
void display()
{
节点*curr;
curr=A;
while(curr){
下一步不能命名;
}
}

您使用的是链表数据结构。我发现奇怪的是您使用的是循环。最后一个节点的下一个元素再次指向开始

以下是我根据您的知识水平和风格(我相信您会看到)提出的
deleteName


您使用的是链表数据结构。我发现奇怪的是,您使用的是循环。最后一个节点的下一个元素再次指向开始

以下是我根据您的知识水平和风格(我相信您会看到)提出的
deleteName


我想知道的是,在哪里每个人都被教导使用
系统(“暂停”)
…恐怕指针的使用没有正确完成。当使用指针时,你应该处理创建和删除。我没有看到代码中的删除。我想知道的是,在哪里每个人都被教导使用
系统(“暂停”)
…恐怕指针的使用没有正确完成。使用指针时,你应该处理创建和删除。我在代码中没有看到删除。无限循环。OP在链表中使用循环。无限循环。OP在链表中使用循环。谢谢你的帮助!你是对的,我没有检查下一个等于空。谢谢你的帮助!你是对的,我没有检查下一个是否等于空。