zoukankan      html  css  js  c++  java
  • <算法编程> 奇偶链表相连

      1 #include<iostream>
      2 using namespace std;
      3 
      4 struct Node
      5 {
      6     int n;
      7     Node* pNext;
      8 };
      9 
     10 Node* AddNode(Node** pHead,Node* pNode)
     11 {
     12     if((*pHead) == NULL)
     13     {
     14         (*pHead) = pNode;
     15     }
     16     else 
     17     {
     18         Node* pEnd = (*pHead);
     19         while(pEnd->pNext != NULL)
     20         {
     21             pEnd = pEnd->pNext;
     22         }
     23         pEnd->pNext = pNode;
     24     }
     25 
     26     return *pHead;
     27 }
     28 
     29 void PrintList(Node* pHead)
     30 {
     31     while(pHead)
     32     {
     33         cout << pHead->n << " ";
     34         pHead = pHead->pNext;
     35     }
     36     cout << endl;
     37 }
     38 
     39 Node* pfun(Node** pHead)
     40 {
     41     Node* p2 = (*pHead)->pNext; //记录第二个元素 即偶的头 以便最后拼接
     42     Node* ptmp = *pHead;
     43     Node* ptmp_move = *pHead;
     44     while(ptmp_move->pNext->pNext) 
     45     {
     46         ptmp_move = ptmp_move->pNext;
     47         ptmp->pNext = ptmp->pNext->pNext;
     48         ptmp = ptmp_move;
     49     }
     50     ptmp_move->pNext = NULL; //记得赋空值
     51 
     52     ptmp = (*pHead);
     53     while(ptmp->pNext)
     54         ptmp = ptmp->pNext;
     55     ptmp->pNext = p2; //遍历找到奇的尾 然后把奇的尾指向偶的头
     56 
     57     return *pHead;
     58 }
     59 
     60 int main()
     61 {
     62     Node* pHead = NULL;
     63 
     64     Node* pNode1 = new Node;
     65     pNode1->n = 1;
     66     pNode1->pNext = NULL;
     67     AddNode(&pHead,pNode1);
     68     Node* pNode2 = new Node;
     69     pNode2->n = 2;
     70     pNode2->pNext = NULL;
     71     AddNode(&pHead,pNode2);
     72     Node* pNode3 = new Node;
     73     pNode3->n = 3;
     74     pNode3->pNext = NULL;
     75     AddNode(&pHead,pNode3);
     76     Node* pNode4 = new Node;
     77     pNode4->n = 4;
     78     pNode4->pNext = NULL;
     79     AddNode(&pHead,pNode4);
     80     Node* pNode5 = new Node;
     81     pNode5->n = 5;
     82     pNode5->pNext = NULL;
     83     AddNode(&pHead,pNode5);
     84     Node* pNode6 = new Node;
     85     pNode6->n = 6;
     86     pNode6->pNext = NULL;
     87     AddNode(&pHead,pNode6);
     88     Node* pNode7 = new Node;
     89     pNode7->n = 7;
     90     pNode7->pNext = NULL;
     91     AddNode(&pHead,pNode7);
     92     Node* pNode8 = new Node;
     93     pNode8->n = 8;
     94     pNode8->pNext = NULL;
     95     AddNode(&pHead,pNode8);
     96     
     97     PrintList(pHead);
     98     pHead = pfun(&pHead);
     99     PrintList(pHead);
    100 
    101     system("pause");
    102     return 0;
    103 }
  • 相关阅读:
    成功的两大法宝:自我管理与积累人脉
    CEO十五条法则 (是基于对CEO更加的关怀)
    百度李彦宏教你创业七大招!非常实用
    商业领袖摘下"帽子"才能炼成极致
    Alter index coalesce VS shrink space
    sort_area_size参数的一些表现
    Difference between parameter COMPATIBLE and OPTIMIZER_FEATURES_ENABLE
    Oracle常用的几个父栓
    Know more about RAC GES STATISTICS
    ORA07445 [SIGBUS] [Object specific hardware error]错误一例
  • 原文地址:https://www.cnblogs.com/Aaaaaalei0612/p/11218810.html
Copyright © 2011-2022 走看看