习题11-7 奇数值结点链表
day3 记录
struct ListNode *readlist(){
struct ListNode *t,*head,*last;
head=last=NULL;
int x;
scanf("%d",&x);
while(x != -1){
t=(struct ListNode *)malloc(sizeof(struct ListNode));
t->data=x;
t->next=NULL;
if(head == NULL){
head=t;
}else{
last->next=t;
}
last=t;
scanf("%d",&x);
}
return head;
}
struct ListNode *getodd( struct ListNode **L ){
struct ListNode *newHead=NULL,*newLast=NULL,*kill=NULL,*t;
struct ListNode *newL=(*L),*pre=(*L);
while(*L){
if(((*L)->data) % 2 == 1){
t=(struct ListNode *)malloc(sizeof(struct ListNode));
if(newHead == NULL){
newHead=t;
}else{
newLast->next=t;
}
t->data=(*L)->data;
t->next=NULL;
newLast=t;
if((*L) == newL){
newL=newL->next;
}else{
pre->next=(*L)->next;
}
kill=(*L);
(*L)=(*L)->next;
free(kill);
}else{
pre=(*L);
(*L)=(*L)->next;
}
}
(*L)=newL;
return newHead;
}