本文主要是介绍在结点类型为DLink Node 的双链表中将p所指结点(非尾结点)与其后继结点交换的操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在结点类型为DLink Node 的双链表中将p所指结点(非尾结点)与其后继结点交换的操作
C语言题解
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>typedef struct Node{int data;struct Node *next,*prior;
}DNode,*DLinkList;void InitList(DLinkList L){L = (DLinkList)malloc(sizeof(DNode));L->next = NULL;L->prior = NULL;
}void InsertListByHead(DLinkList L,int x){DLinkList s = (DLinkList)malloc(sizeof(DNode));s->data = x;s->next = L->next;if(L->next!=NULL){L->next->prior = s;s->prior = L;L->next = s;}else{s->prior = L;L->next = s;}
}void ChangeNode(DLinkList p){DLinkList q = p->next;if(q->next==NULL){p->next = q->next;p->prior->next = q;q->prior = p->prior;p->prior = q;q->next = p;}else{p->next = q->next;p->prior->next = q;q->prior = p->prior;p->prior = q;q->next->prior = p;q->next = p;}
}void PrintList(DLinkList L){DLinkList p = L->next;while(p){printf("%d ",p->data);p = p->next;}
}int main(){DLinkList L;InitList(L);for(int i=10;i>0;i--){InsertListByHead(L,i);}printf("当前链表为:");PrintList(L);printf("\n进行交换结点后:\n");DLinkList p = L->next->next;ChangeNode(p);PrintList(L);return 0;
}
测试
一般情况
倒数第二个结点
这篇关于在结点类型为DLink Node 的双链表中将p所指结点(非尾结点)与其后继结点交换的操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!