剑指Offer-第5章 优化时间和空间效率
代码如下:
#if 0 bool g_bInputInvalid = false; bool CheckInvalidArray(int* numbers, int length) { g_bInputInvalid = false; if(numbers == NULL && length <= 0) g_bInputInvalid = true; return g_bInputInvalid; } bool CheckMoreThanHalf(int* numbers, int length, int number) { int times = 0; for(int i = 0; i < length; ++i) { if(numbers[i] == number) times++; } bool isMoreThanHalf = true; if(times * 2 <= length) { g_bInputInvalid = true; isMoreThanHalf = false; } return isMoreThanHalf; } // ====================方法1==================== int MoreThanHalfNum_Solution1(int* numbers, int length) { if(CheckInvalidArray(numbers,length)) return 0; int mid=length>>1; int low=0,high=length-1; int indx=Partition(numbers,low,high); while(indx!=mid){ if(indx>mid) { high=indx-1; indx=Partition(numbers,low,high); } else { low=indx+1; indx=Partition(numbers,low,high); } } int r=numbers[indx]; if(!CheckMoreThanHalf(numbers,length,r)) return 0; return r; } // ====================方法2==================== int MoreThanHalfNum_Solution2(int* numbers, int length){ if(CheckInvalidArray(numbers,length)) return 0; int num,cnt; num=numbers[0]; cnt=1; for(int i=1;i<length;i++){ if(cnt==0) num=numbers[i],cnt=1; else if(num==numbers[i]) cnt++; else cnt--; } if(!CheckMoreThanHalf(numbers,length,num)) return 0; return num; } // ====================测试代码==================== void Test(char* testName, int* numbers, int length, int expectedValue, bool expectedFlag) { if(testName != NULL) printf("%s begins: \n", testName); int* copy = new int[length]; for(int i = 0; i < length; ++i) copy[i] = numbers[i]; printf("Test for solution1: "); int result = MoreThanHalfNum_Solution1(numbers, length); if(result == expectedValue && g_bInputInvalid == expectedFlag) printf("Passed.\n"); else printf("Failed.\n"); printf("Test for solution2: "); result = MoreThanHalfNum_Solution2(copy, length); if(result == expectedValue && g_bInputInvalid == expectedFlag) printf("Passed.\n"); else printf("Failed.\n"); delete[] copy; } // 存在出现次数超过数组长度一半的数字 void Test1() { int numbers[] = {1, 2, 3, 2, 2, 2, 5, 4, 2}; Test("Test1", numbers, sizeof(numbers) / sizeof(int), 2, false); } // 不存在出现次数超过数组长度一半的数字 void Test2() { int numbers[] = {1, 2, 3, 2, 4, 2, 5, 2, 3}; Test("Test2", numbers, sizeof(numbers) / sizeof(int), 0, true); } // 出现次数超过数组长度一半的数字都出现在数组的前半部分 void Test3() { int numbers[] = {2, 2, 2, 2, 2, 1, 3, 4, 5}; Test("Test3", numbers, sizeof(numbers) / sizeof(int), 2, false); } // 出现次数超过数组长度一半的数字都出现在数组的后半部分 void Test4() { int numbers[] = {1, 3, 4, 5, 2, 2, 2, 2, 2}; Test("Test4", numbers, sizeof(numbers) / sizeof(int), 2, false); } // 输入空指针 void Test5() { int numbers[] = {1}; Test("Test5", numbers, 1, 1, false); } // 输入空指针 void Test6() { Test("Test6", NULL, 0, 0, true); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } #endif
代码如下:
#if 0 // ====================方法1==================== void GetLeastNumbers_Solution1(int* input, int n, int* output, int k){ if(input==0||output==0||n<=0||k<=0||k>n) return; int low=0,high=n-1; int indx=Partition(input,low,high); while(indx!=k-1){ if(indx>k-1) { high=indx-1; indx=Partition(input,low,high); } else{ low=indx+1; indx=Partition(input,low,high); } } for(int i=0;i<k;i++) output[i]=input[i]; } // ====================方法2==================== typedef multiset<int, greater<int> > intSet; typedef multiset<int, greater<int> >::iterator setIterator; void GetLeastNumbers_Solution2(const vector<int>& data, intSet& leastNumbers, int k) { leastNumbers.clear(); if(k < 1 || data.size() < k) return; vector<int>::const_iterator iter = data.begin(); for(; iter != data.end(); ++ iter) { if((leastNumbers.size()) < k) leastNumbers.insert(*iter); else { setIterator iterGreatest = leastNumbers.begin(); if(*iter < *(leastNumbers.begin())) { leastNumbers.erase(iterGreatest); leastNumbers.insert(*iter); } } } } void GetLeastNumbers_Solution3(int* input, int n, int* output, int k){ if(input==0||output==0||n<=0||k<0) return; for(int i=0;i<n;i++){ if(i<k){ output[i]=input[i]; } else{ make_heap(output,output+k,greater<int>()); if(input[i]<output[0]) output[0]=input[i]; } } } // ====================测试代码==================== void Test(char* testName, int* data, int n, int* expectedResult, int k) { if(testName != NULL) printf("%s begins: \n", testName); vector<int> vectorData; for(int i = 0; i < n; ++ i) vectorData.push_back(data[i]); if(expectedResult == NULL) printf("The input is invalid, we don't expect any result.\n"); else { printf("Expected result: \n"); for(int i = 0; i < k; ++ i) printf("%d\t", expectedResult[i]); printf("\n"); } printf("Result for solution1:\n"); int* output = new int[k]; GetLeastNumbers_Solution1(data, n, output, k); if(expectedResult != NULL) { for(int i = 0; i < k; ++ i) printf("%d\t", output[i]); printf("\n"); } printf("Result for solution2:\n"); intSet leastNumbers; GetLeastNumbers_Solution2(vectorData, leastNumbers, k); printf("The actual output numbers are:\n"); for(setIterator iter = leastNumbers.begin(); iter != leastNumbers.end(); ++iter) printf("%d\t", *iter); printf("\n"); printf("Result for solution3:\n"); GetLeastNumbers_Solution1(data, n, output, k); if(expectedResult != NULL) { for(int i = 0; i < k; ++ i) printf("%d\t", output[i]); printf("\n"); } printf("\n\n"); delete[] output; } // k小于数组的长度 void Test1() { int data[] = {4, 5, 1, 6, 2, 7, 3, 8}; int expected[] = {1, 2, 3, 4}; Test("Test1", data, sizeof(data) / sizeof(int), expected, sizeof(expected) / sizeof(int)); } // k等于数组的长度 void Test2() { int data[] = {4, 5, 1, 6, 2, 7, 3, 8}; int expected[] = {1, 2, 3, 4, 5, 6, 7, 8}; Test("Test2", data, sizeof(data) / sizeof(int), expected, sizeof(expected) / sizeof(int)); } // k大于数组的长度 void Test3() { int data[] = {4, 5, 1, 6, 2, 7, 3, 8}; int* expected = NULL; Test("Test3", data, sizeof(data) / sizeof(int), expected, 10); } // k等于1 void Test4() { int data[] = {4, 5, 1, 6, 2, 7, 3, 8}; int expected[] = {1}; Test("Test4", data, sizeof(data) / sizeof(int), expected, sizeof(expected) / sizeof(int)); } // k等于0 void Test5() { int data[] = {4, 5, 1, 6, 2, 7, 3, 8}; int* expected = NULL; Test("Test5", data, sizeof(data) / sizeof(int), expected, 0); } // 数组中有相同的数字 void Test6() { int data[] = {4, 5, 1, 6, 2, 7, 2, 8}; int expected[] = {1, 2}; Test("Test6", data, sizeof(data) / sizeof(int), expected, sizeof(expected) / sizeof(int)); } // 输入空指针 void Test7() { int* expected = NULL; Test("Test7", NULL, NULL, expected, 0); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); return 0; } #endif
#if 0 bool g_InvalidInput = false; int FindGreatestSumOfSubArray(int *pData, int nLength) { if((pData == NULL) || (nLength <= 0)) { g_InvalidInput = true; return 0; } g_InvalidInput = false; int nCurSum = 0; int nGreatestSum = 0x80000000; for(int i = 0; i < nLength; ++i) { if(nCurSum <= 0) nCurSum = pData[i]; else nCurSum += pData[i]; if(nCurSum > nGreatestSum) nGreatestSum = nCurSum; } return nGreatestSum; } // ====================测试代码==================== void Test(char* testName, int* pData, int nLength, int expected, bool expectedFlag) { if(testName != NULL) printf("%s begins: \n", testName); int result = FindGreatestSumOfSubArray(pData, nLength); if(result == expected && expectedFlag == g_InvalidInput) printf("Passed.\n"); else printf("Failed.\n"); } // 1, -2, 3, 10, -4, 7, 2, -5 void Test1() { int data[] = {1, -2, 3, 10, -4, 7, 2, -5}; Test("Test1", data, sizeof(data) / sizeof(int), 18, false); } // 所有数字都是负数 // -2, -8, -1, -5, -9 void Test2() { int data[] = {-2, -8, -1, -5, -9}; Test("Test2", data, sizeof(data) / sizeof(int), -1, false); } // 所有数字都是正数 // 2, 8, 1, 5, 9 void Test3() { int data[] = {2, 8, 1, 5, 9}; Test("Test3", data, sizeof(data) / sizeof(int), 25, false); } // 无效输入 void Test4() { Test("Test4", NULL, 0, 0, true); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); return 0; } #endif
#if 0 // ====================方法一==================== int NumberOf1(unsigned int n); int NumberOf1Between1AndN_Solution1(unsigned int n) { int number = 0; for(unsigned int i = 1; i <= n; ++ i) number += NumberOf1(i); return number; } int NumberOf1(unsigned int n) { int number = 0; while(n) { if(n % 10 == 1) number ++; n = n / 10; } return number; } // ====================方法二==================== int NumberOf1(const char* strN); int PowerBase10(unsigned int n); int NumberOf1Between1AndN_Solution2(int n) { if(n<=0) return 0; char str[50]; sprintf(str,"%d",n); return NumberOf1(str); } int NumberOf1(const char* strN){ if(strN==0||*strN<'0'||*strN>'9'||*strN=='\0') return 0; int first=*strN-'0'; int len=strlen(strN); if(len==1&&first==0) return 0; if(len==1&&first>0) return 1; int numFirstDigit=0; if(first>1) numFirstDigit=PowerBase10(len-1); else if(first==1) numFirstDigit=atoi(strN+1)+1; int numOtherDigits=first*(len-1)*PowerBase10(len-2); int numRecursive=NumberOf1(strN+1); return numFirstDigit+numOtherDigits+numRecursive; } int PowerBase10(unsigned int n){ int p=1; for(unsigned int i=0;i<n;i++) p*=10; return p; } int NumberOf1Between1AndN_Solution3(int n) { if(n<=0) return 0; int iFactor=1; int iCnt=0; int iLower,iCurrent,iHigher; while(n/iFactor!=0){ iLower=n-(n/iFactor)*iFactor, iCurrent=(n/iFactor)%10, iHigher=n/(iFactor*10); switch(iCurrent){ case 0: iCnt+=iHigher*iFactor; break; case 1: iCnt+=iHigher*iFactor+iLower+1; break; default: iCnt+=(iHigher+1)*iFactor; break; } iFactor*=10; } return iCnt; } // ====================测试代码==================== void Test(char* testName, int n, int expected) { if(testName != NULL) printf("%s begins: \n", testName); if(NumberOf1Between1AndN_Solution1(n) == expected) printf("Solution1 passed.\n"); else printf("Solution1 failed.\n"); if(NumberOf1Between1AndN_Solution2(n) == expected) printf("Solution2 passed.\n"); else printf("Solution2 failed.\n"); if(NumberOf1Between1AndN_Solution3(n) == expected) printf("Solution3 passed.\n"); else printf("Solution3 failed.\n"); printf("\n"); } void Test() { Test("Test1", 1, 1); Test("Test2", 5, 1); Test("Test3", 10, 2); Test("Test4", 55, 16); Test("Test5", 99, 20); Test("Test6", 10000, 4001); Test("Test7", 21345, 18821); Test("Test8", 0, 0); } int main(int argc, char* argv[]) { Test(); return 0; } #endif
代码如下:
#if 0 int compare(const void* strNumber1, const void* strNumber2); // int型整数用十进制表示最多只有10位 const int g_MaxNumberLength = 10; char* g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1]; char* g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1]; void PrintMinNumber(int* numbers, int length) { if(numbers==0||length<=0) return ; char** strNumbers=new char*[length]; for(int i=0;i<length;i++){ strNumbers[i]=new char[g_MaxNumberLength+1]; sprintf(strNumbers[i],"%d",numbers[i]); } qsort(strNumbers,length,sizeof(char*),compare); for(int i=0;i<length;i++) printf("%s",strNumbers[i]); printf("\n"); for(int i=0;i<length;i++) delete[] strNumbers[i]; delete[] strNumbers; } // 如果[strNumber1][strNumber2] > [strNumber2][strNumber1], 返回值大于0 // 如果[strNumber1][strNumber2] = [strNumber2][strNumber1], 返回值等于0 // 如果[strNumber1][strNumber2] < [strNumber2][strNumber1], 返回值小于0 int compare(const void* strNumber1, const void* strNumber2) { strcpy(g_StrCombine1,*(char**)strNumber1); strcat(g_StrCombine1,*(char**)strNumber2); strcpy(g_StrCombine2,*(char**)strNumber2); strcat(g_StrCombine2,*(char**)strNumber1); return strcmp(g_StrCombine1,g_StrCombine2); } // ====================测试代码==================== void Test(char* testName, int* numbers, int length, char* expectedResult) { if(testName != NULL) printf("%s begins:\n", testName); if(expectedResult != NULL) printf("Expected result is: \t%s\n", expectedResult); printf("Actual result is: \t"); PrintMinNumber(numbers, length); printf("\n"); } void Test1() { int numbers[] = {3, 5, 1, 4, 2}; Test("Test1", numbers, sizeof(numbers)/sizeof(int), "12345"); } void Test2() { int numbers[] = {3, 32, 321}; Test("Test2", numbers, sizeof(numbers)/sizeof(int), "321323"); } void Test3() { int numbers[] = {3, 323, 32123}; Test("Test3", numbers, sizeof(numbers)/sizeof(int), "321233233"); } void Test4() { int numbers[] = {1, 11, 111}; Test("Test4", numbers, sizeof(numbers)/sizeof(int), "111111"); } // 数组中只有一个数字 void Test5() { int numbers[] = {321}; Test("Test5", numbers, sizeof(numbers)/sizeof(int), "321"); } void Test6() { Test("Test6", NULL, 0, "Don't print anything."); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } #endif
代码如下:
#if 0 // ====================算法1的代码==================== bool IsUgly(int number) { while(number%2==0) number/=2; while(number%3==0) number/=3; while(number%5==0) number/=5; return number==1; } int GetUglyNumber_Solution1(int index) { if(index<=0) return 0; int i=0; int cnt=0; while(cnt<index){ i++; if(IsUgly(i)){ cnt++; printf("%d\n",cnt); } } return i; } int Min(int a,int b,int c){ int m=a<b?a:b; m=m<c?m:c; return m; } int GetUglyNumber_Solution2(int index) { if(index<=0) return 0; int *UglyNumber=new int[index]; UglyNumber[0]=1; int cnt=1; int *p2=UglyNumber, *p3=UglyNumber, *p5=UglyNumber; while(cnt<index){ int m=Min(*p2*2,*p3*3,*p5*5); UglyNumber[cnt++]=m; while(*p2*2<=m) p2++; while(*p3*3<=m) p3++; while(*p5*5<=m) p5++; } int ugly=UglyNumber[cnt-1]; delete[] UglyNumber; return ugly; } // ====================测试代码==================== void Test(int index, int expected) { if(GetUglyNumber_Solution1(index) == expected) printf("solution1 passed\n"); else printf("solution1 failed\n"); if(GetUglyNumber_Solution2(index) == expected) printf("solution2 passed\n"); else printf("solution2 failed\n"); } int main(int argc, char* argv[]) { Test(1, 1); Test(2, 2); Test(3, 3); Test(4, 4); Test(5, 5); Test(6, 6); Test(7, 8); Test(8, 9); Test(9, 10); Test(10, 12); Test(11, 15); Test(1500, 859963392); Test(0, 0); return 0; } #endif
代码如下:
#if 0 char FirstNotRepeatingChar(char* pString) { if(pString == NULL) return '\0'; const int tableSize = 256; unsigned int hashTable[tableSize]; for(unsigned int i = 0; i<tableSize; ++ i) hashTable[i] = 0; char* pHashKey = pString; while(*(pHashKey) != '\0') hashTable[*(pHashKey++)] ++; pHashKey = pString; while(*pHashKey != '\0') { if(hashTable[*pHashKey] == 1) return *pHashKey; pHashKey++; } return '\0'; } // ====================测试代码==================== void Test(char* pString, char expected) { if(FirstNotRepeatingChar(pString) == expected) printf("Test passed.\n"); else printf("Test failed.\n"); } int main(int argc, char* argv[]) { // 常规输入测试,存在只出现一次的字符 Test("google", 'l'); // 常规输入测试,不存在只出现一次的字符 Test("aabccdbd", '\0'); // 常规输入测试,所有字符都只出现一次 Test("abcdefg", 'a'); // 鲁棒性测试,输入NULL Test(NULL, '\0'); return 0; } #endif
代码如下:
#if 0 int InversePairsCore(int* data, int* cp, int start, int end); int InversePairs(int* data, int length) { if(data==0||length<=0) return 0; int *cp=new int [length]; for(int i=0;i<length;i++) cp[i]=data[i]; int cnt=InversePairsCore(data,cp,0,length-1); delete[] cp; return cnt; } int InversePairsCore(int* data, int* cp, int start, int end){ if(start==end){ cp[start]=data[start]; return 0; } int mid=(end-start)/2; int left=InversePairsCore(cp,data,start,start+mid); int right=InversePairsCore(cp,data,start+mid+1,end); int indx=end,i=start+mid,j=end; int cnt=0; while(i>=start&&j>=start+mid+1){ if(data[i]>data[j]){ cp[indx--]=data[i--]; cnt+=j-start-mid; } else{ cp[indx--]=data[j--]; } } for(;i>=start;i--) cp[indx--]=data[i--]; for(;j>=start+mid+1;j--) cp[indx--]=data[j--];; return left+right+cnt; } // ====================测试代码==================== void Test(char* testName, int* data, int length, int expected) { if(testName != NULL) printf("%s begins: ", testName); if(InversePairs(data, length) == expected) printf("Passed.\n"); else printf("Failed.\n"); } void Test1() { int data[] = {1, 2, 3, 4, 7, 6, 5}; int expected = 3; Test("Test1", data, sizeof(data) / sizeof(int), expected); } // 递减排序数组 void Test2() { int data[] = {6, 5, 4, 3, 2, 1}; int expected = 15; Test("Test2", data, sizeof(data) / sizeof(int), expected); } // 递增排序数组 void Test3() { int data[] = {1, 2, 3, 4, 5, 6}; int expected = 0; Test("Test3", data, sizeof(data) / sizeof(int), expected); } // 数组中只有一个数字 void Test4() { int data[] = {1}; int expected = 0; Test("Test4", data, sizeof(data) / sizeof(int), expected); } // 数组中只有两个数字,递增排序 void Test5() { int data[] = {1, 2}; int expected = 0; Test("Test5", data, sizeof(data) / sizeof(int), expected); } // 数组中只有两个数字,递减排序 void Test6() { int data[] = {2, 1}; int expected = 1; Test("Test6", data, sizeof(data) / sizeof(int), expected); } // 数组中有相等的数字 void Test7() { int data[] = {1, 2, 1, 2, 1}; int expected = 3; Test("Test7", data, sizeof(data) / sizeof(int), expected); } void Test8() { int expected = 0; Test("Test8", NULL, 0, expected); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); return 0; } #endif
#if 0 unsigned int GetListLength(ListNode* pHead); ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) { if(pHead1==0||pHead2==0) return 0; int len1=GetListLength(pHead1), len2=GetListLength(pHead2); ListNode* p,*q; int n; if(len1>len2) p=pHead1,q=pHead2,n=len1-len2; else p=pHead2,q=pHead1,n=len2-len1; while(n-->0) p=p->m_pNext; while(p!=0&&q!=0&&p!=q){ p=p->m_pNext; q=q->m_pNext; } return p; } unsigned int GetListLength(ListNode* pHead) { int n=0; ListNode* p=pHead; while(p!=0) { n++; p=p->m_pNext; } return n; } // ====================测试代码==================== void DestroyNode(ListNode* pNode); void Test(char* testName, ListNode* pHead1, ListNode* pHead2, ListNode* pExpected) { if(testName != NULL) printf("%s begins: ", testName); ListNode* pResult = FindFirstCommonNode(pHead1, pHead2); if(pResult == pExpected) printf("Passed.\n"); else printf("Failed.\n"); } // 第一个公共结点在链表中间 // 1 - 2 - 3 \ // 6 - 7 // 4 - 5 / void Test1() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ListNode* pNode6 = CreateListNode(6); ListNode* pNode7 = CreateListNode(7); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode6); ConnectListNodes(pNode4, pNode5); ConnectListNodes(pNode5, pNode6); ConnectListNodes(pNode6, pNode7); Test("Test1", pNode1, pNode4, pNode6); DestroyNode(pNode1); DestroyNode(pNode2); DestroyNode(pNode3); DestroyNode(pNode4); DestroyNode(pNode5); DestroyNode(pNode6); DestroyNode(pNode7); } // 没有公共结点 // 1 - 2 - 3 - 4 // // 5 - 6 - 7 void Test2() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ListNode* pNode6 = CreateListNode(6); ListNode* pNode7 = CreateListNode(7); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode5, pNode6); ConnectListNodes(pNode6, pNode7); Test("Test2", pNode1, pNode5, NULL); DestroyList(pNode1); DestroyList(pNode5); } // 公共结点是最后一个结点 // 1 - 2 - 3 - 4 \ // 7 // 5 - 6 / void Test3() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ListNode* pNode6 = CreateListNode(6); ListNode* pNode7 = CreateListNode(7); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode7); ConnectListNodes(pNode5, pNode6); ConnectListNodes(pNode6, pNode7); Test("Test3", pNode1, pNode5, pNode7); DestroyNode(pNode1); DestroyNode(pNode2); DestroyNode(pNode3); DestroyNode(pNode4); DestroyNode(pNode5); DestroyNode(pNode6); DestroyNode(pNode7); } // 公共结点是第一个结点 // 1 - 2 - 3 - 4 - 5 // 两个链表完全重合 void Test4() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode5); Test("Test4", pNode1, pNode1, pNode1); DestroyList(pNode1); } // 输入的两个链表有一个空链表 void Test5() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode2 = CreateListNode(2); ListNode* pNode3 = CreateListNode(3); ListNode* pNode4 = CreateListNode(4); ListNode* pNode5 = CreateListNode(5); ConnectListNodes(pNode1, pNode2); ConnectListNodes(pNode2, pNode3); ConnectListNodes(pNode3, pNode4); ConnectListNodes(pNode4, pNode5); Test("Test5", NULL, pNode1, NULL); DestroyList(pNode1); } // 输入的两个链表有一个空链表 void Test6() { Test("Test6", NULL, NULL, NULL); } void DestroyNode(ListNode* pNode) { delete pNode; pNode = NULL; } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); return 0; } #endif