help, please, with sorting of the simply linked list. There is a student structure, the format is surname_name_of_the_property. need to do a sort by last name. below I will attach my unsuccessful attempt, the structure itself and a small piece of the program.

struct student { string name; string surname; string middlename; float progress; int age; struct student* next; }; struct list { student st; list *next; }; void Print(list *b) { list *print = b; while (print != NULL) { std::cout << print->st.surname << " " << print->st.name << " " << print->st.middlename << " " << print->st.age << " " << print->st.progress << endl; print = print->next; } std::cout << "NULL\n"; } void sort(list **begin) { list *t = new list; list *t2 = *begin; t = *begin; while (t != 0) { list *t1 = t->next; if (t1->st.surname < t2->st.surname) { list *tmp = t2; t2 = t1; t = t2; } t = t->next; } } int main() { setlocale(LC_ALL, "Russian"); SetConsoleCP(1251); SetConsoleOutputCP(1251); forward_list <student> st; list* begin = NULL; list* head = nullptr; list* item = nullptr; for (;;) { int x = menu(); switch (x) { case 1: { Print(begin); break; } case 8: { sort(&begin); break; } 
  • First of all, if you have not initialized the structure fields, you will not be able to compare. What to compare with, how to sort and what to sort? .. Your code leaves only one questions. - AR Hovsepyan

1 answer 1

So, you have: a structure with the name of the student. What you named list is not a list. In general, you strangely did: create this list , pass it to the sorting function, but at the same time create the list from student structures. In short, we assume that the list is:

 forward_list <student> st; 

What do you need to sort it? First you need to fill it, but this is purely your responsibility.

1) The operation is more or less for structures of type student . There are no problems with this at all, since it depends only on the name, and the name is defined as a string for which these operations already exist

 bool operator>(student &a, student &b) { return a.name > b.name; } 

2) Need a sorting function. To do this, in the simplest form, simply go through the list from the beginning to the end and compare two adjacent elements, if the first is larger than the second (for this, the operation > needed), then swap them. And so several times until the entire list is sorted.

  • can you explain why the list structure is wrong? - Timur Mahmudov
  • I just understood the topic of the video, something like this was explained there. A list of article, in theory, is not needed at all. - Timur Mahmudov
  • Well, look: you have your list structure - how to iterate on it? These elements, of course, can be connected, but here only, since they indicate only the following structure, how to proceed to the previous element? This is not a list, but a branch. The list must contain the root. Besides, as I understood from your code, you use lists from the standard library. Although I have never used them, but since you make them, then why is this list structure at all? - Andrej Levkovitch
  • So the list is simply connected, should there be a link to the previous one? - Timur Mahmudov
  • You did not read it: I wrote that the list should contain a root. A simply linked list is simply connected, that in it the elements indicate only the next element. But! How about this list, how are you, were you going to work? How, for example, to add an item there? Or withdraw? How to find the right one? This is not a list, but a node for the list. The list itself will be something like: list {node *root; node *last; unsigned size;}; list {node *root; node *last; unsigned size;}; . Thus, we will have direct access to the first element, for iterations; to last, for quick addition; to the size of the list, in order to know its size. - Andrej Levkovitch