Single-linked list sequential insertion

Source: Internet
Author: User

Inserts a new element into the single-linked list and guarantees that the element is ordered after insertion.

"Program Instance"

1 structListNode2 {3     intVal;4ListNode *Next;5 };6 7 BOOLSll_insert (ListNode **root,intvalue)8 {9ListNode *prevnode =NULL;TenListNode *current = *Root; One  A      while(Current!=null && current->val<value) -     { -Prevnode=Current ; theCurrent=current->Next; -     } -  -ListNode *new =NewListNode; +     if(New = =NULL) -         return false; +  ANew->val=value; at  -new->next=Current ; -  -     if(PrevNode = =NULL) -*root=New; -     Else inprevnode->next=New; -  to     return true; +}

===============================================================================

An improved approach:

Analysis: In the above method, we need to specify two pointers, one to the current node, and one to the previous node of the current node (note that its initialization is null to mark the position to be inserted at the first node). The method needs to differentiate between two situations:

(1) The position to be inserted is in the middle or tail of the single-linked list, at which point after the pointer to the previous node of the current node, even if the location of the newly inserted node is to be inserted, simply connect the current node to the back of the new insertion node to complete the insertion of the node:

1 prevnode->next=New; 2 new->next=current;

(2) When the position to insert the node is a node, the pointer to the previous node of the current node (that is, the first node) is PrevNode null (because it has not yet been assigned a value in the node), only the next node of the newly inserted node is set to the current node. Instead, set the root node to the newly inserted node:

1 new->next= current; 2 *root=new;

When the original linked list is empty, the case is also included in the case.

Because the above code needs to be divided, the algorithm is optimized as follows:

We try to use a pointer to complete the task, the pointer to the current node is essential, because we need it to access the Val in the node. In the process of constant comparison, the pointer to the final current node is the one that points to the node to be inserted.

However, when we adjust the form of the node so that the next field is at the beginning of the node, the value of the pointer to that node is the address at the beginning of the node, and the address is also a pointer to the start position of the next field. When we want to insert a node, we just need to point the pointer to the next field of the current node to the address of the next field of the newly inserted node, and the next point of the new insert to the pointer to the current node.

"Program Instance"

1 structListNode2 {3ListNode *Next;4     intVal;5 };6 7 intSll_insert (ListNode **root,intvalue)8 {9ListNode *current=NULL;Ten  One      while((current=*root)!=null && current->val<value) A     { -Root=&current->Next; -     } the  -ListNode *new =NewListNode; -     if(New = =NULL) -         return-1; +New->val=value; -  +new->next=Current ; A*root=new;//A pointer to the old position is attached to the back of the newly inserted node relative to the first value that is greater than value is re-assigned at  -     return 0; -}

Single-linked list sequential insertion

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.