在有序链表中插入结构并将其写入二进制文件

时间:2017-08-21 18:09:08

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

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

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

1 个答案:

答案 0 :(得分:2)

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

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

它基本上告诉 - 一旦与curr不同,在curr之前插入新人。 您必须将其更改为 - 在大于(小于)curr时插入新人 - 具体取决于排序顺序。

所以不要检查strcmp!= 0检查strcmp&gt; 0或&lt; 0

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