A null pointer exception to note about the list algorithm

Source: Internet
Author: User

In the study of data structure, I believe that many pen pals will be pointers to this knowledge point trapped, a small design to the pointer algorithm may take you a lot of time to be able to perfect, to blame the end is not aware of the occurrence of null pointers. I'm holding a chestnut below:

A single linked list L with a leading node is known, and its nodes are defined as follows:

1 template <typename t>2struct  linklist{3T data; 4 linklist<t> * head; 5 }

Now design an algorithm to find the logical structure of the last node with the value e:
Oh, look very simple, just need to go through the list to find the results, the following is attached to my original code:

1Template <typename t>2 intFindLast (linklistclass<t>l,t e)3 {4Linklist<t> * p=L.head;5     intposition=0, i=0;6     While (p!=null) ①7     {8       p=p->next; ②9i++;Ten        if (p->data==e) ③ Oneposition=i; A     } -      -     returnPosition//returns 0 if the lookup successfully returns its logical sequence number the}

Then I compile, link, there is no error, but during the operation of the error can not continue to run, this is a run-time error

See, do not know what is wrong, so I debug a bit, found that the root of the problem lies in the above source code in the order of 123 marks.

This function ignores the null pointer exception error, if the pointer p to the tail node of the chain list L, at this time into the while loop, after the statement ②,p==null, and then execute ③ will be packet null pointer exception

The problem is found, the solution will be much easier, below I cite a solution code (in fact there are many)

1Template<typename t>2 intFindLast (linklistclass<t>&L, T e)3 {4     intPosition =0;5     inti =1;6linklist<t> * p = l.head->Next;7      while(P! =NULL)8     {9         if(P->data = =e)TenPosition =i; Onei++; Ap = p->Next; -     } -     returnposition; the}


How, the problem is small, but need to pay attention to AH! Usually programming must pay attention to these boundary value problems, especially the algorithm that involves pointers.

A null pointer exception to note about the list algorithm

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.