按字母顺序对链表进行排序

Sorting a Linked List into alphabetical order

本文关键字:排序 链表 顺序      更新时间:2023-10-16

我从排序的链表中删除节点时遇到问题。我从.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 listn";
    cout << "2. Display the length of the listn";
    cout << "3. Delete name from the listn";
    cout << "4. display the length of a section of the listn";
    cout << "5. Print out section of listn";
    cin >> input;
    switch (input)
    {
    case 1:
        display();
        break;
    case 2:
        cout << "There are " << count - 1 << " names in the listn";
        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;
}

这是我到目前为止的代码。您会注意到,在我的主函数中,我从一个名为"Data.txt"的文件中读取输入。

joe
bob
harry
mary
brian
tom
jerry
bullwinkle
pam
ellis
dale
bill
barrack
george
gertrude
zack
zeus
apollo
gemini
greg
larry
meriam
webster
thomas
stewart
dianna
theresa
billyjoe
carl
karl
charles
karla
donna
tena
kerry
howard
johnson
ulyssess
paul
peter
issaac
marvin
dudz
chuck
ellie
anny
judy
matt
ross
dan
robert
kim
eric
junkun
ghassan
cris
raymond
avery
roy
halley
mitzee
ziggy
rocky
twirly
max
huey
dewy
hongkongfooey
clarence
lala
sammy
fred
francis

这就是 txt 文档的组成^^。任何建议将不胜感激。谢谢!

您正在访问 next,而不检查它是否为 null,并且您没有循环访问列表。另外,您应该在找到它后中断(除非您要删除所有实例并且应该删除节点,因为您将泄漏内存。此外,您将无法删除第一个元素,因为您永远不会检查它。您可以添加特定的检查,因为您需要处理根节点的更改。

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

当然,如果要删除名称的所有实例,则需要删除返回和中断语句。

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

void display()
{
    node *curr;
    curr = A;
    while(curr){
       cout << curr->name << endl;
       curr = curr->next;
    }
}
while (current && strcmp(current->name , newname) <=0){
    last = current;
    current = current->next;
}

试试这个。

您正在使用链表数据结构。我觉得奇怪的是你使用循环。最后一个节点的下一个元素再次指向开头。

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

void deleteName(string name) {
    node *current = A;
    node *previous;
    while (current) {
        if (current->name == name) {
            previous->next = current->next;
            delete current;
            break;
        } else {
            previous = current;
            current = current->next;
        }
    }
}