It is necessary to develop a function for swapping a given element to the end of the list, how to do this? The third day nothing happens

List.cpp

List* LoadList(char* FN, char* LN) { FILE* f = fopen(FN, "rt"); if (!f) { cout << " "; return NULL; } char st[Max_ch]; fgets(st, Max_ch, f); List* lst = CreateList(LN, st); while (fgets(st, Max_ch, f)) { AddHead(lst, st); } return lst; } // ?????Перестановка????? void Per(char* Inf) { } /*-Создать элемент списка-*/ Itm* CrItm(int Id, char* Inf) { Itm* t = new Itm; t->next = t->prev = NULL; t->id = Id; strcpy(t->Info, Inf); return t; } /*-Создать список(Заголовок, информация)-*/ List* CreateList(char* Name, char* Inf) { List* lst = new List; strcpy(lst->Name, Name); lst->getId = 1; lst->begin = CrItm(lst->getId, Inf); lst->end = lst->begin; return lst; } /*-Добавить строку-*/ Itm* AddHead(List* lst, char* Inf) { lst->cur = CrItm(++lst->getId, Inf); lst->begin->prev = lst->cur; lst->cur->next = lst->begin; lst->begin = lst->cur; return lst->begin; } /*-Вывести список-*/ void ViewLst(List* lst) { printf(" %s \n", lst->Name); lst->cur = lst->begin; while (lst->cur) { printf("%i: %s \n", lst->cur->id, lst->cur->Info); lst->cur = lst->cur->next; } } /*---------------------------------------------------------------------*/ void DelHead(List* lst) { if (lst->begin == lst->end) { DelLst(lst); } else { lst->cur = lst->begin; lst->begin->prev = NULL; delete lst->cur; } } void DelLst(List* lst) { while (lst->begin != lst->end) { DelHead(lst); } delete lst->begin; delete lst; } void ViewLstBk(List* lst) { printf(" %s \n", lst->Name); lst->cur = lst->end; while (lst->cur) { printf("%i: %s \n", lst->cur->id, lst->cur->Info); lst->cur = lst->cur->prev; } } 

List.h

  struct Itm { int id; char Info[Max_ch]; Itm *next, *prev; //Начало и конец списка }; struct List { char Name[20]; int getId; Itm * begin, *cur,*end; }; List* CreateList(char* Name, char* Inf); Itm* AddHead(List* lst, char* Inf); Itm * CrItm(int id); void ViewLst(List* lst); void DelHead(List* lst); void DelLst(List* lst); void ViewLstBk(List* lst); void Per(List* lst); List* LoadList(char* FN, char* LN); 
  • The prototype and implementation of the Per function is different. As far as I understand, there must be something like void Per (List * lst, char * Inf); Implement the functions: search by key, cut, add to tail (can be useful in other tasks). The function Per will consist of them. - paulgri
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Yes, everything is simple.

 void Per(List* lst, char* inf) { for(Itm *t = lst->begin; t != NULL; t = t->next) { if ((strcmp(t->Info,inf) == 0) && (t != lst->end)) { t->prev->next = t->next; t->next->prev = t->prev; t->next = NULL; t->prev = lst->end; t->prev->next = t; lst->end = t; } } } 

Only I did not fix other errors, please note. So I, like um in all your code, do not have error checking! And your list is displayed from the end to the beginning, the lines read from the file are completed with \n , something else in detail. I hope you cope with these flaws and finish checking for errors yourself?

And yet - in this code there is nothing from C ++, except for the absence of the need to write the word struct everywhere :) Is this exactly what you need? Maybe a better C tag to fix?