这一版本主要是对第一版本各函数进行优化,包括代码本身的优化和函数功能的优化~~/************************************************************************/ /* author : thomas E-mail: chenhua308@gmail.com */ /************************************************************************/ #include <stdio.h> #include <malloc.h> typedef struct tag { int data; struct tag *next; struct tag *pre; } dnode; //create bi-list dnode *creat( int i ) { dnode *head; head = (dnode *)malloc(sizeof(dnode)); head -> next = NULL; head -> pre = NULL; head -> data = i; return head; } //compute the length of bi-list (retrieve in the direction of form left to right) int length( dnode *head ) { int n = 0; dnode *p; p = head; while ( p != NULL ) { n++; p = p -> next; } return n; } //print bi-list(from left to right) void printnext( dnode *head ) { dnode *p; int n = length( head ); int i ; if ( n == 0 ) { printf("the link list is empty! /n"); } else { printf("/n these %d records are : /n", n); p = head; for ( i = 1; i <=n; i++) { printf("the %d record is: %d /n", i, p -> data ); if ( i != n) { p = p -> next; } } } } //print bi-list(from right to left) void printprev( dnode *head ) { dnode *p; int n = length( head ); int i; if ( n == 0 ) { printf("the link list is empty! /n"); } else { printf("/n these %d records are : /n", n); p = head; //move to the end of list while ( p -> next != NULL ) p = p -> next; for ( i = 1; i <=n; i++) { printf("the %d record is: %d /n", i, p -> data ); if ( i != n) { p = p -> pre; } } } } //insert in the direction of ascend dnode *insertascend( dnode *head, int num ) { dnode *p0, *previous, *current; p0 = (dnode *)malloc( sizeof ( dnode ) ); p0 -> data = num; previous = NULL; current = head; //find the location of insert the new node while ( current != NULL && current -> data < num ) { previous = current; current = current -> next; } p0 -> next = current; //这个地方和单链表有点区别,需要判断是否是在链表尾部添加 if ( current != NULL ) { current -> pre = p0; } if ( previous == NULL ) { head = p0; p0 -> pre = NULL; //p0 -> next = current; //current -> pre = p0; } else { //p0 -> next = current; previous -> next = p0; //current -> pre = p0; p0 -> pre = previous; } return head; } //in the direction of downward dnode *insertdown( dnode *head, int num ) { dnode *p0, *previous, *current; p0 = (dnode *)malloc( sizeof ( dnode ) ); p0 -> data = num; previous = NULL; current = head; //find the location of insert the new node while ( current != NULL && current -> data > num ) { previous = current; current = current -> next; } p0 -> next = current; if ( current != NULL ) { current -> pre = p0; } if ( previous == NULL ) { //insert in front of head node head = p0; p0 -> pre = NULL; } else { previous -> next = p0; p0 -> pre = previous; } return head; } //delete the specified node(function bad to the list that has just one element) dnode *del(dnode *head, int num ) { dnode *current; current = head; if ( head == NULL ) { printf("the link list is empty! /n"); } else { while ( current != NULL && current -> data != num ) { current = current -> next; } if ( current == NULL ) { printf("the element is not found! /n"); } else { if ( current -> pre == NULL ) { //delete the head node head = current -> next; current -> next ->pre = NULL; } else { current -> pre -> next = current -> next; //若删除的是尾节点则不需要下面这步了 if ( current -> next != NULL ) { current -> next -> pre = current -> pre; } } } } return head; } //上一版本似乎可读性不强,写个可读性稍强的,并且可以对仅有一个元素的链表进行删除 dnode *del2(dnode *head, int num ) { dnode *current, *previous; current = head; previous = NULL; if ( head == NULL ) { printf("the link list is empty! /n"); } else { while ( current != NULL && current -> data != num ) { previous = current; current = current -> next; } if ( current == NULL ) { printf("the element is not found! /n"); } else { if ( previous == NULL ) { //delete the head node head = current -> next; if ( current -> next != NULL ) { //except the situation that just one node exist current -> next -> pre = NULL; } } else { previous -> next = current -> next; //若删除的是尾节点则不需要下面这步了 if ( current -> next != NULL ) { current -> next -> pre = previous; } } } } return head; } void main(int argc, char *argv[]) { dnode *head; head = creat(4); printnext(head); printprev(head); // head = insertascend(head, 5); head = insertdown(head, 5); printnext(head); printprev(head); //head = insertascend(head, 2); head = insertdown(head, 2); printnext(head); printprev(head); head = del2(head, 5); printnext(head); printprev(head); head = del2(head, 2); printnext(head); printprev(head); head = del2(head, 4); printnext(head); printprev(head); } 想附上一张调试图,但似乎不简单,唉~~~下次吧!