Linked list[solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Linked list[solved]

Push element at head I wrap my head around this problem since quite a long time but I can't seem to find how to do it: I want to make a VOID function that adds a element at the front of the list. I got a code here but it doesn't seem to work... Could anybody help me please ? https://code.sololearn.com/cpyoytLae1BU/?ref=app

19th Mar 2020, 1:08 PM
Uni
Uni - avatar
15 Answers
+ 3
the while loop inside "add_front" isn't quite right, just make it: while((*c)->next){ // iterate through list until we reach tail not NULL c = &(*c)->next; } and as @andriy kan did mentioned you need to swap the function names.
19th Mar 2020, 2:06 PM
MO ELomari
+ 4
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ May be you are compiling ๐Ÿฆ„Uni๐Ÿฆ„'s code on C++ compiler? Errors you are writing about are given by C++ compiler, but not C compiler. The code is valid C code, but not valid C++ code.
19th Mar 2020, 1:43 PM
andriy kan
andriy kan - avatar
+ 3
Mohamed ELomari thank you it worked ! But I don't understand the thing with the functions name...
19th Mar 2020, 2:57 PM
Uni
Uni - avatar
+ 3
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ oh right I see thank you
19th Mar 2020, 3:43 PM
Uni
Uni - avatar
+ 2
In your code function names contradict what they do: in add_back you are trying to insert an item in front of a list and in add_front you are trying to append an item to the end of a list. I changed the function names as they should be. If you need your function naming just swap the function names. I fixed only add_front and add_back. The rest of the code should work. void add_front(cell *c, int n){ cell p=malloc(sizeof(cell)); p->val=n; p->next=*c; *c = p; } void add_back(cell *c, int n){ cell p=malloc(sizeof(cell)); p->val=n; p->next=NULL; if(!*c){ *c = p; }else{ cell ptr = *c; while(ptr->next) ptr = ptr->next; ptr->next = p; } }
19th Mar 2020, 1:34 PM
andriy kan
andriy kan - avatar
+ 2
๐Ÿฆ„Uni๐Ÿฆ„ , You are using opposite naming of what we do by convention. Suppose this is linked list: list = a -> b -> c when we use push_back() it means we are adding new element to 'rear' of list. list.add_back(d); //a->b->c->d but in your code add_back() adds an element before the passed node(cell) add_back(&c,k); // a->b->k->c Same goes for add_front() by convention add_front will mean adding element at 'front' position, i.e as first element. but in your code add_front() adds element after passed node (cell). You can use whatever name you want ๐Ÿ‘€ but most of the people like to see it how it is by convention.
19th Mar 2020, 3:42 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 2
๐Ÿฆ„Uni๐Ÿฆ„ Correction that wrote Mohamed ELomari : while((*c)->next){ // iterate through list until we reach tail not NULL c = &(*c)->next; } is not enough to fix your code! If you look at my corrections you could see that I also appended an additional check on empty list. Without that check your code get access violation again if you try to add_back an item to an empty_list: for example, try this: int main() { cell c=NULL; add_back(&c, 1); // function add_back raise access violation //.... also, code cell ptr = *c; while(ptr->next) ptr = ptr->next; is more effective than while((*c)->next){ c = &(*c)->next; } because the second one do the additional dereference operation in the loop
19th Mar 2020, 7:59 PM
andriy kan
andriy kan - avatar
+ 2
Blake and Nada Nedal please do not post irrelevant answers on this thread.
19th Mar 2020, 9:40 PM
Uni
Uni - avatar
+ 2
Mohamed ELomari Your new code fix access violation issue. My point was that ๐Ÿฆ„Uni๐Ÿฆ„ marked code with while((*c)->next){ as correct answer but it is not correct as the code doesn't check *c on NULL To use extra dereference or do not is a matter of personal preference for the programmer. Using of dereference operation at each iteration of a loop has some overhead. For x86 platform it is not a big deal. Moreover, extra dereference may be optimized out with -O3 flag (max optimization for performance). But for small devices where performance is a matter and its compiler is not such a good optimizer I prefer iterate over a list without additional dereference operation (as in my code). But it is only my preference.
20th Mar 2020, 7:07 AM
andriy kan
andriy kan - avatar
+ 2
Erisa Murati please do not spam this thread with irrelevant answers.
21st Mar 2020, 11:29 AM
Uni
Uni - avatar
+ 1
Segmentation faults ๐Ÿ˜. There are many things going wrong. first thing is typedef . clang 5.0.2 gives error for redefinition : 'struct cell *' vs 'cell' next thing to consider is that malloc returns a void pointer so you need to typecast it before assignment. like this : type * p = (type *) malloc(sizeof(type)); There are many other things that I failed to fix ๐Ÿ˜. Just listed a few so that you can work on fixing them. Hope it helps.
19th Mar 2020, 1:35 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 1
andriy kan , Oh yes. Thanks for correction. I compiled code without saving it. By default it's compiled as C++. In C it's valid ๐Ÿ‘ Thanks again.
19th Mar 2020, 1:47 PM
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰
๐Ÿ‡ฎ๐Ÿ‡ณOmkar๐Ÿ•‰ - avatar
+ 1
@andriy kan this can fix the access violation issue and it does not have the if else statement: void add_back(cell *c, int n){ cell p=malloc(sizeof(cell)); p->val=n; while(*c) { c=&(*c)->next; } p->next = *c; *c = p; }
19th Mar 2020, 9:05 PM
MO ELomari
0
Java
21st Mar 2020, 11:25 AM
Erisa Murati
Erisa Murati - avatar
0
#include# Int main()
21st Mar 2020, 11:29 AM
Erisa Murati
Erisa Murati - avatar