/*下边这个 逻辑混乱 头结点的话 head先指向了head->next 然后 下边会继续找!!因为只用了if 下边没有用else if!!
*/
struct node * DelNode(struct node*head,int x)
/*为什么会出错?因为非常没有条理 逻辑混乱!!!!
每一个分支都不是独立的
*/
{
if (head==NULL)
/*1、空链表*/
{
printf("空链表,无可删\n");
exit(0);
} struct node* p=head;
struct node* pre=NULL;
int find=0;
if (head->data==x)
{
printf("找错头\n");
head=head->next;
/*3、首节点 */
free(p); /*此处删掉之后 应该结束了!不然 后面还会找 对吧 这是一连串的作用啊!!!!*/
return head;
} while (p!=NULL)
{
if (p->data==x)
{
find = 1;
break;
}
pre = p;
/*pre记录目标节点的前一个 */
p=p->next;
} if (find==0) /*2、没有找到的处理*/
{
printf("No this data\n");
exit(0);
} /* else if (p->next==NULL) 4、尾节点且非首节点 { pre->next=p->next; free(p); } */
else
{
pre->next=p->next;
free(p);
} return head;
}