赞
踩
输入:一个链表,以及反转数
输出:反转后的链表,例如,如果链表有9个结点,反转数为3,结果为321654987;反转数为4,结果为432187659;反转数为1,结果不变
函数:
- void Reverselist(struct List *plist, int num){
- if(num == 1){
- return;
- }
- int revernum = plist->count/num;
- struct Node *phead = plist->pfront; //pfront是反转前的头结点
- struct Node *pheadthen;
- struct Node *prearnow;
- int flag = 1; //flag用来确定反转后链表的头
- while(revernum > 0){
- pheadthen = onereverse(phead, num - 1);
- if(flag == 1){
- plist->pfront = pheadthen;
- flag = 0;
- }else{
- prearnow->pnext = pheadthen;
- }
- prearnow = phead;
- phead = phead->pnext;
- revernum --;
- }
- return;
- }
里面的onereverse函数:
- struct Node* onereverse(struct Node *phead, int renum){
- struct Node *p1,*p2,*p3;
- p1 = phead;
- p2 = p1->pnext;
- p3 = p2->pnext;
- while(renum > 0){
- p2->pnext = p1;
- p1 = p2;
- p2 = p3;
- if(renum == 1){
- renum = 1;
- }else{
- p3 = p3->pnext;
- }
- renum --;
- }
- phead->pnext = p2;
- return p1;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。