簡體   English   中英

將鏈接列表按字母順序排序

[英]Sorting a Linked List into alphabetical order

我無法從已排序的鏈表中刪除節點。 我從.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;

}

這是我到目前為止的代碼。 您會注意到,在我的主要功能中,我從名為“ 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文檔的組成部分^^。 任何建議將不勝感激。 謝謝!

while (current && strcmp(current->name , newname) <=0){
    last = current;
    current = current->next;
}

嘗試這個。

您正在訪問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;
}

當然,如果要刪除名稱的所有實例,則需要刪除return和break語句。

您的顯示功能也將清空列表。 您需要設置curr,而不是A:

void display()
{
    node *curr;
    curr = A;
    while(curr){
       cout << curr->name << endl;
       curr = curr->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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM