繁体   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