天天看点

双链表反序(链表)

双链表反序(链表)
Every storm is part of your journey. 

Yes, you will make it through this one too. You will come out stronger than you were 

before...      

双链表定义

typedef struct _dNode
{
    int data;
    struct _dNode *pre;
    struct _dNode *next;
}DNode;      

关键点

1.关键在于节点中的pre, next指针,只要交换他们的值,就可以将双链表反序。

int ReverseDoubleList(DNode * h)
{
    if(!h || !h->next) {
        printf("DLinkedNode:Reverse!\n");
        return -1;
    }
    
    DNode *p,*temp;
    p=h->next;
    while(p!=NULL) {
        temp=p->pre;
        p->pre=p->next;
        p->next=temp;
 
        if(p->pre==NULL)
            break;
        else
            p=p->pre;
    }
    
    h->next->next=NULL;
    h->next=p;
    return 0;
}