user-0 提问: 奇数值结点链表（pta）

回复:
user: user-1
content: struct ListNode *readlist() { 	struct ListNode *head,*pre,*p; 	head=(struct ListNode*)malloc(sizeof(struct ListNode)); 	head->next=NULL; 	pre=head;//保存head地址，并替代head进行操作  	while(1) 	{ 	    p=(struct ListNode*)malloc(sizeof(struct ListNode)); 	    scanf("%d",&p->data); 	    if(p->data==-1) 	    break; 		p->next=NULL; 		pre->next=p; 		pre=p; 	} 	free(p); 	return head->next; } struct ListNode *deletem_1( struct ListNode *L)//删除奇数 { 	struct ListNode *head,*p; 	head=L; 	p=L->next; 	while(p) 	{ 		if(p->data%2!=0) 		{ 			L->next=p->next; 			free(p); 			p=L->next; 			continue; 		} 		L=p; 		p=p->next; 	} 	if(head->data%2!=0) 	head=head->next; 	return head; } struct ListNode *deletem_2( struct ListNode *L)//删除偶数 { 	struct ListNode *head,*p; 	head=L; 	p=L->next; 	while(p) 	{ 		if(p->data%2==0) 		{ 			L->next=p->next; 			free(p); 			p=L->next; 			continue; 		} 		L=p; 		p=p->next; 	} 	if(head->data%2==0) 	head=head->next; 	return head; } struct ListNode *getodd( struct ListNode **L ) { 	struct ListNode *head,*pre,*p,*Q;//创建一个新链表，使其与*L链表相等 	head=(struct ListNode*)malloc(sizeof(struct ListNode)); 	head->next=NULL; 	pre=head; 	Q=*L; 	while(Q) 	{ 		p=(struct ListNode*)malloc(sizeof(struct ListNode)); 		p->data=Q->data; 		p->next=NULL; 		pre->next=p; 		pre=p; 		Q=Q->next; 	} 	*L=deletem_1(*L);//得到删除奇数的链表 	return deletem_2(head->next);//得到删除偶数的链表. }

user: user-2
content: #include <stdio.h> #include <stdlib.h>  struct ListNode {     int data;     struct ListNode *next; };  struct ListNode *readlist(); struct ListNode *getodd( struct ListNode **L );  void printlist( struct ListNode *L ) {     struct ListNode *p = L->next;      while (p) {         printf("%d ", p->data);         p = p->next;     }     printf("\n"); }  int main() {     struct ListNode *L, *Odd;     L = readlist();     Odd = getodd(&L);     printf("Odd list: ");     printlist(Odd);     printf("Even list: ");     printlist(L);      return 0; }  /* 你的代码将被嵌在这里 */ struct ListNode *readlist() {     struct ListNode *head,*tail;     struct ListNode *p;     int size = sizeof(struct ListNode);     head = (struct ListNode*)malloc(size);     head->next = NULL;     p = head;     while(1) {         tail = (struct ListNode*)malloc(size);         scanf("%d", &tail->data);         if (tail->data==-1) {             p->next = NULL;             break;         }         p->next=tail;         p = tail;     }     free(tail);   /* 最后一个节点申请了，但是没有用要释放掉 */      return head;   /*返回一个带哨兵的链表*/ }   struct ListNode *getodd( struct ListNode **L )  //将单链表L中奇数值的结点分离出来，重新组成一个新的链表 {     struct ListNode *odd_head, *prev, *pcur, *pnew;     int size = sizeof(struct ListNode);      odd_head = (struct ListNode *)malloc(size);     if (!odd_head)         exit(1);      prev = *L;     pcur = (*L)->next;     pnew = odd_head;      while (pcur) {         if (pcur->data % 2) {             pnew->next = pcur;             pnew = pcur;             prev->next = pcur->next;             pcur = pcur->next;         } else {             prev = prev->next;             pcur = pcur->next;         }     }     pnew->next = NULL;      return odd_head;     /*     struct ListNode *head,*oddhead,*evenhead,*oddtail,*eventail,*odd,*even,*first,*temp;     head = (struct ListNode*)malloc(size);     oddhead = (struct ListNode*)malloc(size);  odd=oddhead;     evenhead =  (struct ListNode*)malloc(size); even=evenhead;     oddhead->next=NULL;     evenhead->next=NULL;     head=*L;     first=*L;temp=first;              //代表传入的指针的首地址     if(head!=NULL){         if(head->data%2!=0){oddhead=*L;odd=oddhead;}         if(head->data%2==0){evenhead=*L;even=evenhead;}     }      while(head->next!=NULL){         if(head->next->data%2!=0){   //从第二个节点开始判断 (奇数)             head=head->next;             oddtail=head;             oddhead->next=oddtail;             oddhead=oddtail;         }         else if(head->next->data%2==0){  //从第二个节点开始判断 （偶数）             head=head->next;             eventail=head;             evenhead->next=eventail;             evenhead=eventail;         }     }      if(first->data%2==0)      //第一个为偶数     {         *L=first;         return odd->next;     }     else                    //第一个为奇数     {         *L=even->next;         return first;     }     */  }  参考一下吧，对于要求，若定义一个带哨兵的单链表会比较容易解决。详见上面的代码； 分离函数可以简单一些，没必要定义那么多指针变量，因为变量多了会让程序看着不容易理解。

user: user-0
content: 我的第一次提问，实在不知道哪里出错了，求大佬帮帮我，我想了快一天了┭┮﹏┭┮

user: user-3
content: 数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633