void print_line(Node* n) { // ***** DEBUT SOLUTION ***** for(Node* p = n; p->r != n; p = p->r) { printf("%s ", p->c->name); } printf("%s\n", n->l->c->name); // ****** FIN SOLUTION ****** } Node* load_file(char* filename) { // Open file FILE* f = fopen(filename, "r"); if (f==NULL) { fprintf(stderr, "Can't open file <%s>\n", filename); exit(-1); } // Reference node Node* h = new_node(); h->name = "<>"; // Buffer to read data char* buffer = (char*)malloc(maxlen*sizeof(char)); // First line (list of objects) fgets(buffer, maxlen, f); char* str_token = strtok(buffer, " \r\n"); while (str_token != NULL) { // Another object! h->nb++; // Create a new header node Node* n = new_node(); n->name = strdup(str_token); // Attach node to existing ones // Theres 4 pointers to set (two from node n, two pointing to node n) // ***** DEBUT SOLUTION ***** n->l = h->l; n->r = h; h->l->r = n; h->l = n; // ****** FIN SOLUTION ****** // Get next name str_token = strtok(NULL, " \r\n"); } // Read each offers (one per line) int line_counter = 0; Node** provides = malloc(h->nb * sizeof(Node*)); while (!feof(f)) { line_counter++; // Read line if (fgets(buffer, maxlen, f)) { // Get number of objects and fill "provides" from buffer int nb_pr = get_offer(buffer, h, provides, line_counter); // Create nodes // Create nb_pr nodes and set/update 7 pointers for each node // (5 from the node (u, d, l, r, c), 2 pointing to the node) // ***** DEBUT SOLUTION ***** Node* first = NULL; for (int i=0; i<nb_pr; ++i) { Node* n = new_node(); n->d = provides[i]; // down is column header (cycling) n->u = provides[i]->u; // up is previous last in column n->c = provides[i]; // direct link to column header provides[i]->u->d = n; // down for last in column is n provides[i]->u = n; // new last in column is n if (!first) { first = n; } else { n->l = first->l; n->r = first; first->l->r = n; first->l = n; } } // ****** FIN SOLUTION ****** } } // Close file fclose(f); return h; }