码迷,mamicode.com
首页 > 其他好文 > 详细

简单版 无库函数

时间:2016-04-13 21:10:12      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:小代码

 #include"wz.h"
struct ComplexNode{ int value; ComplexNode* pNext; ComplexNode* pSibling;};      
  void deleteList(ComplexNode* pHead)
 { while(pHead!=NULL)
    { ComplexNode* pNext = pHead->pNext; delete pHead; pHead = pNext; }
  }

void Con(ComplexNode* pHead)  
{  
    ComplexNode* pNode = pHead;  
    while(pNode != NULL)              
    {  
        ComplexNode *pCloned = pNode->pNext;  
        if(pNode->pSibling != NULL)  
        {  
            pCloned->pSibling = pNode->pSibling->pNext;   
        }  
        pNode = pCloned->pNext;  
    }  
}  
void print(ComplexNode* pHead)
{ ComplexNode* p=pHead;
  while(p) { cout<<p->value<<" ";  p=p->pNext;}
  cout<<endl;
}
 ComplexNode* myClone2(ComplexNode* pHead)
 {  
   ComplexNode*p= new ComplexNode;
   ComplexNode*q= NULL;
   ComplexNode*newhead= NULL;
    if(pHead)
  {   p=pHead;
    while(p->pNext)
    {
     ComplexNode*add= new ComplexNode;
     add->value=p->value;
     add->pSibling=NULL;add->pNext=p->pNext;
     p->pNext=add;
     p=p->pNext->pNext;
   }
     ComplexNode*add= new ComplexNode;
     add->value=p->value;
     add->pSibling=NULL;add->pNext=p->pNext;
     p->pNext=add;
     p=pHead;
     q=p->pNext;
     newhead=q;
  
     Con(p);
     q=newhead;
     while(q->pNext)
    {
    p->pNext=q->pNext;p=p->pNext;
    q->pNext=p->pNext;q=q->pNext;
   }
     p->pNext=NULL;                      
  }
return newhead;
}
void t2()
{   ComplexNode* p1 = new ComplexNode; ComplexNode* p2 = new ComplexNode;
    ComplexNode* p3 = new ComplexNode; ComplexNode* p4 = new ComplexNode;
    p1->pNext = p2; p2->pNext = p3; p3->pNext = p4; p4->pNext = NULL;
    p1->value = 1 ; p2->value = 2; p3->value = 3 ; p4->value = 4;
    p1->pSibling = p3;p2->pSibling = p4; p3->pSibling = NULL; p4->pSibling = NULL;
    print(p1);
    ComplexNode* newHead = myClone2(p1);
 cout<<"old link:"<<endl;   print(p1);
 cout<<"new link:"<<endl;    print(newHead);
    cout<<(newHead->pSibling)->value<<endl; //3
    deleteList(newHead);
    deleteList(p1);   
}
int main()
{
t2();
return 0;
}


简单版 无库函数

标签:小代码

原文地址:http://wzsts.blog.51cto.com/10251779/1763368

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!