/* 删除节点:删除pos节点 当pos==0时,删除头结点head,让其后的节点成为头结点 由于head节点有可能改变。因此我们这里要返回一个指针 */ Node* DeleteNode(Node* head, int pos) { assert(head != nullptr); Node* item = head; if (pos == 0) // 删除头结点需要特殊处理 { head = head->next; //改变了head指针 delete item; item = nullptr; return head; } // 先找到pos之前的pos-1元素指针 Node* p = SearchPosition(head, pos-1); if (p != nullptr) // 删除尾节点不需要特殊处理 { item = p->next; p->next = item->next; delete item; item = nullptr; } return head; }
void DebugAnalysisTest(S_BOARD *pos, S_SEARCHINFO *info) { FILE *file; file = fopen("lct2.epd","r"); char lineIn [1024]; info->depth = MAXDEPTH; info->timeset = TRUE; int time = 1140000; if(file == NULL) { printf("File Not Found\n"); return; } else { while(fgets (lineIn , 1024 , file) != NULL) { info->starttime = GetTimeMs(); info->stoptime = info->starttime + time; ClearHashTable(pos->HashTable); ParseFen(lineIn, pos); printf("\n%s\n",lineIn); printf("time:%d start:%d stop:%d depth:%d timeset:%d\n", time,info->starttime,info->stoptime,info->depth,info->timeset); SearchPosition(pos, info); memset(&lineIn[0], 0, sizeof(lineIn)); } } }
/* 插入节点 在节点pos之后才插入元素 当pos=0时,让其在head之后插入节点 */ void InsertNode(Node* head, int pos, int data) { assert(head != nullptr); Node* item = new Node(); item->data = data; // 这里能处理当pos==0的情况,pos==0时返回head,在head之后插入item即可 // 这里也能处理在尾节点插入的情况,当在尾节点时,p->next == nullptr. 让item->next = p->next = nullptr即可 Node* p = SearchPosition(head, pos); if (p != nullptr) { item->next = p->next; p->next = item; } }
void TestSingleLink() { Node* head = CreateSingleLink(0); PrintSingleLink(head); int len = GetSingleLinkLength(head); cout << "the list's length is: " << len << endl; Node* psearch = SearchPosition(head, 0); if (psearch) { cout << "The pos 0 node in the list, the data is: " << psearch->data << endl; } psearch = SearchPosition(head, 1); if (psearch) { cout << "The pos 1 node in the list, the data is: " << psearch->data << endl; } psearch = SearchData(head, 1); if (psearch) { cout << "The data 1 exists in the list, the data is: " << psearch->data << endl; } InsertNode(head, 0, 1); cout << "after InsertNode 1, the list is: " << endl; PrintSingleLink(head); head = DeleteNode(head, 0); cout << "after DeleteNode 0, the list is: " << endl; PrintSingleLink(head); cout << endl; // avoid memory leak DestroySingleLink(head); head = CreateSingleLink(10); PrintSingleLink(head); len = GetSingleLinkLength(head); cout << "the list's length is: " << len << endl; psearch = SearchPosition(head, 10); if (psearch) { cout << "The pos 10 node in the list, the data is: " << psearch->data << endl; } psearch = SearchData(head, 9); if (psearch) { cout << "The data 9 exists in the list, the data is: " << psearch->data << endl; } InsertNode(head, 5, 55); cout << "after InsertNode 55, the list is: " << endl; PrintSingleLink(head); head = DeleteNode(head, 11); cout << "after DeleteNode 11, the list is: " << endl; PrintSingleLink(head); head = DeleteNode(head, 6); cout << "after DeleteNode 6, the list is: " << endl; PrintSingleLink(head); head = DeleteNode(head, 0); cout << "after DeleteNode 0, the list is: " << endl; PrintSingleLink(head); cout << endl; // avoid memory leak DestroySingleLink(head); }