输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算: 输入若干个正整数(输入-1为结束标志),建立一个单向链表,将其中的奇数值结点删除后输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct biao * list;
struct biao{
int data;
struct biao * next;
};
list create666(){ //尾结点链表创建
list head ,p,tail;
head=tail=NULL;
int num;
while(1){
p=(list)malloc(sizeof(struct biao));
scanf("%d",&num);
if(num==-1)break;
p->data=num;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
return head;
}
list findodd666(list head){
list p1=NULL,p2;
p2=head;
while(p2) //防止段错误
{
if(p2->data%2==1)
if(p2==head) {
head=head->next; p2=head; //头结点后移
}
else{
p1->next=p2->next; //删除奇数
p2=p2->next;
}
else{
p1=p2; p2=p2->next; //p1p2都后移
}
}
return head;
}
int main(){
list head=NULL,p=NULL;
int x;
scanf("%d\n",&x);
for(int i=0;i<x;i++){
head=create666();
head=findodd666(head);
if(head!=NULL){
for(p=head;p->next!=NULL;p=p->next){
printf("%d ",p->data);
}
printf("%d\n",p->data);
}
}
return 0;
}