Node in linked list not being deleted properly?












0















I have posted MCVE code which can be copied and run in any IDE to check the results.



The input in the linked list is as follows:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar Polycarbonate 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am looking for is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am getting is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Some random symbols 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


In this problem I am trying to delete the intermediate node, therefore I am not considering the first and last node. Please can someone let me know why is this happening?



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lamina
{
char lamina_fiber[30];
char lamina_matrix[30];
float E1,E2,p,v12,v21,G12;
struct lamina *nextnode;
};

int main()
{
struct lamina *head,*data;
head=(struct lamina *)calloc(1,sizeof(struct lamina));
data=head;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Epoxy");
data->E1= 43.75;
data->E2= 18.90;
data->p= 1.48;
data->v12= 0.34;
data->v21= 0.148;
data->G12= 3.93;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");
data->E1= 43.40;
data->E2= 14.00;
data->p= 1.39;
data->v12= 0.34;
data->v21= 0.110;
data->G12= 2.75;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"ABS");
data->E1= 43.42;
data->E2= 13.94;
data->p= 1.35;
data->v12= 0.35;
data->v21= 0.112;
data->G12= 2.74;

data=head;

struct lamina *temp,*anothertemp;
temp=(struct lamina *)calloc(1,sizeof(struct lamina));
anothertemp=(struct lamina *)calloc(1,sizeof(struct lamina));

temp=data;
size_t i=0;

while(i<1)
{
temp=temp->nextnode;
i++;
}

if(i==1)
{
anothertemp=temp->nextnode;
anothertemp=temp;
free(temp);
}

for(int i=0;i<3;i++)
{
printf("%s %s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2fn",
data->lamina_fiber,data->lamina_matrix,data->E1,data->E2,data->p,
data->v12,data->v21,data->G12);
data=data->nextnode;
}

}









share|improve this question

























  • OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

    – user3629249
    Jan 3 at 1:23
















0















I have posted MCVE code which can be copied and run in any IDE to check the results.



The input in the linked list is as follows:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar Polycarbonate 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am looking for is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am getting is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Some random symbols 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


In this problem I am trying to delete the intermediate node, therefore I am not considering the first and last node. Please can someone let me know why is this happening?



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lamina
{
char lamina_fiber[30];
char lamina_matrix[30];
float E1,E2,p,v12,v21,G12;
struct lamina *nextnode;
};

int main()
{
struct lamina *head,*data;
head=(struct lamina *)calloc(1,sizeof(struct lamina));
data=head;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Epoxy");
data->E1= 43.75;
data->E2= 18.90;
data->p= 1.48;
data->v12= 0.34;
data->v21= 0.148;
data->G12= 3.93;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");
data->E1= 43.40;
data->E2= 14.00;
data->p= 1.39;
data->v12= 0.34;
data->v21= 0.110;
data->G12= 2.75;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"ABS");
data->E1= 43.42;
data->E2= 13.94;
data->p= 1.35;
data->v12= 0.35;
data->v21= 0.112;
data->G12= 2.74;

data=head;

struct lamina *temp,*anothertemp;
temp=(struct lamina *)calloc(1,sizeof(struct lamina));
anothertemp=(struct lamina *)calloc(1,sizeof(struct lamina));

temp=data;
size_t i=0;

while(i<1)
{
temp=temp->nextnode;
i++;
}

if(i==1)
{
anothertemp=temp->nextnode;
anothertemp=temp;
free(temp);
}

for(int i=0;i<3;i++)
{
printf("%s %s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2fn",
data->lamina_fiber,data->lamina_matrix,data->E1,data->E2,data->p,
data->v12,data->v21,data->G12);
data=data->nextnode;
}

}









share|improve this question

























  • OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

    – user3629249
    Jan 3 at 1:23














0












0








0








I have posted MCVE code which can be copied and run in any IDE to check the results.



The input in the linked list is as follows:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar Polycarbonate 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am looking for is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am getting is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Some random symbols 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


In this problem I am trying to delete the intermediate node, therefore I am not considering the first and last node. Please can someone let me know why is this happening?



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lamina
{
char lamina_fiber[30];
char lamina_matrix[30];
float E1,E2,p,v12,v21,G12;
struct lamina *nextnode;
};

int main()
{
struct lamina *head,*data;
head=(struct lamina *)calloc(1,sizeof(struct lamina));
data=head;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Epoxy");
data->E1= 43.75;
data->E2= 18.90;
data->p= 1.48;
data->v12= 0.34;
data->v21= 0.148;
data->G12= 3.93;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");
data->E1= 43.40;
data->E2= 14.00;
data->p= 1.39;
data->v12= 0.34;
data->v21= 0.110;
data->G12= 2.75;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"ABS");
data->E1= 43.42;
data->E2= 13.94;
data->p= 1.35;
data->v12= 0.35;
data->v21= 0.112;
data->G12= 2.74;

