天天看點

雙連結清單反序(連結清單)

雙連結清單反序(連結清單)
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;
}