不能删除头节点的链表
#include"stdio.h"typedef struct node
{ int data;
struct node *next;
}NODE;
NODE *creat()
{ int x;
NODE *head,*p,*s;
head=(NODE*)malloc(sizeof(NODE));
p=head;
printf("输入整数,以0标志结束\n");
scanf("%d",&x);
while(x!=0)
{ s=(NODE*)malloc(sizeof(NODE));
s->data=x;
p->next=s;
s->next=NULL;
p=s;
scanf("%d",&x);
}
p->next=NULL;
p=head;
head=head->next;
free(p);
return head;
}
void dele(NODE *head,int x)
{ NODE *p,*q;
if(head==NULL) { printf("链表下益\n"); return ;}
if(head->data==x)
{ p=head;
head=head->next;
free(p);
}
else
{ q=head; p=head->next;
while(p!=NULL&&p->data!=x)
if(p->data!=x)
{ q=p;p=p->next;
}
if(p!=NULL)
{ q->next=p->next;
free(p);
}
else printf("没找到\n");
}
}
main()
{ NODE *head,*p;
int x;
head=creat();
p=head;
while(p!=NULL)
{ printf("%d",p->data);
p=p->next;
}
printf("输入要删除的节点:");
scanf("%d",&x);
dele(head,x);
p=head;
while(p->next!=NULL)
{ printf("%d",p->data);
p=p->next;
}
}