user-0 提问: 双向链表前后项交换是不是这样的过程?

回复:
user: user-0
content: //exchange   A B C D        pTmpD->CurItem->prev= pList->CurItem->next->prev;    //D          pTmpChg->CurItem->prev= pList->CurItem->prev;    //C        pTmpChg->CurItem->next= pList->CurItem->next;          pTmpA->CurItem->next = pList->CurItem->prev->prev->next;   //A         pList->CurItem->prev = pList->CurItem->prev->prev;        pList->CurItem->next = pList->CurItem->prev->next;         pList->CurItem->prev->prev = pTmpChg->CurItem->prev;          pList->CurItem->prev->next = pTmpChg->CurItem->next;           pList->CurItem->prev->prev->next= pTmpA->CurItem->next;     //<--A           pList->CurItem->next->prev = pTmpD->CurItem->prev;    //<--D         //exchange

user: user-1
content: 1<=>p<=>q<=>2 以交换pq为例说明：  p->prev->next = q;//1->q<=>2 q->prev = p->prev;//1<=>q<=>2 p->next = q->next;//1<=>q..p->2 q->next->prev = p;//1<=>q..p<=>2 q->next = p;//1<=>p->q<=>2 p->prev = q;//1<=>q<=>p<=>2  中间有些步骤可以调换，不做说明

user: user-0
content: //exchange        pTmpChg->CurItem->prev= pList->CurItem->prev;         pTmpChg->CurItem->next= pList->CurItem->next;          pList->CurItem->prev = pList->CurItem->prev->prev;        pList->CurItem->next = pList->CurItem->prev->next;         pList->CurItem->prev->prev = pTmpChg->CurItem->prev;          pList->CurItem->prev->next = pTmpChg->CurItem->next;          //exchange

user: user-2
content: 引用 3 楼 hgjyts65fg 的回复:A B C D,  B C交换 自身要改变两个指针（B两个，C两个） A指向B和D指向C也要改变（B C都不是头尾的情况）。 所以，不包括临时操作，最少要改变6个指针.++

user: user-3
content: 建议，自己写个小程序测试下！多练下手！

user: user-4
content: A B C D,  B C交换 自身要改变两个指针（B两个，C两个） A指向B和D指向C也要改变（B C都不是头尾的情况）。 所以，不包括临时操作，最少要改变6个指针.

user: user-0
content: 谢谢. 标点符号要点清楚， 不然看不清.

user: user-5
content: 既然是双向链表，那就意味着，你可以根据一个给出的节点，prev next两个成员就可以知道前节点和后节点 交换节点的话其实并不是换位置，只是改一下指针的指向。 prevA nodeA nextA A节点和它的前后节点 prevB nodeB nextB B节点和它的前后节点 交换可以这样实现 prevA的next成员指向改为nodeB nextA的prev成员指向改为nodeB,再将nodeB的prev next成员 指向改为prevA 和 nextA 这样一弄，就把nodeB放到了prevA 和 nextA之间了(“之间"只是一个形象的说法，实际内存地址是没有移动的，链表本来就不是一个顺序存储的结构) 然后只要把nodeA放到prevB和nextB之间就行，这个和上面同理。 因为是双向链表，所以不用考虑头尾节点的特殊情况。

user: user-6
content: lz 的模型还停留在数组上

user: user-7
content: 引用 楼主 huabinsir 的回复:双向链表前后项交换是不是这样的过程?  后项->临时交换, 前项->后项, 临时交换->前项  pTmpChg->CurItem = pList->CurItem;  //后项->临时交换 pList->CurItem->next = pList->CurItem->prev;  //前项->后项 pList->CurItem->prev = pTmpChg->CurItem;     //临时交换->前项  实在不习惯骚年变量命名的习惯，要是我的话就这样  typedef int ValueType; struct node{     ValueType value;     struct node* pre, *next; }; 至于你说的交换前后项，有两种实现方式： （1）纯指针操作 void swapWithBehind(struct node* p) {     struct node* q;     if(p == NULL || p == p->next) return; //connect p->pre with p->next     q = p->pre;     q->next = p->next;     p->next->pre = q; //connect p with p->next->next     q = p->next->next;     p->next = q;     q->pre = p; } （2）值操作(ValueType复制比较简单时还是比较好用的说) void swapWithBehind(struct node* p) {     ValueType tmp;     if(p == NULL || p == p->next) return;     tmp = p->value;     p->value = p->next->value;     p->next->value = tmp; }

user: user-0
content: //(B)前pList->CurItem->prev; (C)后pList->CurItem     //exchange   A B<->C D  pList->CurItem->prev->prev->next = pList->CurItem; //A->C B D  pList->CurItem->prev = pList->CurItem->prev->prev;  //A<->C B D  pList->CurItem->prev->next = pList->CurItem->next;  //A<->C B-> D  pList->CurItem->next->prev = pList->CurItem->prev;  //A<->C B<-> D  pList->CurItem->next = pList->CurItem->prev;    //A<->C->B<-> D  pList->CurItem->prev->prev =	 pList->CurItem;  //A<->C<->B<-> D   <---试过，这样不行

user: user-0
content: //(B)前pList->CurItem->prev; (C)后pList->CurItem           //exchange   A B<->C D       pList->CurItem->prev->prev->next = pTmp->CurItem; //A->C B D       pList->CurItem->prev = pList->CurItem->prev->prev;  //A<->C B D       pList->CurItem->prev->next = pList->CurItem->next;  //A<->C B-> D       pList->CurItem->next->prev = pList->CurItem->prev;  //A<->C B<-> D       pList->CurItem->next = pList->CurItem->prev;    //A<->C->B<-> D       pList->CurItem->prev->prev =	 pList->CurItem;  //A<->C<->B<-> D   ?

user: user-8
content: 引用 7 楼 my_live_123 的回复:1<=>p<=>q<=>2 以交换pq为例说明：  p->prev->next = q;//1->q<=>2 q->prev = p->prev;//1<=>q<=>2 p->next = q->next;//1<=>q..p->2 q->next->prev = p;//1<=>q..p<=>2 q->next = p;//1<=>p->q<=>2 p->prev = q;//1<=>q<=>p<=>2  中间有些步骤可以调换，不做说明 1<-->p<-->q<-->2 以交换pq为例说明： p->prev->next = q;           //1    ->q       p       2 q->prev            = p->prev;//1<-->q       p       2 p->next            = q->next;//1<-->q       p    ->2 q->next->prev = p;           //1<-->q       p<-->2 q->next            = p;           //1<-->q   ->p<-->2 p->prev            = q;           //1<-->q<-->p<-->2

user: user-0
content: //exchange   A B<->C D        pTmpD->CurItem = pList->CurItem->next;    //D         pTmpA->CurItem = pList->CurItem->prev->prev;   //A         pTmpChg->CurItem = pList->CurItem;  //C ->tmp        pList->CurItem = pList->CurItem->prev;  //B->C        pList->CurItem->prev = pTmpChg->CurItem;  //tmp->B           pTmpA->CurItem->next = pList->CurItem->prev;   //A下一个是B        pList->CurItem->prev->prev = pTmpA->CurItem;   //B上一个是A         pList->CurItem->prev->prev->next = pList->CurItem; //B下一个是C        pList->CurItem->prev = pList->CurItem->prev;        //C上一个是B        pList->CurItem->next = pTmpD->CurItem;      //C下一个是C        pTmpD->CurItem->prev = pList->CurItem;	 //D上一个是C        //exchange    这样应是OK 的