in this function correct pointer not returned to the calling functionWhat are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?Using global variables in a functionHow do function pointers in C work?Typedef function pointer?Why should I use a pointer rather than the object itself?Function says node is deleted but is not deletedPointers issue in linked list function's return I cannot figureLinked list delete node. Free(pointer) prints 0 in next nodeIn my Singly linked List implementation why is it even though I allocated memory for the node to be freed, the pointer to the Node isn't NULL?

Metric version of "footage"?

Create dashed intersections with labels using pgfplots and tikz

I quit, and boss offered me 3 month "grace period" where I could still come back

Can I play a first turn Simic Growth Chamber to have 3 mana available in the second turn?

Is killing off one of my queer characters homophobic?

How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?

Chaining Dissonant Whispers via War Caster feat

How can an advanced civilization forget how to manufacture its technology?

Alternatives to using writing paper for writing practice

When did the Roman Empire fall according to contemporaries?

School House Points (Python + SQLite)

Ambiguous sentences: How to tell when they need fixing?

Why does Hellboy file down his horns?

Won 50K! Now what should I do with it

What is the commentary on Leviticus 21:2-4 - why is wife not included on the list

Measuring mystery distances

Cubic programming and beyond?

Is this floating-point optimization allowed?

Did any of the founding fathers anticipate Lysander Spooner's criticism of the constitution?

Is it rude to tell recruiters I would only change jobs for a better salary?

Why the term 'unified' in "unified mass unit"?

Are there any double stars that I can actually see orbit each other?

In which ways do anagamis still experience ignorance?

TikZ Can I draw an arrow by specifying the initial point, direction, and length?



in this function correct pointer not returned to the calling function


What are the differences between a pointer variable and a reference variable in C++?What is a smart pointer and when should I use one?Using global variables in a functionHow do function pointers in C work?Typedef function pointer?Why should I use a pointer rather than the object itself?Function says node is deleted but is not deletedPointers issue in linked list function's return I cannot figureLinked list delete node. Free(pointer) prints 0 in next nodeIn my Singly linked List implementation why is it even though I allocated memory for the node to be freed, the pointer to the Node isn't NULL?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















the problem is when i print the list after deleting the node the printing function prints 0 in place of the deleted node.....but i wanted it to print nothing.



// the function call is delete_from_key(&head,i);
//node is struct linked_list
void delete_from_key(node *head, int key)

node *new, *temp;
if(head == NULL)

printf("nothing to delete . the list is empty.n");
return;

else if(head->num == key)

temp=head;
head=temp->next;
free(temp);
return;

new=search_key(head, key);//search_key returns pointer node holding the key.
if(new != NULL)

temp = new;
new = new->next;
free(temp);
return;




example:



the list is 1-> 2-> 3-> 4-> 5->

if i called this function with key 2

the expected output is 1-> 3-> 4-> 5->

instead the actual output is 1-> 0-> 3-> 4-> 5->










share|improve this question
























  • Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

    – StoryTeller
    Mar 26 at 6:02











  • temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

    – sameerkn
    Mar 26 at 6:08











  • You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

    – H.S.
    Mar 26 at 6:10












  • @sameerkn don't answer in comments.

    – bolov
    Mar 26 at 6:11











  • You have already checked if head is NULL in first if, so you don't need to check again in second...

    – Aconcagua
    Mar 26 at 6:15

















2















the problem is when i print the list after deleting the node the printing function prints 0 in place of the deleted node.....but i wanted it to print nothing.



// the function call is delete_from_key(&head,i);
//node is struct linked_list
void delete_from_key(node *head, int key)

node *new, *temp;
if(head == NULL)

printf("nothing to delete . the list is empty.n");
return;

else if(head->num == key)

temp=head;
head=temp->next;
free(temp);
return;

new=search_key(head, key);//search_key returns pointer node holding the key.
if(new != NULL)

temp = new;
new = new->next;
free(temp);
return;




example:



the list is 1-> 2-> 3-> 4-> 5->

if i called this function with key 2

the expected output is 1-> 3-> 4-> 5->

instead the actual output is 1-> 0-> 3-> 4-> 5->










share|improve this question
























  • Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

    – StoryTeller
    Mar 26 at 6:02











  • temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

    – sameerkn
    Mar 26 at 6:08











  • You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

    – H.S.
    Mar 26 at 6:10












  • @sameerkn don't answer in comments.

    – bolov
    Mar 26 at 6:11











  • You have already checked if head is NULL in first if, so you don't need to check again in second...

    – Aconcagua
    Mar 26 at 6:15













2












2








2








the problem is when i print the list after deleting the node the printing function prints 0 in place of the deleted node.....but i wanted it to print nothing.



// the function call is delete_from_key(&head,i);
//node is struct linked_list
void delete_from_key(node *head, int key)

node *new, *temp;
if(head == NULL)