data=head;

struct lamina *temp,*anothertemp;
temp=(struct lamina *)calloc(1,sizeof(struct lamina));
anothertemp=(struct lamina *)calloc(1,sizeof(struct lamina));

temp=data;
size_t i=0;

while(i<1)
{
temp=temp->nextnode;
i++;
}

if(i==1)
{
anothertemp=temp->nextnode;
anothertemp=temp;
free(temp);
}

for(int i=0;i<3;i++)
{
printf("%s %s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2fn",
data->lamina_fiber,data->lamina_matrix,data->E1,data->E2,data->p,
data->v12,data->v21,data->G12);
data=data->nextnode;
}

}









share|improve this question
















I have posted MCVE code which can be copied and run in any IDE to check the results.



The input in the linked list is as follows:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar Polycarbonate 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am looking for is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


The result I am getting is:



Kevlar Epoxy 43.75 18.90 1.48 0.34 0.148 3.93
Some random symbols 43.40 14.00 1.39 0.34 0.110 2.75
Kevlar ABS 43.42 13.94 1.35 0.35 0.112 2.74


In this problem I am trying to delete the intermediate node, therefore I am not considering the first and last node. Please can someone let me know why is this happening?



#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct lamina
{
char lamina_fiber[30];
char lamina_matrix[30];
float E1,E2,p,v12,v21,G12;
struct lamina *nextnode;
};

int main()
{
struct lamina *head,*data;
head=(struct lamina *)calloc(1,sizeof(struct lamina));
data=head;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Epoxy");
data->E1= 43.75;
data->E2= 18.90;
data->p= 1.48;
data->v12= 0.34;
data->v21= 0.148;
data->G12= 3.93;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");
data->E1= 43.40;
data->E2= 14.00;
data->p= 1.39;
data->v12= 0.34;
data->v21= 0.110;
data->G12= 2.75;

data->nextnode=(struct lamina *)calloc(1,sizeof(struct lamina));
data=data->nextnode;

strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"ABS");
data->E1= 43.42;
data->E2= 13.94;
data->p= 1.35;
data->v12= 0.35;
data->v21= 0.112;
data->G12= 2.74;

data=head;

struct lamina *temp,*anothertemp;
temp=(struct lamina *)calloc(1,sizeof(struct lamina));
anothertemp=(struct lamina *)calloc(1,sizeof(struct lamina));

temp=data;
size_t i=0;

while(i<1)
{
temp=temp->nextnode;
i++;
}

if(i==1)
{
anothertemp=temp->nextnode;
anothertemp=temp;
free(temp);
}

for(int i=0;i<3;i++)
{
printf("%s %s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2fn",
data->lamina_fiber,data->lamina_matrix,data->E1,data->E2,data->p,
data->v12,data->v21,data->G12);
data=data->nextnode;
}

}






c linked-list singly-linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 0:25







AMD

















asked Jan 3 at 0:20









AMDAMD

20229




20229













  • OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

    – user3629249
    Jan 3 at 1:23



















  • OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

    – user3629249
    Jan 3 at 1:23

















OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

– user3629249
Jan 3 at 1:23





OT: regarding statements like: data->E1= 43.75; the literal: 43.75 is a double but the destination field is a float To correct this problem, append a f after all those literals

– user3629249
Jan 3 at 1:23












1 Answer
1






active

oldest

votes


















2














strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_fiber,"Polycarbonate");


should be



strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");


Otherwise you are printing uninitialized memory (which is the reason for your "Some Random Symbols")



Furthermore, if you are trying to delete the second node, you have to modify the first node (head):



head->next = head->next->next;


To free the deleted node, first save head->next to a temporary variable and free it at last.






share|improve this answer





















  • 1





    @AMD No, an assignment to anothertemp does not modify the head of your list at all.

    – Ctx
    Jan 3 at 0:32






  • 1





    For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

    – Ctx
    Jan 3 at 0:36






  • 2





    @AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

    – Ctx
    Jan 3 at 0:42






  • 2





    @AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

    – Ctx
    Jan 3 at 0:46






  • 1





    If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

    – David C. Rankin
    Jan 3 at 1:14














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014870%2fnode-in-linked-list-not-being-deleted-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_fiber,"Polycarbonate");


should be



strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");


Otherwise you are printing uninitialized memory (which is the reason for your "Some Random Symbols")



Furthermore, if you are trying to delete the second node, you have to modify the first node (head):



