#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct node
{
int date;
node *next;
};
node* delou(node * head)
{
node *pre,*l;
pre=head;
l=head->next;
while(l!=NULL)
{
if(l->date%2==0)
{
if(l->next==NULL)
{
pre->next =NULL,free(l);
return head;
}
else
{
pre->next=l->next;
l=pre->next;
pre=l;
}
}
else
{
pre=l;
if(l->next!=NULL)
l=l->next;
else
return head;
}
}
}
void shou(node *head)
{
node *l;
l=head->next;
while(l!=NULL)
{
cout<<l->date<<" ";
l=l->next;
}
cout<<endl;
}
int main()
{
int n;
node *head,*l,*p;
head=(node*)malloc(sizeof(node));
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
p=(node*)malloc(sizeof(node));
cin>>p->date;
p->next=NULL;
if(i==0)
{
head->next=l=p;
}
else
{
l->next=p;
l=p;
}
}
head=delou(head);
shou(head);
}
return 0;
}
链表删除偶数
最新推荐文章于 2024-03-11 18:28:54 发布