+ 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
15 Respuestas
+ 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.
+ 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.
+ 3
Mohamed ELomari thank you it worked !
But I don't understand the thing with the functions name...
+ 3
🇮🇳Omkar🕉 oh right I see thank you
+ 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;
}
}
+ 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.
+ 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
+ 2
Blake and Nada Nedal please do not post irrelevant answers on this thread.
+ 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.
+ 2
Erisa Murati please do not spam this thread with irrelevant answers.
+ 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.
+ 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.
+ 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;
}
0
Java
0
#include#
Int main()