head->next = head->next->next;


To free the deleted node, first save head->next to a temporary variable and free it at last.






share|improve this answer





















  • 1





    @AMD No, an assignment to anothertemp does not modify the head of your list at all.

    – Ctx
    Jan 3 at 0:32






  • 1





    For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

    – Ctx
    Jan 3 at 0:36






  • 2





    @AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

    – Ctx
    Jan 3 at 0:42






  • 2





    @AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

    – Ctx
    Jan 3 at 0:46






  • 1





    If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

    – David C. Rankin
    Jan 3 at 1:14


















2














strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_fiber,"Polycarbonate");


should be



strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");


Otherwise you are printing uninitialized memory (which is the reason for your "Some Random Symbols")



Furthermore, if you are trying to delete the second node, you have to modify the first node (head):



head->next = head->next->next;


To free the deleted node, first save head->next to a temporary variable and free it at last.






share|improve this answer





















  • 1





    @AMD No, an assignment to anothertemp does not modify the head of your list at all.

    – Ctx
    Jan 3 at 0:32






  • 1





    For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

    – Ctx
    Jan 3 at 0:36






  • 2





    @AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

    – Ctx
    Jan 3 at 0:42






  • 2





    @AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

    – Ctx
    Jan 3 at 0:46






  • 1





    If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

    – David C. Rankin
    Jan 3 at 1:14
















2












2








2







strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_fiber,"Polycarbonate");


should be



strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");


Otherwise you are printing uninitialized memory (which is the reason for your "Some Random Symbols")



Furthermore, if you are trying to delete the second node, you have to modify the first node (head):



head->next = head->next->next;


To free the deleted node, first save head->next to a temporary variable and free it at last.






share|improve this answer















strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_fiber,"Polycarbonate");


should be



strcpy(data->lamina_fiber,"Kevlar");
strcpy(data->lamina_matrix,"Polycarbonate");


Otherwise you are printing uninitialized memory (which is the reason for your "Some Random Symbols")



Furthermore, if you are trying to delete the second node, you have to modify the first node (head):



head->next = head->next->next;


To free the deleted node, first save head->next to a temporary variable and free it at last.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 0:27

























answered Jan 3 at 0:24









CtxCtx

11.5k82238




11.5k82238








  • 1





    @AMD No, an assignment to anothertemp does not modify the head of your list at all.

    – Ctx
    Jan 3 at 0:32






  • 1





    For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

    – Ctx
    Jan 3 at 0:36






  • 2





    @AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

    – Ctx
    Jan 3 at 0:42






  • 2





    @AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

    – Ctx
    Jan 3 at 0:46






  • 1





    If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

    – David C. Rankin
    Jan 3 at 1:14
















  • 1





    @AMD No, an assignment to anothertemp does not modify the head of your list at all.

    – Ctx
    Jan 3 at 0:32






  • 1





    For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

    – Ctx
    Jan 3 at 0:36






  • 2





    @AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

    – Ctx
    Jan 3 at 0:42






  • 2





    @AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

    – Ctx
    Jan 3 at 0:46






  • 1





    If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

    – David C. Rankin
    Jan 3 at 1:14










1




1





@AMD No, an assignment to anothertemp does not modify the head of your list at all.

– Ctx
Jan 3 at 0:32





@AMD No, an assignment to anothertemp does not modify the head of your list at all.

– Ctx
Jan 3 at 0:32




1




1





For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

– Ctx
Jan 3 at 0:36





For example this: anothertemp=temp->nextnode; anothertemp=temp; is always wrong, you are just assigning two different pointers to anothertemp which you don't use at all. You only assign to anothertemp and never read from it.

– Ctx
Jan 3 at 0:36




2




2





@AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

– Ctx
Jan 3 at 0:42





@AMD It is key that you try to understand, that anothertemp is only written to and never used/read. You can eliminate anothertemp completely, it will change nothing.

– Ctx
Jan 3 at 0:42




2




2





@AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

– Ctx
Jan 3 at 0:46





@AMD yes, that's what head->next = head->next->next does. In general: Iterate to the node before the node to delete, and do node->next = node->next->next(but remember to save the pointer to free it after the assignment)

– Ctx
Jan 3 at 0:46




1




1





If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

– David C. Rankin
Jan 3 at 1:14







If the ->next->next soup gives you heartache, you can always just save a prev pointer to bracket the nodes, e.g. prev, node_to_del, next. It's up to you. Key is to draw it out on paper, erase the node_to_del and then code what you must do to connect the prev->next pointer to next.

– David C. Rankin
Jan 3 at 1:14






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014870%2fnode-in-linked-list-not-being-deleted-properly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory