C++ C++;:按编号顺序对链表排序错误

C++ C++;:按编号顺序对链表排序错误,c++,pointers,linked-list,nodes,C++,Pointers,Linked List,Nodes,我正在读取的文件如下所示: t 44 c 13 a 47 十九, g 41 n 51 。四, 以此类推,一共有53条线路。我需要读入这些行,并使用addInOrder函数将它们按顺序排列。我用cout's做了一个测试,我看到了 字母=t,数字=44,头部=44 字母=c,数字=13,标题=13 字母=a,数字=47,标题=13 字母:[空格],num=19,head=13 字母:g,num=41,head=13 字母:n,num=51,head=13 信:,num=4,head=13 头部直到稍

我正在读取的文件如下所示:

t 44

c 13

a 47

十九,

g 41

n 51

。四,

以此类推,一共有53条线路。我需要读入这些行,并使用addInOrder函数将它们按顺序排列。我用cout's做了一个测试,我看到了

字母=t,数字=44,头部=44

字母=c,数字=13,标题=13

字母=a,数字=47,标题=13

字母:[空格],num=19,head=13

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

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

信:,num=4,head=13

头部直到稍后num=12时才改变,然后在num=1时再次改变。然后head等于1,直到完成相加

当我最后打印时,链表按第一个数字排序,而不是按整数排序,顺序如下:

  • 一,
  • 十,
  • 十一,
  • 十九,
  • 二,
  • 二十
  • 二十一,
  • 二十九
  • 三,
  • 三十
  • 三十一
等等。。。我需要它是:

  • 一,
  • 二,
  • 三,
  • 九,
  • 十,
  • 十一,
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);
}

您正在比较的是
字符串
,而不是数字。列表按字母顺序正确排序


如果要按数字顺序排序,则必须将字符串转换为整数(或任何其他数值)。

related/dupe:我真不敢相信我竟然错过了!成功了!谢谢。如果您想保持字符串排序行为,可以强制每个条目附加0<代码>001、002、010、011。我不建议你这样做,但要让你知道。
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;
    }
}