Reset of assigned element at the end of list - C+++



  • We need to develop a function of reversing the assigned element at the end of the list, how do we do it? The third day is nothing.

    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);



  • It's 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 haven't made other mistakes, will you? So I don't have any error checks, like you do in your code! And you've got the list to the end of the start, which is counted from the row file. \n, something else. I hope you can handle these heats and finish checks on your own?

    And there's nothing in this code from C+++, except there's no need to write the word everywhere. struct : Is that right? Maybe it's better to tag. C You want me to fix it?


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2