最近对双向链表的研究(比较好的插入双向链表的程序)
[复制链接]
#include<stdio.h> #include<stdlib.h>
#define FALSE 0 #define TRUE 1 typedef struct NODE { struct NODE *fwd; struct NODE *bwd; int value; }Node;
//巧妙地插入函数 int dll_insert(Node *rootp, int value ) { register Node *this; register Node *next; register Node *newnode;
for(this = rootp;(next = this->fwd)!=NULL;this=next) { if(next->value == value) return 0; if(next->value > value) break; } newnode = (Node *)malloc(sizeof(Node)); if(newnode == NULL) return -1; newnode->value = value;
newnode->fwd = next; this->fwd = newnode;
//分别判断两个指针的位置 if( this != rootp ) newnode->bwd = this; //正常插入链表 else newnode->bwd = NULL; //newnode作为根节点
if( next != NULL ) next->bwd = newnode; //正常插入链表 else rootp->bwd = newnode; //newnode做为根节点,newnode->fwd=rootp;
return 1;
}
|