Double-linked list insertion

Source: Internet
Author: User

Pre-compilation

#include <stdlib.h><stdio.h>#define status int#define TRUE 1#define FALSE 0

struct node{    int value;            /*  */    struct NODE *prev;  /*  */    struct NODE *next;  /*  */}node;
Data Structure
1 /*2 function Description:3 inserting new nodes (from small to large)4 5 Parameters:6 ROOTP--a double pointer to the root node7 New_value--To insert a new value for the linked list8 9 return Value:Ten returns True if the insert succeeds; One Otherwise, returns false A */ - Status -Insert (Node **ROOTP,intnew_value) the { -Node *current = *ROOTP;  -Node *_new =NULL; -  +     /*Find insertion Position*/ -      while(Current! =NULL) { +         if(Current->value >new_value) A              Break; at  -         if(Current->value = =new_value) { -printf"value%d already exists \ n", new_value); -             returnFALSE; -         } -Current = Current->Next; in     } -  to     /*Create a new node*/ +_new = (Node *)malloc(sizeof(Node)); -     if(_new = =NULL) { theprintf"not enough memory \ n" ); *         returnFALSE; $     }Panax Notoginseng_new->value =New_value; -  the     /*Insert new node*/ +     if(Current = =NULL) { A         if(current = = *ROOTP) {/*insert to empty list*/ the*ROOTP =_new; +_new->prev =_new; -_new->next =NULL; $          $}Else{/*Insert to Trailer*/ -_new->next =NULL; -_new->prev = (*ROOTP)prev; the(*ROOTP)->prev->next =_new; -(*ROOTP)->prev =_new;Wuyi         } the}Else{ -         if(current = = *ROOTP) {/*before inserting to the root node*/ Wu_new->prev = (*ROOTP)prev; -_new->next =Current ; AboutCurrent->prev =_new; $*ROOTP =_new; -  -}Else{/*before inserting a node in the middle.*/ -_new->prev = current->prev; ACurrent->prev->next =_new; +_new->next =Current ; theCurrent->prev =_new; -         } $     } the  the     returnTRUE; the}
inserting code

1 /*2 function Description:3 inserting new nodes (from small to large)4 5 Parameters:6 ROOTP--a double pointer to the root node7 New_value--To insert a new value for the linked list8 9 return Value:Ten returns True if the insert succeeds; One Otherwise, returns false A */ - Status -Insert (Node **ROOTP,intnew_value) the { -Node *current = *ROOTP;  -Node *_new =NULL; -  +     /*Find insertion Position*/ -      while(Current! =NULL) { +         if(Current->value >new_value) A              Break; at  -         if(Current->value = =new_value) { -printf"value%d already exists \ n", new_value); -             returnFALSE; -         } -Current = Current->Next; in     } -  to     /*Create a new node*/ +_new = (Node *)malloc(sizeof(Node)); -     if(_new = =NULL) { theprintf"not enough memory \ n" ); *         returnFALSE; $     }Panax Notoginseng_new->value =New_value; -  the     /*Insert new node*/ +     if(current = = NULL) {/*To insert a root or tail node.*/ A         if(Current! = *ROOTP) { the(*ROOTP)->prev->next =_new;  +         } -_new->prev = (current = = *ROOTP)? _new: (*ROOTP)prev; $_new->next =NULL; $(current = = *ROOTP)? *ROOTP: (*ROOTP)->prev) =_new; -          -}Else{/*before inserting to the root node or the middle node.*/ the_new->prev = current->prev; -(current = = *ROOTP)? current->prev:current->prev->next) =_new;Wuyi_new->next =Current ; the(current = = *ROOTP)? *rootp:current->prev) =_new; -     } Wu  -     returnTRUE; About}
Optimized Insert Code

Double-linked list 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.