c语言实现单链表头插法
#include<stdio.h>
#include<stdlib.h>
typedef struct lnode{
int data;
struct lnode *next;
}lnode;
void dss(lnode *l,int x){
lnode *q=l->next,*pre=l,*p;
while(q!=NULL){
if(q->data==x){
p=q;q=q->next;
pre->next=q;
free(p);
}
else{
pre=q;q=q->next;
}
}
}
void create(lnode **l){
(*l)=(lnode*)malloc(sizeof(lnode));
(*l)->next=NULL;
int x;
lnode *s;
scanf("%d",&x);
while(x!=99){
s=(lnode*)malloc(sizeof(lnode));
s->data=x;
s->next=(*l)->next;
(*l)->next=s;
scanf("%d",&x);
}
}
void printfa(lnode *l){
lnode *x;
x=l->next;
while(x!=NULL){
printf("%d ",x->data);
printf("\n");
x=x->next;
}
}
int main(){
lnode *b;
create(&b);
dss(b,2);
printfa(b);
return 0;
}