Insert problem with linked list

Source: Internet
Author: User

BUG: Today I wrote a very SB code, the problem actually I have noticed, in the previous writing such code, I also left a mind, but may be a long time not to write these things lead to new problems arising. In fact, I have written a note about this problem before, and now write a deeper impression. The code is first attached:
  
 
  1. #ifndef LIST_INSERTREMOVEPRINT_H
  2. #define LIST_INSERTREMOVEPRINT_H
  3. #include<iostream>
  4. struct ListNode{
  5. int val;
  6. struct ListNode *nxt;
  7. };
  8. void addListNode(struct ListNode*list,int value){
  9. struct ListNode *nodeInsert=(struct ListNode*)(malloc(sizeof(struct ListNode)));
  10. nodeInsert->val=value;
  11. nodeInsert->nxt=NULL;
  12. if(list==NULL){
  13. list=nodeInsert;
  14. return ;
  15. }
  16. struct ListNode *iter=list;
  17. while(iter->nxt!=NULL){
  18. iter=iter->nxt;
  19. }
  20. iter->nxt=nodeInsert;
  21. }
  22. #end
In fact, a familiar person will know where the problem is at a glance!!!!! The highlight section illustrates the problem. The reason for making this mistake is that the passing of the pointer is not very clear.
 
   
  
  1. void addListNode(struct ListNode*list,int value)
There are two parameters in this function, one is the pointer type of the linked list node, and the other one is the int type. For this list node pointer, what are we passing in? Of course, the incoming is an address, because it is a pointer. Well, how do we pass this pointer, the value of!!!!!!!. You pass the address or the value of Ah, but this value is "address" and so on. Now that you understand this, the problem is, you change it in the function body.
 
   
  
  1. list=nodeInsert
What do you mean by this? ,,, to pay the list for this Nodeinsert address? That is, you have this function.
 
   
  
  1. void test(int a,int value){
  2. int c;
  3. a=c;
  4. }
What do you mean? Do you really want to change the argument value by A=c in the function body? Get the hell out of here.
 
   
  
  1. iter->nxt=nodeInsert;
This is even more ridiculous, you are still changing the value of the parameter, this must not be.
How to fix this bug1, if you want to change the argument through the parameter, for the address of the value of the problem, you can use "*"-----dereference operator.
That is , we use this solution to refer to this parameter, so that we find the corresponding space of the formal parameter address, in memory to change the value of this real. 2, the pointer to the pointer, and then the dereference operator. The argument can be changed by the parameter!!!!!!, because it has to be dereferenced.       


From for notes (Wiz)

Insert problem with linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.