读书人

单链表转换为循环双链表 哪里不对,该怎

发布时间: 2012-05-22 18:18:54 作者: rapoo

单链表转换为循环双链表 哪里不对

C/C++ code
struct node{    int data;    node* next;    node(int d,node* n)    {        data = d;        next = n;    }};struct Dnode {    int data;    Dnode* next;    Dnode* prev;};Dnode* change(node* head){    Dnode* Dhead = new Dnode;    Dnode* tmp = Dhead;    Dnode* p = NULL;    while (head)    {        Dhead->prev = p;        Dhead->data = head->data;        p = Dhead;        Dhead->next = new Dnode;        head = head->next;        Dhead = Dhead->next;    }    Dhead->next = tmp;    tmp->prev = Dhead;    return tmp;}


[解决办法]
C/C++ code
Dnode* change(node* head){    if(head==NULL){return NULL;}    Dnode* Dhead = new Dnode;    Dnode* q = NULL;    node *p;    Dhead->next = Dhead;    Dhead->prev = Dhead;    Dnode* last = Dhead;    p=head->next; 

读书人网 >C++

热点推荐