I received a task for coursework - to implement a dynamic structure in C ++, which contains information: faculty, group, number of students, number of scholarships, grade point average, headman's name through doubly linked lists.

Now I want to choose how to present the list node - as a structure containing all these fields, i.e. representing a separate group (let's call it simple) or as a structure that contains the field of the name of the faculty and a nested structure with groups, which in turn already contains the fields related to the group (let's call it complex).

Since I am not familiar with the intricacies of implementing doubly linked lists (without using classes and STL, since the subject is not OOP, that is, doing everything manually), I would like to understand how difficult it will be to implement a task with a node that is a complex structure. I like the difficult option more, but due to lack of experience, I am afraid that there will not be enough knowledge to implement it and I will simply lose time. Tell me how to do better, and if you can, drop materials on how to implement a doubly linked list (not in the general case, where the data are ints, but in a similar one), and how to do it with a "complex" structure.

  • In addition, the use of constructors, destructors and everything associated with classes is not welcome. - Kirill Terentyev

1 answer 1

Is this exactly C ++? If you cannot use constructors, destructors, operators, then it is easier to write in C probably. There just will be only the structure.

About biconnected lists - instead of int use pointers to your structures, you need to figure out how to allocate memory for structures, how to free this memory, and that's it ( new/delete plus or malloc/free ).

Then just spawn the structure under your entities, and write the functions that create them, fill, delete, etc.

Judging by the assignment, you are required to make a list of groups, that is, one structure describes a group ( простая your words), another (s) structures are required to create a doubly linked list of these structures.

Example:

 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct{ int a; char* text; }data; data* create_data(int a, const char* text){ data* ret = malloc(sizeof(data)); ret->a = a; size_t text_len = strlen(text); ret->text = malloc(sizeof(char) * (text_len + 1)); ret->text = strcpy(ret->text, text); return ret; } void free_data(data* d){ if (d->text != NULL){ free(d->text); d->text = NULL; } } typedef struct node{ data* d; struct node* next; struct node* prev; }node; node* create_node(data* d){ node* ret = malloc(sizeof(node)); ret->next = NULL; ret->prev = NULL; ret->d = d; return ret; } void free_node(node* n){ if (n->d != NULL){ free_data(n->d); free(n->d); n->d = NULL; } } typedef struct{ node* head; node* tail; }linked_list; linked_list* create_list(){ linked_list* ret = malloc(sizeof(linked_list)); ret->head = NULL; ret->tail = NULL; return ret; } void free_list(linked_list* l){ node* cur = l->head; while (cur != NULL){ free_node(cur); cur = cur->next; if (cur != NULL){ if (cur->prev != NULL) free(cur->prev); } } } linked_list* add_to_tail(linked_list* l, data* d){ node* new_node = create_node(d); new_node->next = NULL; new_node->prev = l->tail; if (l->tail == NULL){ l->tail = new_node; l->head = new_node; }else{ l->tail->next = new_node; l->tail = new_node; } return l; } void print_list(linked_list* l){ node* cur = l->head; while (cur != NULL){ printf("node a:%d text:%s\n", cur->d->a, cur->d->text); cur = cur->next; } return; } int main() { linked_list* list = create_list(); for (int i = 0; i < 10; i++){ list = add_to_tail(list, create_data(i, "hello")); } print_list(list); free_list(list); free(list); return 0; } 

Conclusion:

 node a:0 text:hello node a:1 text:hello node a:2 text:hello node a:3 text:hello node a:4 text:hello node a:5 text:hello node a:6 text:hello node a:7 text:hello node a:8 text:hello node a:9 text:hello