#include<stdio.h>
typedef struct node
{
int data;
node *prior,*next;
}DNode,*DLinkList;
void reverse(DLinkList &L)
{
DNode *p,*q;
p=L;
do
{
q=p->next;
p->next=p->prior;
p->prior=q;
p=q;
}while(p!=L);
}
int main()
{
int i;
DLinkList L;
DNode *s,*r,*p;
L=(DNode*)malloc(sizeof(DNode));
r=L;
for(i=0;i<10;i++)
{
s=(DNode*)malloc(sizeof(DNode));
s->data=i;
r->next=s;
s->prior=r;
r=s;
}
r->next=L;
L->prior=r;
p=L->next;
while(p!=L)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
reverse(L);
p=L->next;
while(p!=L)
{
printf("%d",p->data);
p=p->next;
}
}
双循环链表逆置
这段代码演示了如何使用C语言实现双向链表的反转。首先定义了一个双向链表节点结构,然后通过一个do-while循环实现了链表的反转操作。在main函数中创建了一个包含10个节点的链表,并打印原始链表和反转后的链表数据。
摘要由CSDN通过智能技术生成