printf("nothing to delete . the list is empty.n");
return;

else if(head->num == key)

temp=head;
head=temp->next;
free(temp);
return;

new=search_key(head, key);//search_key returns pointer node holding the key.
if(new != NULL)

temp = new;
new = new->next;
free(temp);
return;




example:



the list is 1-> 2-> 3-> 4-> 5->

if i called this function with key 2

the expected output is 1-> 3-> 4-> 5->

instead the actual output is 1-> 0-> 3-> 4-> 5->










share|improve this question
















the problem is when i print the list after deleting the node the printing function prints 0 in place of the deleted node.....but i wanted it to print nothing.



// the function call is delete_from_key(&head,i);
//node is struct linked_list
void delete_from_key(node *head, int key)

node *new, *temp;
if(head == NULL)

printf("nothing to delete . the list is empty.n");
return;

else if(head->num == key)

temp=head;
head=temp->next;
free(temp);
return;

new=search_key(head, key);//search_key returns pointer node holding the key.
if(new != NULL)

temp = new;
new = new->next;
free(temp);
return;




example:



the list is 1-> 2-> 3-> 4-> 5->

if i called this function with key 2

the expected output is 1-> 3-> 4-> 5->

instead the actual output is 1-> 0-> 3-> 4-> 5->







c pointers scope dynamic-memory-allocation singly-linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 6:43







acidus101

















asked Mar 26 at 5:55









acidus101acidus101

143 bronze badges




143 bronze badges












  • Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

    – StoryTeller
    Mar 26 at 6:02











  • temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

    – sameerkn
    Mar 26 at 6:08











  • You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

    – H.S.
    Mar 26 at 6:10












  • @sameerkn don't answer in comments.

    – bolov
    Mar 26 at 6:11











  • You have already checked if head is NULL in first if, so you don't need to check again in second...

    – Aconcagua
    Mar 26 at 6:15

















  • Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

    – StoryTeller
    Mar 26 at 6:02











  • temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

    – sameerkn
    Mar 26 at 6:08











  • You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

    – H.S.
    Mar 26 at 6:10












  • @sameerkn don't answer in comments.

    – bolov
    Mar 26 at 6:11











  • You have already checked if head is NULL in first if, so you don't need to check again in second...

    – Aconcagua
    Mar 26 at 6:15
















Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

– StoryTeller
Mar 26 at 6:02





Don'e spam tags. new is a keyword in C++, so there is no possible way this is part of a C++ program. Tags are to define the scope of a question correctly, not to gain exposure.

– StoryTeller
Mar 26 at 6:02













temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

– sameerkn
Mar 26 at 6:08





temp = new->next; //temp points to node holding the key...new->next = temp->next; //new->next points to node which is after the node holding the key....free(temp);

– sameerkn
Mar 26 at 6:08













You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

– H.S.
Mar 26 at 6:10






You have missed setting the previous pointer of new to next pointer of new when new is not NULL. You have to keep track of previous pointer of the node which you want to delete.

– H.S.
Mar 26 at 6:10














@sameerkn don't answer in comments.

– bolov
Mar 26 at 6:11





@sameerkn don't answer in comments.

– bolov
Mar 26 at 6:11













You have already checked if head is NULL in first if, so you don't need to check again in second...

– Aconcagua
Mar 26 at 6:15





You have already checked if head is NULL in first if, so you don't need to check again in second...

– Aconcagua
Mar 26 at 6:15












2 Answers
2






active

oldest

votes


















4














You need to update the previous node's next as well as it's next is getting deleted.



prev_node=search_key(head, key);//search_key returns pointer to the node just before the node holding the key.

if(prev_node!= NULL)

// prev_node->next is the one that needs to be deleted
temp = prev_node->next;

//Make the prev node point to the next node of the one that's getting deleted
prev_node->next = temp->next;

free(temp);
return;






share|improve this answer

























  • i updated the search function but forgot to update the comment. i have edited it.thanks!

    – acidus101
    Mar 26 at 6:34


















3














First bug



head=temp->next;


Since head is a local parameter this assignment changes this local parameter. You need to change the pointer passed to the function, so you need your code to be:



void delete_from_key(node **head, int key)
//...
*head=temp->next;
//...


2nd bug



new = new->next;


Kind of the same problem. new is a local variable. You are changing a local variable, you aren't changing the pointer within the list.



If search_key indeed returns pointer to the node before the found key you need



temp = new->next;
new->next = new->next->next;
free(temp);





share|improve this answer























  • it worked thank you!

    – acidus101
    Mar 26 at 6:32













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%2f55350648%2fin-this-function-correct-pointer-not-returned-to-the-calling-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You need to update the previous node's next as well as it's next is getting deleted.



prev_node=search_key(head, key);//search_key returns pointer to the node just before the node holding the key.

if(prev_node!= NULL)

// prev_node->next is the one that needs to be deleted
temp = prev_node->next;

