从文件中读取数据的双链接列表。

问题描述 投票:0回答:1

我创建了一个简单的双链接列表来存储有关植物的数据。我需要让数据从c++文件中写和读。 写入文件没有问题。 但我不知道如何正确地从文件中读取数据(.也许有一些功能。

早些时候,当我从文件中写入数据到一个数组时,我使用循环,按索引对元素进行排序,但现在当我需要将元素写入一个双链接列表时--我真的不明白该怎么做。

这是我的代码。

#include <iostream>
#include <cstring>
#include <list>
#include <fstream>
#include <locale>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <list>

using namespace std;
typedef struct  
{
    char plant[100];
    char family[100];
    char species[100];  

}BOOK;

typedef struct tag_obj
{
    BOOK b;
    struct tag_obj* prev, * next;
}OBJ;

OBJ* head = NULL, * tail = NULL;

void add_obj(OBJ* obj, BOOK book, char sim[50]) {
    OBJ* c = head;
    while (c != NULL) {
    if (strcmp(c->b.plant, sim) == 0){
         cout<< " element already existst(("<< endl;
        return ;
     }
     c = c->next;
    }
    OBJ* ptr = new OBJ;

    ptr->b = book;
    ptr->prev = obj;
    ptr->next = (obj == NULL) ? NULL : obj->next;

    if (obj != NULL) {
        obj->next = ptr;
        if (obj->next != NULL) obj->next->prev = ptr;
    }
    if (ptr->prev == NULL) head = ptr;
    if (ptr->next == NULL) tail = ptr;
}

void del_obj(OBJ* obj) {
    char x[50];
    cout << "enter the name of the plant whose data you want to delete: ";
    cin >> x;
    OBJ* tmp;
    OBJ* h;

    if (head == NULL) 
    {
        cout << "List empty,nothing to delete" << endl;
        return;
    }
    if (strcmp(head->b.plant, x) == 0)     
    {
        tmp = head;
        head = head->next;  
        head->prev = NULL;   
        cout << "Element Deleted" << endl;
        free(tmp);
        return;
    }
    h = head;
    while (h->next->next != NULL)
    {

        if (strcmp(h->next->b.plant, x) == 0)    
        {
            tmp = h->next;
            h->next = tmp->next;
            tmp->next->prev = h;
            cout << "Element Deleted" << endl;
            free(tmp);
            return;
        }
        h = h->next;
    }
    if (strcmp(h->next->b.plant, x) == 0) 
    {
        tmp = h->next;
        free(tmp);
        h->next = NULL;      
        cout << "Element Deleted" << endl;
        return;
    }
    cout << "Element " << x << " not found" << endl;



}


void show() {
    OBJ* c = head;
    int d = 1;
    while (c != NULL) {
        cout << d << ")" << "  " << c->b.plant << "  " << c->b.family << "  " << c->b.species << endl;
        c = c->next;
        d++;
    }
}


int main() {
    //setlocale(LC_ALL, "ukr");
    ofstream fout;
    fout.open("plant account.txt", ios::app);
    ifstream fin;
    fin.open("plant account.txt");
    if (!fout.is_open())
    {
        cout << "Помилка вiдкриття файлу";
        return 1;
    }
    char sim[50];
    BOOK book = { " PLANT  ","  FAMILY  " };
    //add_obj(tail, book);

    int choice;
    cout << "**plant account**" << endl;
    cout << "1(add an element)" << endl;
    cout << "2(delete)" << endl;
    cout << "3(show)" << endl;
    cout << "4(exit)" << endl << endl;

start:
        cout << " Choose action : ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            fout << endl;
            cout << "Plant name: ";
            cin >> book.plant;
            strcpy_s(sim, book.plant);
            fout << book.plant << "  ";
            cout << "Plant family: ";
            cin >> book.family;
            fout << book.family << "  ";
            cout << "Plant species: ";
            cin >> book.species;
            fout << book.species << "  ";
            add_obj(tail, book,sim);
            cout << endl;

            goto start;
        case 2:
            del_obj(head);
            cout << endl;
            goto start;
        case 3:
            show();
            cout << endl;
            goto start;
        case 4:
            cout << " " << endl;
            exit(222);
        default:
            break;

        }

    fout.close();
    fin.close();

    return 0;
}
c++ file linked-list doubly-linked-list
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.