簡體   English   中英

C ++:按數字錯誤順序排序鏈表

[英]C++: Sorting linked list in order by number error

我正在閱讀的文件如下所示:

t 44

c 13

47歲

19

克41

n 51

4

依此類推,共有53行。 我需要閱讀這些行,並使用addInOrder函數將它們排序。 我對cout進行了測試,結果發現

字母= t,數量= 44,頭= 44

字母= c,num = 13,頭= 13

字母= a,num = 47,頭= 13

字母:[space],num = 19,head = 13

字母:g,num = 41,head = 13

字母:n,num = 51,head = 13

信: 。 ,num = 4,head = 13

直到后來num = 12時,head才改變,然后在num = 1時再次改變。head等於1直到完成加法。

當我最后打印時,鏈接列表按第一個數字而不是整個數字排序,它遵循以下順序:

  • 1個
  • 10
  • 11
  • ...
  • 19
  • 2
  • 20
  • 21
  • ...
  • 29
  • 3
  • 30
  • 31

等等...我需要它是:

  • 1個
  • 2
  • 3
  • ...
  • 9
  • 10
  • 11

addInOrder函數的邏輯中是否有錯誤?

這是我的主要:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <ctype.h>

using namespace std;

struct ListNode
{
    string letter;
    string num;
    ListNode *next;
};

void addInOrder(ListNode *&h, string l, string n);
void printList(ListNode *h, int &lengthOfFile);
void deleteList(ListNode *&h);

int main()
{
    string letter;
    string num;
    string lines;
    int lengthOfFile = 0;
    const string FILENAME = "link to file";
    ifstream inFile(FILENAME);

    ListNode *head = nullptr;

    if (inFile)
    {
        string line;
        for (int lineNum = 1; getline(inFile, line); lineNum++)
        {
            stringstream ss(line);
            string word;

            for (int wordNum = 1; ss >> word; wordNum++)
            {
                if (wordNum == 1)
                {
                    char c = word[0];

                    if (isalpha(c))
                    {
                        letter = c;
                    }
                    else if (word == "!" or word == ".")
                    {
                        letter = word;
                    }
                    else if (word != "!" or word != ".")
                    {
                        letter = "  ";
                        num = word;
                        lengthOfFile++;
                        if (wordNum == 1)
                        {
                            addInOrder(head, letter, num);
                        }
                    }
                }
                if (wordNum == 2)
                {
                    num = word;
                    lengthOfFile++;
                }
                if (wordNum == 2)
                {
                    addInOrder(head, letter, num);
                }
            }
        }
    inFile.close();
    }

    printList(head, lengthOfFile);

    deleteList(head);
}

這是我按數字順序添加到鏈接列表中的函數:

void addInOrder(ListNode *&h, string l, string n)
{
    ListNode *newNode;
    newNode = new ListNode;

    newNode->letter = l;

    newNode->num = n;

    if (h == nullptr)
    {
        h = newNode;
        newNode->next = nullptr;
    }
    else
    {
        ListNode *prev = nullptr;
        ListNode *curr = h;

        while ((curr != nullptr) && (curr->num < n))
        {
            prev = curr;
            curr = curr->next;
        }
        if (prev == nullptr)
        {
            h = newNode;
            newNode->next = curr;
        }
        else
        {
            prev->next = newNode;
            newNode->next = curr;
        }

    }
}

打印列表以確保索引:

void printList(ListNode *h, int &lengthOfFile)
{
    ListNode *ptr = h;  

    for(int i = 0; i < lengthOfFile; i++)
    {
        cout << ptr->letter << " ";
        cout << ptr->num << " ";
        cout << endl;
        ptr = ptr->next;
    }
    cout << endl;
}

刪除列表:

void deleteList(ListNode *&h)
{
    ListNode *ptr;
    while (h != nullptr)
    {
        ptr = h;
        h = h->next;
        delete ptr;
    }
}

您正在比較string ,而不是數字。 該列表已按字母順序正確排序。

如果要按數字順序對其進行排序,則必須將字符串轉換為整數(或任何其他數字值)。

暫無
暫無

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

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