user-0 提问: 新手求助（双链表双向冒泡，程序编译通过但执行出错，求指导，附源码）

回复:
user: user-1
content: 引用 3 楼 qq_38418863 的回复:请忽视二楼  #define chartonumber(x) (x-'0')//将char类型转换为对应数值  typedef int ElemType; typedef struct DNode { 	ElemType data; 	struct DNode *prior, *next; }DNode,	*DoubleList;  void CreateList(DoubleList L)  {  	DNode *r, *s;		//r为尾指针  	char c;				//c用于接收输入值  	int flag =1; 		//设置一个标志，初值为1，当输入"$"时，flag为0，建表结束 	L->next=L; 	L->prior=L; 	r=L;              	while(flag)          	{ 		c=getchar(); 		if(c!='$') 		{ 			s=(DNode*)malloc(sizeof(DNode)); 			s->data=chartonumber(c); 			r->next=s; 			s->prior=r; 			r=s; 		} 		else 		{ 			flag=0; 			r->next=L;    			L->prior=r; 		} 	} }  //数据结构为双向循环链表  void Creatdlist(DoubleList L){ 	DNode *p; 	L=(DNode * )malloc(sizeof(DNode)); 	printf("请输入双向链表数据!:\n"); 	CreateList(L); 	p = L->next; 	while(p != L) 	{ 		printf("%d\n",p->data); 		p = p->next; 	} }   void Shakersort(DoubleList L){ 	 //DNode *p; 	 DNode *l = L;  	 DNode *r = L->next; 	 char exchange; 	 DNode *flag; 	 while(l!=r){											//左右相遇时表示排序结束，终止循环  		for(; l->next != r; l = l->next){					//向右冒泡  			if(l->data > l->next->data){ 				exchange = l->data; 				l->data = l->next->data; 				l->next->data = exchange; 				flag = l; 			} 		} 		r = flag; 		for(r = r->prior; r->prior != l; r = r->prior){		//向左冒泡  			if(r->data > r->next->data){ 				exchange = r->data; 				r->data = r->next->data; 				r->next->data = exchange; 				flag = r->next; 			} 		} 		l = flag; 	 }  }  int main(){ 	DoubleList l;	 	Creatdlist(l); 	printf("test");  	Shakersort(l);  	return 0; }   冒泡排序的算法不是这样的。可以通过交换数据间接实现节点的交换

user: user-2
content: 1楼的回复就可以了， 你排序只是排data，链表理解成数组， 数组头饰L， 结尾是L->prior

user: user-0
content: 请忽视二楼  #define chartonumber(x) (x-'0')//将char类型转换为对应数值  typedef int ElemType; typedef struct DNode { 	ElemType data; 	struct DNode *prior, *next; }DNode,	*DoubleList;  void CreateList(DoubleList L)  {  	DNode *r, *s;		//r为尾指针  	char c;				//c用于接收输入值  	int flag =1; 		//设置一个标志，初值为1，当输入"$"时，flag为0，建表结束 	L->next=L; 	L->prior=L; 	r=L;              	while(flag)          	{ 		c=getchar(); 		if(c!='$') 		{ 			s=(DNode*)malloc(sizeof(DNode)); 			s->data=chartonumber(c); 			r->next=s; 			s->prior=r; 			r=s; 		} 		else 		{ 			flag=0; 			r->next=L;    			L->prior=r; 		} 	} }  //数据结构为双向循环链表  void Creatdlist(DoubleList L){ 	DNode *p; 	L=(DNode * )malloc(sizeof(DNode)); 	printf("请输入双向链表数据!:\n"); 	CreateList(L); 	p = L->next; 	while(p != L) 	{ 		printf("%d\n",p->data); 		p = p->next; 	} }   void Shakersort(DoubleList L){ 	 //DNode *p; 	 DNode *l = L;  	 DNode *r = L->next; 	 char exchange; 	 DNode *flag; 	 while(l!=r){											//左右相遇时表示排序结束，终止循环  		for(; l->next != r; l = l->next){					//向右冒泡  			if(l->data > l->next->data){ 				exchange = l->data; 				l->data = l->next->data; 				l->next->data = exchange; 				flag = l; 			} 		} 		r = flag; 		for(r = r->prior; r->prior != l; r = r->prior){		//向左冒泡  			if(r->data > r->next->data){ 				exchange = r->data; 				r->data = r->next->data; 				r->next->data = exchange; 				flag = r->next; 			} 		} 		l = flag; 	 }  }  int main(){ 	DoubleList l;	 	Creatdlist(l); 	printf("test");  	Shakersort(l);  	return 0; }

user: user-0
content: 附上完整的定义+实现  #define chartonumber(x) (x-'0')//将char类型转换为对应数值  typedef int ElemType; typedef struct DNode { 	ElemType data; 	struct DNode *prior, *next; }DNode,	*DoubleList;  void CreateList(DoubleList L)  {  	DNode *r, *s;		//r为尾指针  	char c;				//c用于接收输入值  	int flag =1; 		//设置一个标志，初值为1，当输入"$"时，flag为0，建表结束 	L->next=L; 	L->prior=L; 	r=L;              	while(flag)          	{ 		c=getchar(); 		if(c!='$') 		{ 			s=(DNode*)malloc(sizeof(DNode)); 			s->data=chartonumber(c); 			r->next=s; 			s->prior=r; 			r=s; 		} 		else 		{ 			flag=0; 			r->next=L;    			L->prior=r; 		} 	} void Creatdlist(DoubleList L){ 	DNode *p; 	L=(DNode * )malloc(sizeof(DNode)); 	printf("请输入双向链表数据!:\n"); 	CreateList(L); 	p = L->next; 	while(p != L) 	{ 		printf("%d\n",p->data); 		p = p->next; 	} }   void Shakersort(DoubleList L){ 	 //DNode *p; 	 DNode *l = L;  //	 DNode *r = L->next; 	 char exchange; 	 DNode *flag; /*	 while(0){											//左右相遇时表示排序结束，终止循环  		for(; l->next != r; l = l->next){					//向右冒泡  			if(l->data > l->next->data){ 				exchange = l->data; 				l->data = l->next->data; 				l->next->data = exchange; 				flag = l; 			} 		} 		r = flag; 		for(r = r->prior; r->prior != l; r = r->prior){		//向左冒泡  			if(r->data > r->next->data){ 				exchange = r->data; 				r->data = r->next->data; 				r->next->data = exchange; 				flag = r->next; 			} 		} 		l = flag; 	 }*/  }  int main(){ 	DoubleList l;	 	Creatdlist(l); 	printf("test");  	Shakersort(l);  	return 0; }  }

user: user-2
content: 看不懂 你的 DoubleList 到底是什么东西，可以强转成 DNode ？ 你到底是搞双向链表 还是 循环链表？   /* l(L) -> r -> l */ void sort(DNode* L) {      DNode *l = L;      DNode *r = L->next;      char exchange; 	  	for (l = L; l != L; l = l->next) { 		for (r = l->next; r != L; r = r->next) { 			if (r->data > l->data) { 				exchange = r->data; 				r->data = l->data; 				l->data = exchange; 			} 		}	 	} }

user: user-3
content: 无返回值传参传结构体指针可以创建链表吗。