单链表中偶数节点的删除

#include
#include
#include
typedef struct stu{
int data;
struct stu *next;
}s,*link;
void output(link h)
{
    s *p;
    p=h->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
void creat(link h)
{
  s *pre;
  pre=h;

  while(pre->next!=NULL)
  {
     if((pre->next->data)%2==0)
       {
           pre->next=pre->next->next;//此时尾指针不可再向前移动,若移动则无法判断当前节点是否为偶数,前几次因为移动指针而出错
       }

   else
   {
      pre=pre->next;
   }
  }

int main()
{
  int x;
  link h;
  s *r,*p;
  h=(s*)malloc(sizeof(s));
  h->next=NULL;
  r=h;
  while(scanf("%d",&x)!=EOF&&x)
   {
        p=(s*)malloc(sizeof(s));
        p->data=x;
        r->next=p;
        r=p;
   }
   r->next=NULL;
   creat(h);
   output(h);

    return 0;
}
 

你可能感兴趣的:(单链表中偶数节点的删除)