n为链表长度,s为位置,即将第s个数与第s+1个数对换 默认data是以1,2,3....n的顺序 #include<stdio h=""> #include<stdlib h=""> #include<string h=""> typedef struct node{ int data; struct node *next; struct node *prior; }Node; Node* InitList(int n){ Node *l,*p,*s; int i,j; l=(Node*)malloc(sizeof(Node)); l->next=NULL; p=(Node*)malloc(sizeof(Node)); p=l; for(i=0;i<n i="" s="(Node*)malloc(sizeof(Node));" scanf="" d="" j="" s-="">data=j; s->next=p->next;p->next=s;s->prior=p; p=s; } return l; } //初始化 void PrintList(Node *l){ Node *p; p=l->next; while(p){ printf("%3d",p->data ); p=p->next; } printf("\n"); } //打印链表元素 void fun(Node *l,int s){ Node *p,*tmp1,*tmp2; int i; p=l; for(i=0;i<s-1 i="" p="p-">next; } tmp1=p->next ;tmp2=p->next->next ; tmp1->next=tmp2->next; p->next=tmp2; tmp2->next=tmp1; //先把next指针换好 tmp2->prior=p; tmp1->prior=tmp2; tmp1->next->prior=tmp1; //再逐个调整好prior指针 } int main(){ Node *l; int n,s; printf("n="); scanf("%d",&n); printf("s="); scanf("%d",&s); l=InitList(n); printf("the original list:"); PrintList(l); fun(l,s); printf("the new list:"); PrintList(l); } </s-1></n></string></stdlib></stdio>