-1

The file that I am reading in lookes like this:

t 44

c 13

a 47

19

g 41

n 51

. 4

and so on, there are a total of 53 lines. I need to read these lines in and put them in order using a addInOrder function. I did a test with cout's and I saw that

letter= t, num= 44, head= 44

letter= c, num= 13 , head= 13

letter= a , num= 47 , head= 13

letter: [space] , num= 19 , head= 13

letter: g , num= 41 , head= 13

letter: n , num= 51 , head= 13

letter: . , num= 4 , head= 13

The head doesn't change until until later on when num = 12, and then again when num= 1. head is then equal to 1 until it's done adding.

When I do print in the end, the Linked List is sorted by the first number rather than the whole number, it follows the order:

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

and so on... I need it to be:

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

Is there an error in my logic for the addInOrder function?

This is my main:

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

Here is my function that I add to the Linked List in order of the number:

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

Printing the list to make sure of the indexes:

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

Deleting the list:

void deleteList(ListNode *&h)
{
    ListNode *ptr;
    while (h != nullptr)
    {
        ptr = h;
        h = h->next;
        delete ptr;
    }
}
Community
  • 1
  • 1
sam
  • 75
  • 6

1 Answers1

2

You are comparing string, not numbers. The list is sorted correctly in alphabetical order.

If you want it sorted in numeric order, you must convert the strings to integers (or any other numeric value).

MPops
  • 355
  • 3
  • 8
  • 1
    I can't believe I missed that! It worked! Thank you. – sam May 22 '18 at 20:56
  • If you want to keep string sorting behavior, you could force each entry to have 0s appended to them. `001, 002, 010, 011`. I don't recommend doing this, but just letting you know. – MPops May 22 '18 at 21:01