简体   繁体   中英

C++ Sorted Structure Linked List

I am working on a project where I import the data from a text file into a linked list in order, then output the linked list. However, whenever I output the linked list, I always get the last entry in the text file repeating over and over.

Here is the structure:

struct account
{
    int accountNumber;
    double balance;
    string firstName;
    string lastName;

    account * next;
};

This is my function to add the node in the list:

void insertAccountByAccountNumber(account * & H, account * n)
{
    if (H == NULL)
    {
        H = n;
        return;
    }
    if (H->accountNumber >= n->accountNumber)
    {
        n->next = H;
        H = n;
        return;
    }
    account * t1, *t2;
    t1 = H;
    t2 = H->next;
    while (t2 != NULL)
    {
        if (t2->accountNumber < n->accountNumber)
        {
            t1 = t2;
            t2 = t2->next;

        }
        else
        {
            n->next = t2;
            t1->next = n;
            return;
        }
        t1->next = n;
    }
}

And here is my code to create the node from the text file:

account * head = NULL;

account * currentAccount = new account;

ifstream fin;
fin.open("record.txt");
while (fin >> accountNumberCheck)
{
    fin >> firstNameCheck;
    fin >> lastNameCheck;
    fin >> balanceCheck;
    currentAccount->accountNumber = accountNumberCheck;
    currentAccount->firstName = firstNameCheck;
    currentAccount->lastName = lastNameCheck;
    currentAccount->balance = balanceCheck;
    currentAccount->next = NULL;
    insertAccountByAccountNumber(* & head, currentAccount);
}

showAccounts(head);


fin.close();

The problem you are having is you are only making one node outside of your while loop and you are reusing it every iteration. With linked list you need to make a new element every time you insert into the list.

Here is a linked list implementation that is on code review that you can look at to get some pointers on how linked list work.

As Nathan mentioned you need to allocate additional memory for each data entry you create and add to your linked list. If you need further improvement here is one: You don`t actually need an additional '&' sign when passing a pointer to a function since it is already an address for the head data. Try:

account * head = NULL;

ifstream fin;
fin.open("record.txt");
while (fin >> accountNumberCheck)
{
    fin >> firstNameCheck;
    fin >> lastNameCheck;
    fin >> balanceCheck;

    account * currentAccount = new account; // Allocate a new memory location for each data entry in the heap
    currentAccount->accountNumber = accountNumberCheck;
    currentAccount->firstName = firstNameCheck;
    currentAccount->lastName = lastNameCheck;
    currentAccount->balance = balanceCheck;
    currentAccount->next = NULL;
    insertAccountByAccountNumber(head, currentAccount);
}

showAccounts(head);


fin.close();

and of course the function header will be:

void insertAccountByAccountNumber(account* H, account* n);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM