#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list *next;
}LNode;
int fun(LNode *h,int x)
{
LNode *p,*q;
p=h;
q=p->next;
while(q!=NULL)
{
if(q->data==x)
{
p->next=q->next;
free(q);
return 1;
}else{
p=q;
q=q->next;
}
}
return 0;
}
LNode *creatlist(int *a)
{
LNode *h,*p,*q;
int i;
h=p=(LNode *)malloc(sizeof(LNode));
for(i=0; i<N; i++)
{
q=(LNode *)malloc(sizeof(LNode));
q->data=a[i];
p->next=q;
p=q;
}
p->next=0;
return h;
}
void outlist(LNode *h)
{
LNode *p;
p=h->next;
if (p==NULL)
{
printf("\nThe list is NULL!\n");
}else{
printf("\nHead");
do{
printf("->%d",p->data);
p=p->next;
} while(p!=NULL);
printf("->End\n");
}
}
int main( )
{
LNode *head;
int x;
int a[N]={1,2,7,3,9,4,10,5};
head=creatlist(a);
printf("\nThe list before deleting :\n");
outlist(head);
x=fun(head,10);
printf("\nThe list after deleting :\n");
outlist(head);
if(x==1){
printf("查找成功!");
}else{
printf("查找失败!");
}
return 0;
}