简体   繁体   English

C ++排序结构链接列表

[英]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. 您遇到的问题是,您仅在while循环之外创建一个节点,并且每次迭代都在重用它。 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. 正如Nathan所提到的,您需要为创建的每个数据条目分配额外的内存,并将其添加到链接列表中。 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. 如果需要进一步改进,请执行以下操作:在将指针传递给函数时,实际上不需要附加的&符号,因为它已经是head数据的地址。 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); void insertAccountByAccountNumber(account * H,account * n);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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