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