习题11-8 单链表结点删除
读取链表
struct ListNode *readlist(){
struct ListNode *head,*tail,*t;
head=tail=NULL;
int x;
scanf("%d",&x);
while(x != -1){
t=(struct ListNode *)malloc(sizeof(struct ListNode));
t->data=x;
t->next=NULL;
if(head == NULL){
head=t;
}else{
tail->next=t;
}
tail=t;
scanf("%d",&x);
}
return head;
}
删除值为m的节点
struct ListNode *deletem( struct ListNode *L, int m ){
struct ListNode *cur,*pre,*t;
while(L->data == m){
L=L->next;
if(L == NULL){
return L;
}
}
pre=L;
cur=L->next;
while(cur != NULL){
if(cur -> data == m){
pre->next=cur->next;
cur=pre->next;
}else{
pre=cur;
cur=cur->next;
}
}
return L;
}