1

How should I modify this function to be able to append from an existing list to a NULL/empty list?

void Appending(Diary *List, Diary *Element) {
    Diary *moving = List;
    if (List == NULL) {
        List = kovetkezo;
    } else { 
        while (moving->NextDiary != NULL)
            moving = moving->NextDiary;
        moving->Nextdiary = Element;
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    If the list you're trying to append onto is `NULL`, then the result should just be the other list. – Chris Nov 24 '21 at 16:20
  • 2
    `List = kovetkezo;` looks fishy, what is `kovetkezo`? If it is some global variable, then your design is very poor. – Jabberwocky Nov 24 '21 at 16:24
  • This [article](https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) might be interesting for you to read. – Jabberwocky Nov 24 '21 at 16:28
  • It could be a possible solution, yet when i'd like to sort out(depends on a defined condition, of course) some elements into this new linkedlist, my programme just sort out the first case. What could i do with it? – Kövesdi László Nov 24 '21 at 20:53
  • How should i call period in the main? – Kövesdi László Nov 24 '21 at 20:55

2 Answers2

1

To allow appending to an empty list, the list pointer in the caller must be updated.

You must either take a pointer to the List pointer: Appending(&head, element);

// Taking a double pointer to the initial element:
void Appending(Diary **List, Diary *Element) {
    Diary *moving = *List;
    if (moving == NULL) {
        *List = Element;
    } else { 
        while (moving->NextDiary != NULL)
            moving = moving->NextDiary;
        moving->Nextdiary = Element;
    }
}

or you can return the updated List pointer and update the list pointer in the caller by storing the return value into it: head = Appending(head, element);

// Returning an updated pointer to the initial element:
Diary *Appending(Diary *List, Diary *Element) {
    Diary *moving = List;
    if (moving == NULL) {
        List = Element;
    } else { 
        while (moving->NextDiary != NULL)
            moving = moving->NextDiary;
        moving->Nextdiary = Element;
    }
    return List;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • It could be a possible solution, yet when i'd like to sort out(depends on a defined condition, of course) some elements into this new linkedlist, my programme just sort out the first case. What could i do with it? – Kövesdi László Nov 24 '21 at 20:52
0

Declare the function to return the updated list.

Diary* Appending(Diary* List, Diary* Element)
{
    Diary* moving = List;
    if (List == NULL)
    {
        List = kovetkezo;
    }
    else
    { 
        while (moving->NextDiary != NULL)
            moving = moving -> NextDiary;
        moving->Nextdiary = Element;
    }
    return List;
}

The caller then uses:

list = Appending(list, newElement);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • One might also just position: `if (list == NULL) return kovetkezo;` at the beginning of the function and then carry on from there without the extra level of indentation with `Diary *moving; for (moving = List; moving->NextDiary != NULL; moving = moving->NextDiary);` to advance to the last node. – Chris Nov 24 '21 at 16:28