//Make the prev node point to the next node of the one that's getting deleted
prev_node->next = temp->next;

free(temp);
return;






share|improve this answer

























  • i updated the search function but forgot to update the comment. i have edited it.thanks!

    – acidus101
    Mar 26 at 6:34















4














You need to update the previous node's next as well as it's next is getting deleted.



prev_node=search_key(head, key);//search_key returns pointer to the node just before the node holding the key.

if(prev_node!= NULL)

// prev_node->next is the one that needs to be deleted
temp = prev_node->next;

//Make the prev node point to the next node of the one that's getting deleted
prev_node->next = temp->next;

free(temp);
return;






share|improve this answer

























  • i updated the search function but forgot to update the comment. i have edited it.thanks!

    – acidus101
    Mar 26 at 6:34













4












4








4







You need to update the previous node's next as well as it's next is getting deleted.



prev_node=search_key(head, key);//search_key returns pointer to the node just before the node holding the key.

if(prev_node!= NULL)

// prev_node->next is the one that needs to be deleted
temp = prev_node->next;

//Make the prev node point to the next node of the one that's getting deleted
prev_node->next = temp->next;

free(temp);
return;






share|improve this answer















You need to update the previous node's next as well as it's next is getting deleted.



prev_node=search_key(head, key);//search_key returns pointer to the node just before the node holding the key.

if(prev_node!= NULL)

// prev_node->next is the one that needs to be deleted
temp = prev_node->next;

//Make the prev node point to the next node of the one that's getting deleted
prev_node->next = temp->next;

free(temp);
return;







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 6:17

























answered Mar 26 at 6:10









Wander3rWander3r

7526 silver badges18 bronze badges




7526 silver badges18 bronze badges












  • i updated the search function but forgot to update the comment. i have edited it.thanks!

    – acidus101
    Mar 26 at 6:34

















  • i updated the search function but forgot to update the comment. i have edited it.thanks!

    – acidus101
    Mar 26 at 6:34
















i updated the search function but forgot to update the comment. i have edited it.thanks!

– acidus101
Mar 26 at 6:34





i updated the search function but forgot to update the comment. i have edited it.thanks!

– acidus101
Mar 26 at 6:34













3














First bug



head=temp->next;


Since head is a local parameter this assignment changes this local parameter. You need to change the pointer passed to the function, so you need your code to be:



void delete_from_key(node **head, int key)
//...
*head=temp->next;
//...


2nd bug



new = new->next;


Kind of the same problem. new is a local variable. You are changing a local variable, you aren't changing the pointer within the list.



If search_key indeed returns pointer to the node before the found key you need



temp = new->next;
new->next = new->next->next;
free(temp);





share|improve this answer























  • it worked thank you!

    – acidus101
    Mar 26 at 6:32















3














First bug



head=temp->next;


Since head is a local parameter this assignment changes this local parameter. You need to change the pointer passed to the function, so you need your code to be:



void delete_from_key(node **head, int key)
//...
*head=temp->next;
//...


2nd bug



new = new->next;


Kind of the same problem. new is a local variable. You are changing a local variable, you aren't changing the pointer within the list.



If search_key indeed returns pointer to the node before the found key you need



temp = new->next;
new->next = new->next->next;
free(temp);





share|improve this answer























  • it worked thank you!

    – acidus101
    Mar 26 at 6:32













3












3








3







First bug



head=temp->next;


Since head is a local parameter this assignment changes this local parameter. You need to change the pointer passed to the function, so you need your code to be:



void delete_from_key(node **head, int key)
//...
*head=temp->next;
//...


2nd bug



new = new->next;


Kind of the same problem. new is a local variable. You are changing a local variable, you aren't changing the pointer within the list.



If search_key indeed returns pointer to the node before the found key you need



temp = new->next;
new->next = new->next->next;
free(temp);





share|improve this answer













First bug



head=temp->next;


Since head is a local parameter this assignment changes this local parameter. You need to change the pointer passed to the function, so you need your code to be:



void delete_from_key(node **head, int key)
//...
*head=temp->next;
//...


2nd bug



new = new->next;


Kind of the same problem. new is a local variable. You are changing a local variable, you aren't changing the pointer within the list.



If search_key indeed returns pointer to the node before the found key you need



temp = new->next;
new->next = new->next->next;
free(temp);






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 26 at 6:10









bolovbolov

35.1k9 gold badges79 silver badges145 bronze badges




35.1k9 gold badges79 silver badges145 bronze badges












  • it worked thank you!

    – acidus101
    Mar 26 at 6:32

















  • it worked thank you!

    – acidus101
    Mar 26 at 6:32
















it worked thank you!

– acidus101
Mar 26 at 6:32





it worked thank you!

– acidus101
Mar 26 at 6:32

















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%2f55350648%2fin-this-function-correct-pointer-not-returned-to-the-calling-function%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript