輸入一個(gè)正整數(shù) repeat (0<repeat<10),做 repeat 次下列運(yùn)算: 輸入若干個(gè)正整數(shù)(輸入-1為結(jié)束標(biāo)志),建立一個(gè)單向鏈表,將其中的奇數(shù)值結(jié)點(diǎn)刪除后輸出#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef?struct?Node
{?
int?data;
struct?Node?*next;?
}No;
No*?createlist()
{
? No?*?head;
? No?*?p,*?pre;
? int?i,a;
head=(No*)malloc(sizeof(No));
head->next=NULL;
pre=head;
while(scanf("%d",&a))
{
if(a!=-1)
????{
p=(No*)malloc(sizeof(No));
???? p->data=a;
pre->next=p;
pre=p;
}
else
{
pre->next=NULL;
break;
}
???}?
??return?head;
}
void?print(No*head)
{
int?i,n=0;
No*?q=head;
No*h=head->next;
while(h)
{
if((h->data)%2!=0)
{
q->next=h->next;
h=h->next;
}
else{
q=h;
???? h=h->next;
}
}
free(h);
while(head->next->next)
{
printf("%d?",head->next->data);
head=head->next;
}
printf("%d",head->next->data);
}
int?main()
{
int?n,i;
scanf("%d",&n);
for(i=0;i<n;i++){
print(createlist());
printf("\n");
}
return?0;
}輸入樣例:2?(repeat=2)
1?2?3?4?5?6?7?-1
1?3?5?-1輸出樣例:2?4?6
鏈表中的段錯(cuò)誤,幫忙找一下吧
weibo_殤雨916_0
2016-05-17 12:58:52