// 测试空链表 void Test4() { printf("=====Test4 starts:=====\n"); printf("expected result: NULL.\n"); ListNode* pNode = FindKthToTail(NULL, 100); PrintListNode(pNode); }
int main(void) { pNode pHead = (pNode)malloc(sizeof(Node)); pHead = CreatList(); printf("%d", FindKthToTail(pHead,3)); /*TraverseList(pHead);*/ return 0; }
void Test1() { 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); printf("Expected result: 4 \n"); ListNode* pNode = FindKthToTail(pNode1, 2); PrintListNode(pNode); DestroyList(pNode1); }
// 测试要找的结点是链表的头结点 void Test3() { printf("=====Test3 starts:=====\n"); 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); printf("expected result: 1.\n"); ListNode* pNode = FindKthToTail(pNode1, 5); PrintListNode(pNode); DestroyList(pNode1); }
int _tmain(int argc, _TCHAR* argv[]) { //print1ToMaxOfNDigits(10); int arr[16]; srand((unsigned int)time(NULL)); for (int i=0;i<16;i++) { arr[i] = rand()%100; std::cout << arr[i] << " "; } std::cout << std::endl; ReorderOddEven(arr,16,IsEven); for (int i=0;i<16;i++) { std::cout << arr[i] << " "; } LinkNode *pHead = new LinkNode; pHead->value = 0; pHead->next = NULL; for (int i=1;i<10000;i++) { LinkNode *node = new LinkNode; node->value = i; node->next = pHead->next; pHead->next = node; } std::cout <<std::endl<< FindKthToTail(pHead,4000)->value << std::endl; while (pHead->next!=NULL) { LinkNode *node = pHead->next; pHead->next = node->next; delete node; } return 0; }