• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Rekurencyjne definiowanie danych w C

Object Storage Arubacloud
0 głosów
96 wizyt
pytanie zadane 25 maja 2017 w C i C++ przez Adam Olesiak Gaduła (3,290 p.)

Hej, mam listę, która może przyjmować za argumenty kilka rzeczy(int, struct film, struct person, ...)

Tyle że struct film zawiera w sobie listę intów.

Jak zrobić to tak żeby struct film widział struct list_handler i odwrotnie?

Coś jak z funkcjami i deklarowaniem ich istnienia nad mainem

 

Całość kodu, ale trochę tego jest:

#include <stdio.h>
#include <stdlib.h>

// === DATA SECTION ===

typedef enum list_element_type
{
    integer, catalogue, person, film, fillable
}LIST_ELEMENT_TYPE;

typedef enum fillable_value_type
{
    word, number
}FILLABLE_VALUE_TYPE;

//problem: film needs list_handler to int/string list, list needs film for a possible union option in element data 

typedef struct film
{
    int id;
    LIST_HANDLER *fillable_list_handler;//TODO: make sure its allocated properly in allocate_film()
}FILM;

typedef struct person
{
    int id;
    LIST_HANDLER *fillable_list_handler;//TODO: make sure its allocated properly in allocate_person()
    LIST_HANDLER *borrowed_films_list_handler;//TODO: allocate with matching type
}PERSON;

typedef struct catalogue
{
    int id;
    LIST_HANDLER *fillable_list_handler;//TODO: make sure its allocated properly in allocate_catalogue()
    LIST_HANDLER *films_list_handler;//TODO: allocate with matching type
}CATALOGUE;

typedef union list_element_data
{
    int i;//probably only used for adresses of borrowed films, films in catalogues
    FILM *film;
    PERSON *person;
    CATALOGUE *catalogue;
    FILLABLE *fillable;
}LIST_ELEMENT_DATA;

typedef struct list_element
{
    struct list_element *next;
    struct list_element *previous;
    LIST_ELEMENT_DATA *data;
}LIST_ELEMENT;

typedef struct list_handler
{
    LIST_ELEMENT_TYPE element_type;//elements in the list will be of this type
    int count;//number of elements in the list. Counted after adding/removing from list
    LIST_ELEMENT *beg;
    LIST_ELEMENT *last;
}LIST_HANDLER;



typedef struct fillable
{
    enum fillable_value_type;//word/number
    FILLABLE_VALUE *value;
    int min_value, max_value;//for checking if user input is good. For strings this means the length
    char name[25];//for displaying to the user what he is changing/adding
    //int id; //probably not needed - we can make the user type whole name instead
}FILLABLE;

typedef union fillable_value
{
    char *word;
    int number;
}FILLABLE_VALUE;





// === FUNCTIONS ===

//these contain all the data for whats's in a film/person/catalogue;
//FIXME: for better code expandability better to keep this data in some structs and use one function to allocate a new struct element.
FILM* allocate_new_film();
PERSON* allocate_new_person();
CATALOGUE* allocate_new_catalogue();

//list operations
//TOADD: add those that you need as you go
void push_back(LIST_HANDLER *h, LIST_ELEMENT *e);

//moving data around aka main functions
void add_new(LIST_HANDLER *h);//checks what kind of data is stored in h and adds an element of that data
void modify(LIST_ELEMENT_DATA *data, enum list_element_type);//lets user choose one of the fillables(f.ex. by name) and modifies the given fillable
void remove(LIST_HANDLER *h);//ask user for id of item to remove and remove this item from the list. Also, if item was of film type, remove list elements with its value in lists in people, catalogues

//user communication
//TODO:add an option to give strings in args. Then only print fillables with names==those_strings
void print_list_element(LIST_ELEMENT e);//prints all fillables' names and values
void print_list(LIST_HANDLER h);

//sorting
//TODO: add sorting

//files
//TODO: add files

//additional functions:
//TODO: add as we go

int main()
{
    //initialize the necessary data
    LIST_HANDLER *films_list_handler = (LIST_HANDLER*)malloc(sizeof(LIST_HANDLER));
    LIST_HANDLER *people_list_handler = (LIST_HANDLER*)malloc(sizeof(LIST_HANDLER));
    LIST_HANDLER *catalogues_list_handler = (LIST_HANDLER*)malloc(sizeof(LIST_HANDLER));

    films_list_handler->count=0;
    films_list_handler->beg=NULL;
    films_list_handler->last=NULL;
    films_list_handler->element_type=film;



    return 0;
}

 

1 odpowiedź

0 głosów
odpowiedź 25 maja 2017 przez .kassad Gaduła (3,420 p.)
Zadeklaruj na początku pliku te struktury, zanim napiszesz ich definicje.

Podobne pytania

0 głosów
1 odpowiedź 291 wizyt
pytanie zadane 11 marca 2019 w C i C++ przez Magmardo Początkujący (310 p.)
0 głosów
1 odpowiedź 762 wizyt
pytanie zadane 8 grudnia 2017 w C i C++ przez Jakub 0 Pasjonat (23,120 p.)
+1 głos
1 odpowiedź 337 wizyt
pytanie zadane 4 listopada 2016 w C i C++ przez Beksio Początkujący (260 p.)

92,576 zapytań

141,426 odpowiedzi

319,651 komentarzy

61,961 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...