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 }