这里有一点主意的,操作接口传递的是头指针的指针,因为逆置后,链表头会发生改变,所以需要传递双重指针,以便接收新的链表头地址
代表实现
int reverseList(st_dataNode** phead){
if(NULL == phead || NULL == *phead){
printf("%s: param error\n",__func__);
return PARAM_ERR;
}
st_dataNode *head = *phead;
st_dataNode *prev = NULL, *p = NULL, *next;
p = head;
while(p != NULL){
next = p->next;
p->next = prev;
prev = p;
p = next;
}
/*原来的最后一个节点,是现在的头结点,在prev里面*/
*phead = prev;
return SUCCESS;
}
void testReverseList(void){
reverseList(&ghead);
dumpList(ghead);
reverseList(&ghead);
dumpList(ghead);
return;
}
调试编译
gcc listMain.c list.c -o a.exe -DDEBUG
调试输出
========= Dump List 0xa8b010 ===========
22 32 19 53 0 47 29 116 4 6
===================================
List length = 10
node: 0xa8b130, data = 6
Can not found num 119
Can not found pos -1 node
find pos 0 node: 0xa8b010, data = 22
find pos 5 node: 0xa8b0b0, data = 47
超过链表长度了!
Can not found pos 12 node
========= Dump List 0xa8b150 ===========
70 22 32 19 53 0 47 29 116 4 6
===================================
========= Dump List 0xa8b150 ===========
70 22 32 19 53 31 0 47 29 116 4 6
===================================
========= Dump List 0xa8b150 ===========
70 22 32 19 53 31 0 47 29 116 4 6 66
===================================
Removed node 0xa8b190: data 66
========= Dump List 0xa8b150 ===========
70 22 32 19 53 31 0 47 29 116 4 6
===================================
Removed node 0xa8b170: data 31
========= Dump List 0xa8b150 ===========
70 22 32 19 53 0 47 29 116 4 6
===================================
Removed node 0xa8b150: data 70
========= Dump List 0xa8b010 ===========
22 32 19 53 0 47 29 116 4 6
===================================
========= Dump List 0xa8b130 ===========
6 4 116 29 47 0 53 19 32 22
===================================
========= Dump List 0xa8b010 ===========
22 32 19 53 0 47 29 116 4 6
===================================