首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-05-22 
单链表转换为循环双链表 哪里不对C/C++ codestruct node{int datanode* nextnode(int d,node* n){data

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

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; 

热点排行