天道酬勤,学无止境

逆序

单链表逆序操作

//逆序操作//算法1:从第二个节点开始,记录它的下一个节点,然后依次挪到第一个节点之前成为新表头int inverse_node(struct node pH){struct node p = pH; //头结点struct node pPrev = NULL; //记录前一个节点struct node pBack = NULL; //记录下一个节点地址struct node *pFirstNode = p->pNext; //记录第一个节点 //节点只有1个或者无有效节点时,返回原来的链表,不用进行逆序操作 if((NULL == pFirstNode) | (NULL == pFirstNode->pNext)) { return 0; } //节点数两个或以上时 else { p = p->pNext; //节点1 pBack = p->pNext; //节点2 pPrev = p; //节点1 p = pBack; //节点2 while(NULL != p->pNext) { pBack = p->pNext; //节点3 pH->pNext = p; p->pNext = pPrev; pPrev = p; p = pBack; } } pH->pNext = p; p->pNext = pPrev; pFirstNode->pNext = NULL; return

2021-03-27 08:26:07    分类:博客    单链表   逆序   c