#help with linked lists

30 messages · Page 1 of 1 (latest)

fading sundial
#

list->tail = list->tail->next; 🤔

celest tiger
#

i dont have a previous struct variable

fading sundial
#

is this a doubly linked list or singly?

celest tiger
#

do i just point straight to head?

#

singly

fading sundial
#

no, in that case, if you want to hold a pointer to the tail (why?), you need to run through the list

celest tiger
#

ok so its fine being tail-> next?

fading sundial
#

no. what is tail->next?

#

hint, it's NULL

celest tiger
#

yeah

#

uhh is that illegal

fading sundial
#

are you concerned with iterator invalidation? if not, there's a simpler way of dealing with the general case

celest tiger
#

i have to pass this test

#

would u like to see my entire file? i managed to complete all the other functions

fading sundial
#

what the hell is the point of tail in that case?

celest tiger
#

i think there are hidden tests aswell

#

but i think the else part looks a bit wrong to me but im not sure how to fix it

fading sundial
#

well, i'll avoid saying anything about optimizing the general case to not be O(n). i dont think it's necessarily relevant

#

while (current->next != element is wrong

#

or, well, you should realize you are stopping one node early

#

it seems you want to do so

#

so LinkedListElement_t* remove = current; is wrong

celest tiger
#

for (LinkedListElement_t* current = list->head; current->next != NULL)

#

would this work?

#
for (LinkedListElement_t* current = list->head; current->next != NULL; current->next) {
            if (current->next == element) {
                LinkedListElement_t* remove = current->next;
                current->next = current->next->next;
                free(remove);
            }
            

        }


#

i've replaced it with this

fading sundial
#

no

celest tiger
#

i dont get it