代码实现
st_doubNode * findDoubListPos(st_doubNode * head, int pos){
if(NULL == head || pos < 0){
return NULL;
}
if(0 == pos) {
return head;
}
st_doubNode * p = NULL;
st_doubNode * q = NULL;
int len = getDoubListLen(head);
if(pos > len) {
printf("超过链表长度了!\n");
return NULL;
}
p = head;
while(p != NULL){
if(pos-- == 0){
q = p;
break;
}
p = p->next;
}
return q;
}
void testFindDoubListPos(void){
st_doubNode * rst = NULL;
rst = findDoubListPos(gDoubHead, -1);
if(NULL != rst){
printf("find pos -1 node: %p, data = %d\n", rst, rst->data);
} else {
printf("Can not found pos -1 node \n");
}
rst = findDoubListPos(gDoubHead, 0);
if(NULL != rst){
printf("find pos 0 node: %p, data = %d\n", rst, rst->data);
} else {
printf("Can not found pos 0 node \n");
}
rst = findDoubListPos(gDoubHead, 5);
if(NULL != rst){
printf("find pos 5 node: %p, data = %d\n", rst, rst->data);
} else {
printf("Can not found pos 5 node \n");
}
rst = findDoubListPos(gDoubHead, 12);
if(NULL != rst){
printf("find pos 12 node: %p, data = %d\n", rst, rst->data);
} else {
printf("Can not found pos 12 node \n");
}
return;
}
调试编译
gcc doublist.c listMain.c -DDEBUG -o a.exe
调试输出
************ testCreateDoubList ************
========= Dump Double List 0x13f4010 ===========
22 32 19 53 0 47 29 116 4 6
===================================
************ testCreateDoubList ************
len = 10
************ testFindDoubListPos ************
Can not found pos -1 node
find pos 0 node: 0x13f4010, data = 22
find pos 5 node: 0x13f40b0, data = 47
超过链表长度了!
Can not found pos 12 node