C-language linked list operations

Source: Internet
Author: User

The V2 version is non-healthy and obviously no more front-end insertion.

INT SLL_INSERT_V2 (Node *current, int value) {    node *prev;     Node *newNode;    while  (current != null & & current->value >= value)  {        prev  = current;        current = current->next;     }    newnode = malloc (sizeof (Node));     if  (newnode == null)  {        return  false;    }    newnode->value = value;     if  (prev == null)  {        newnode->next  = current->next;        current->next =  Newnode;    } else  {        newnode->next =  current;        prev->next = newnode;     }    return true;}

The V3 version implements the Insert function, but it is clear that all inserts are:

The new node is chained to the next node of the current node

Current node chain to new node

So with the third version, all we need is a pointer to the current node and link to the current node.

Int sll_insert_v3 (Node **rootp, int value) {    node *newnode;     Node *current;    Node *prev;     current = *rootp;    while  (current != null &&  Current->value >= value)  {        prev =  current;        current=  current->next;     }    newnode = malloc (sizeof (Node));     newnode->value = value;    newnode->next = current;     if  (prev == null)  {        *ROOTP   = newNode;    } else {         prev->next = nEwnode;    }    return true;} 

V4 Version: Current version, ROOTP as a pointer to link to the current node

int sll_insert_v4 (node **rootp, int value) {node *newnode;    Node *current;    while (current = *ROOTP) = NULL && current->value >= value) {ROOTP = &current->next;    } NewNode = malloc (sizeof (Node));    Newnode->value = value;    Newnode->next = current->next;    *ROOTP = NewNode; return TRUE;}

C-language linked list operations

Related Article

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.