链表节点定义为: struct Node{ int data; struct Node *next; }
编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算: 输入若干个正整数(输入-1为结束标志),建立一个单向链表,将其中的奇数值结点删除后输出
输入输出示例:括号内为说明
输入样例:
2 (repeat=2)
1 2 3 4 5 6 7 -1
1 3 5 -1
输出样例:
2 4 6
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Node{
int data;
struct Node *next;
};
struct Node *Creat_();
struct Node *shanchu(struct Node *head);
void print(struct Node *head);
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
struct Node *head;
head=NULL;
head=Creat_();
head=shanchu(head);
print(head);
}
return 0;
}
struct Node *Creat_()
{
int data;
struct Node *head,*p,*t;
head=NULL;
scanf("%d",&data);
while(data!=-1)
{
p=(struct Node *)malloc(sizeof(struct Node));
p->data =data;
if(head==NULL)
{head=p;
t=head;
}
else
{
t->next = p;
t=t->next;
}
scanf("%d",&data);
}
t->next =NULL;
return head;
}
struct Node *shanchu(struct Node *head)
{
struct Node *p,*p1;
while(head!=NULL&&head->data%2==1)
{
p=head;
head=head->next;
free(p);
}
if(head==NULL)
return NULL;
p=head;
p1=head->next;
while(p1!=NULL)
{
if(p1->data%2==1)
{
p->next = p1->next ;
free(p1);
}
else
p=p->next ;
p1=p->next ;
}
return head;
}
void print(struct Node *head)
{
struct Node *p;
for(p=head;p!=NULL;p=p->next)
{
if(p->next!=NULL)
printf("%d ",p->data );
else
printf("%d\n",p->data);
}
}