c++ - 排序链表在每次执行时插入重复项

标签 c++ linked-list

我有一个函数可以从文件中读取数据,然后根据每行数据中的特定字段按升序插入。

如果我编译并运行这部分代码,它最初会排序,但一旦我重复该操作,我就会得到一个排序的拷贝,如果我再运行一次,每个项目出现 3 次。

如何让此代码停止插入重复项?

void sortData(Node *& head, Node * data)
{
    ifstream fin("data.txt");
    int acc; string fname, lname; double bal;
    while (fin >> acc >> fname >> lname >> bal)
    {
        data = new Node;
        data->account = acc;
        data->fname = fname;
        data->lname = lname;
        data->balance = bal;
        data->next = NULL;

        if (head == NULL)
        {
            head = data;
        }

        else if (data->balance <= head->balance)
        {
            data->next = head;
            head = data;
        }

        else
        {
            Node * temp1 = head;
            Node * temp2 = temp1->next;

            while (temp2 != NULL && data->balance > temp2 ->balance)
            {
                temp1 = temp2 ;
                temp2 = temp2 ->next;
            }

            data->next = t2;
            temp1 ->next = data;
        }

    }

    Node * temp = head;
    while (temp != NULL)
    {
        cout << temp->account << " " << temp->fname << " " << temp->lname << " " << temp->balance << "\n";
        temp = temp->next;
    }

    fin.close();

}

清晰度。

程序运行时有一个简单的菜单,所以当我第一次构建和运行时,我可以做几件事,插入一条新记录,删除,显示所有记录(问题所在),然后退出。我可以尽可能多地重复每个 Action ,期望退出(很明显)。

初始运行,然后根据账号显示所有记录:

1111 FName1 LName1 400.56

2222 FName2 LName2 23.45

3333 FName3 LName3 4599.91

4444 FName4 LName4 1000.5

效果很好。

但是

当我想再次显示所有记录时,我看到了这个

1111 FName1 LName1 400.56

1111 FName1 LName1 400.56

2222 FName2 LName2 23.45

2222 FName2 LName2 23.45

3333 FName3 LName3 4599.91

3333 FName3 LName3 4599.91

4444 FName4 LName4 1000.5

4444 FName4 LName4 1000.5

我没有重新插入文件,只是按递增顺序打印出数据。

最佳答案

您在一个函数中做了太多事情。将它们分成更小的、可重用的函数。

// Function to insert the data sorted in an order.
void insertSortedData(Node*& head,
                      int acc,
                      std::string const& fname,
                      std::string const& lname,
                      double bal)
{
   // If the list is empty, create a node and return.
   Node* newNode = new Node(acc, fname, lname, bal)
   if ( head == NULL )
   {
      head = newNode;
      return;
   }

   // If the value of the new node is less or equat to the value
   // of the head node, make it the head node and return.
   if (newNode->balance <= head->balance)
   {
      newNode->next = head;
      head = newNode;
      return;
   }

   // Insert new node at the right place.
   Node * temp1 = head;
   Node * temp2 = temp1->next;

   while (temp2 != NULL && newNode->balance > temp2->balance)
   {
      temp1 = temp2 ;
      temp2 = temp2->next;
   }

   newNode->next = temp2;
   temp1->next = newNode;
}

// Function to read data from a file and create
// a sorted list.
void readDataFromFile(std::string const& file,
                      Node*& head)
{
   std::ifstream fin(file);
   int acc;
   std::string fname;
   std::string lname;
   double bal;
   while (fin >> acc >> fname >> lname >> bal)
   {
      insertSortedData(head, acc, fname, lname, bal);
   }
}

// Functoin to print the contents of a list.
void printList(Node* head)
{
   Node* temp = head;
   while (temp != NULL)
   {
      cout << temp->account << " " << temp->fname << " " << temp->lname << " " << temp->balance << "\n";
      temp = temp->next;
   }
}

然后,您可以分别调用每一个:

int main()
{
   Node* head = NULL;

   // Read data from a file.
   readDataFromFile("data-1.txt", head);

   // Print the data
   printList(head);


   // Read data from a different file.
   readDataFromFile("data-2.txt", head);

   // Print again.
   printList(head);
}

关于c++ - 排序链表在每次执行时插入重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29284254/

相关文章:

c++ - 单链表插入和删除的时间复杂度

c - 打印包含 void* 的链表元素

c - 为什么 `pLQ->tail`是空指针?

C++如何在不丢失内存的情况下删除节点?

c++ - 错误 : 'memcpy_s' was not declared in this scope in gSOAP

c++ - 指向函数的指针(有时/总是?)是函数声明符吗?

c++ - 使用 <tr1/regex> 中的 regex_replace 函数时出错

C++ 使用纯虚函数接收错误, "variable or field ' x' 声明为 void"

c++ - 当我使用 "./waf"时ndnSIM2.0出现错误

java - Java 中的链表;使列表中的最后一个节点指向第一个节点而不是包含 null