Three cases where a node is inserted in a linked list

Source: Internet
Author: User

Inserting an element into a linked list can be divided into three cases:

1, at the time of the node

2. Any position in the middle of the linked list

3, in the last position of the list, you can also think that this situation is appended (this is left to append to the time to achieve)

The following is the implementation of the Code

SN *insert_s_node (SN *head)/* The parameters passed in are inserted into the list of head pointers */{SN *insert_node=null, *dest_node = null;/* Insert_node is the node in the new linked list to be made Dest_node is the node to be inserted */int32 OSM = 1, i32i = 0, flag = 0; Dest_node = (sn*) malloc (sizeof (SN)), Insert_node = HEAD;OSM = osm_printf ("Enter location to insert:");D est_node->sensor_rating = s Canf_for (); /* Enter the inserted ordinal */i32i = GetChar ();/* Eliminate the effect of carriage return */if (0 > dest_node->sensor_rating)/* prevent input of less than 0 */{osm = osm_printf ("Please Lose into a number greater than 0 \ n "); ELSE{OSM = osm_printf ("Enter the element to insert:");/* The element that is inserted */if (NULL! = Dest_node->sensor_title) {Gets (dest_node->sensor_title );/* Input inserted element */}/* traverse to position to insert */while (insert_node->next! = NULL) {insert_node = insert_node->next;if (0 = = flag) {if (dest_node->sensor_rating = = insert_node->next->sensor_rating)/* Determine equality */{dest_node->next = Insert_ node->next;/* start Execution Q->next = P->next Q->next = P */insert_node->next = Dest_node;flag = 1;/* Set Flag */Insert_N Ode = Dest_node->next;}} if (1 = = flag) {insert_node->sensor_rating++;}}} ReTurn head;} 

This can be achieved in the middle of the insertion

But there was a problem when the implementation was inserted at the head node.



This problem is not taken into account in the case of the head node being inserted

Three cases where a node is inserted in a 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.