【问题标题】:Inserting structs in sorted linked list and writing it to binary file在排序链表中插入结构并将其写入二进制文件
【发布时间】:2017-08-21 18:09:08
【问题描述】:

我有一个严重的问题。我需要编写一个程序,在其中将人的详细信息(例如名字、姓氏和年龄)输入到一个结构中,该结构还包含指向下一个人的指针,然后我需要将其写入二进制文件。它必须按姓氏而不是名字升序排序,如链表,使用指针,但只有一个指针指向下一个结构。输入所有数据后,它需要遍历文件并找到具有最低值的结构,然后使用指针按升序打印所有数据。 这是我写的代码,但它不起作用。我需要帮助。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

struct person{
    char firstName[20], lastName[20];
    int age;
    person *next;
};



 void insert(char filename[], int n){
    person *head = NULL;

    ofstream people(filename, ios::binary | ios::trunc);
    for(int i=0; i<n; i++){
        system("cls");
        person *per = new person;
        cout << "firstName: ";
        cin >> (*per).firstName;
        cout << "lastName: ";
        cin >> (*per).lastName;
        cout << "age: ";
        cin >> (*per).age;


        if(head == NULL) {
            (*per).next = head;
            head = per;
            }
        else{
            person *curr = head;
            person *prev = NULL;
            bool end = false ;
            while(!end){
                if(strcmp((*curr).lastName, (*per).lastName) ||
                   (!strcmp((*curr).lastName, (*per).lastName) && 
                   strcmp((*curr).firstName, (*per).firstName))){
                    end = true;
                    }
                else {
                    prev = curr;
                    curr = (*curr).next;
                }
            }

            if(curr==head){
                (*per).next= head;
                head = per;
             }
             else{
                (*per).next = curr;
                (*prev).next = per;
             }
        }

        people.write((char *)&(*per), sizeof(*per));
    }

    people.close();
}

    void print(char filename[])  {
     person *head = NULL;
     person *el;
     person c,min;

     ifstream people(filename, ios::binary);

     people.read((char *)&c, sizeof(c));
     min = c; 
     while(!people.eof()){
        people.read((char *)&c, sizeof(c));
        if(strcmp(c.lastName,min.lastName)== -1 || 
        (!strcmp(c.lastName,min.lastName) && 
        strcmp(c.firstName,min.firstName)==-1)){
            min = c; 
         }   
        }
        head = &min;
     while (head != NULL) {
      cout << (*head).firstName << " " <<(*head).lastName <<" "<< 
      (*head).age << endl;
      el = head;
      head = (*head).next;
      delete el;
    }people.close();
 }


 int main() {

    int n, a;
    char filename[40];

    cout << "filename: " << endl;
    cin.getline(filename, 40);
    do{
        do{ system("cls");
            cout << "Choose one of the options: "<<endl;
            cout << "1. insert "<<endl;
            cout << "2. print "<<endl;
            cout << "0. exit "<<endl;
            cin >> a;
        } while(a<1||a>2);
            switch(a){
                case 1: cout << "How many people would you like to enter? ";
                        cin >> n;
                        insert(filename, n); 
                        break;
                case 2:print(filename);
            }


    } while(a!=0);

return 0;
}

【问题讨论】:

  • “它不工作”是什么意思?您观察到哪些不良行为? “为什么我的代码不起作用?”没有必要的支持信息,对于 Stack Overflow 来说不是一个合适的问题。
  • 所以,如果我有两个以上的人,当我输入第二个人的数据时它就会崩溃。

标签: c++ pointers struct linked-list binaryfiles


【解决方案1】:

看起来您正在尝试创建排序列表,但您插入新节点的位置条件错误

strcmp((*curr).lastName, (*per).lastName) ||
               (!strcmp((*curr).lastName, (*per).lastName) && 
               strcmp((*curr).firstName, (*per).firstName)

它本质上告诉 - 只要它与 curr 不同,就在 curr 之前插入新人。 您必须将其更改为 - 只要大于(小于)curr 就插入新人 - 取决于排序顺序。

所以不要检查 strcmp != 0 而是检查 strcmp >0 或

当然,用 std::string 替换 c-strings 会更容易和更安全

【讨论】:

  • 它没有解决问题,但感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
相关资源
最近更新 更多